-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelPerfect.js
More file actions
186 lines (151 loc) · 5.36 KB
/
pixelPerfect.js
File metadata and controls
186 lines (151 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(function() {
'use strict';
var PixelPerfect = function() {
var optionsLayout = {
display: true,
text: "Drop Image",
zone: document.querySelector('body'),
move: 10
};
return {
// initialisation
init: function() {
this.layout();
this.moveKeyboard();
this.getMove();
},
// set box
layout: function() {
// container div
var thumbnails = document.createElement('div');
thumbnails.setAttribute('id', 'thumbnails');
thumbnails.innerHTML = optionsLayout.text;
// input options
var inputOptions = document.createElement('input');
inputOptions.setAttribute('type', 'text');
inputOptions.setAttribute('class', 'pixelMove');
optionsLayout.zone.appendChild(thumbnails);
optionsLayout.zone.appendChild(inputOptions);
optionsLayout.zone.appendChild( document.createTextNode("move" + optionsLayout.move) );
thumbnails.insertAdjacentHTML('afterBegin', '<img src="" />');
},
getMove: function() {
var img = document.querySelector('#thumbnails img');
// change value for move
document.querySelector('.pixelMove').addEventListener('change', function() {
optionsLayout.move = (document.querySelector('.pixelMove') !== null) ? document.querySelector('.pixelMove').value : optionsLayout.move;
}, false);
// on drag, move img
img.addEventListener('mousedown', function(e) {
var initialized = {
x : img.offsetLeft - e.pageX,
y : img.offsetTop - e.pageY
};
var setPosition = function(e) {
img.style.top = ( initialized.y + e.pageY ) + 'px';
img.style.left = ( initialized.x + e.pageX ) + 'px';
};
document.addEventListener('mousemove', setPosition, false);
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', setPosition, false);
}, false);
});
},
// press keyboard to move
moveKeyboard: function() {
var img = document.querySelector('#thumbnails img');
var positionLeft = parseInt(img.offsetLeft, 10);
var positionTop = parseInt(img.offsetTop, 10);
optionsLayout.zone.addEventListener('keydown', function(e) {
if ( e.keyCode == 40) {
positionTop += parseInt( optionsLayout.move, 10 );
}
if ( e.keyCode == 38) {
positionTop -= parseInt( optionsLayout.move, 10 );
}
if ( e.keyCode == 37) {
positionLeft -= parseInt( optionsLayout.move, 10 );
}
if ( e.keyCode == 39) {
positionLeft += parseInt( optionsLayout.move, 10 );
}
img.style.top = positionTop + 'px';
img.style.left = positionLeft + 'px';
});
}
};
};
// upload image
var UploadImage = function() {
return {
// initialisation
init: function() {
// Setup drag and drop handlers.
var thumbnails = document.querySelector('#thumbnails');
thumbnails.addEventListener('dragenter', this.onDragEnter, false);
thumbnails.addEventListener('dragover', this.onDragOver, false);
thumbnails.addEventListener('dragleave', this.onDragLeave, false);
thumbnails.addEventListener('drop', this.onDrop, false);
this.onDragEnter();
this.onDragOver();
this.onDragLeave();
},
// drag and drop
onDragEnter: function(e) {
e.stopPropagation();
e.preventDefault();
},
onDragOver: function(e) {
e.stopPropagation();
e.preventDefault();
},
onDragLeave: function(e) {
e.stopPropagation();
e.preventDefault();
},
onDrop: function(e) {
e.stopPropagation();
e.preventDefault();
var readFileSize = 0;
var files = e.dataTransfer.files;
// Loop through list of files user dropped.
for (var i = 0, file; file = files[i]; i++) {
readFileSize += file.fileSize;
// Only process image files.
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
var reader = new FileReader();
reader.onerror = function(e) {
alert('Error code: ' + e.target.error.code);
};
// Create a closure to capture the file information.
reader.onload = (function(aFile) {
return function(evt) {
// Generate angle between -30 and 30 degrees.
var deg = Math.floor(Math.random() * 31);
deg = Math.floor(Math.random() * 2) ? deg : -deg;
var data = {
'name': aFile.name,
'src': evt.target.result,
'fileSize': aFile.fileSize,
'type': aFile.type,
'rotate': deg
};
// Render thumbnail with the file info (data object).
document.querySelector('#thumbnails img').src = data.src;
};
})(file);
// Read in the image file as a data url.
reader.readAsDataURL(file);
}
return false;
}
};
};
var pixel = new PixelPerfect();
var upload = new UploadImage();
pixel.init();
upload.init();
})();