-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagedrawer.js
310 lines (257 loc) · 10.5 KB
/
imagedrawer.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*!
* ImageDrawer.js - jQuery plugin to animate a drawing image
*
* @version v1.2.0
* @link GitHub - https://github.com/UstymUkhman/ImageDrawer.js
* @license MIT License - https://opensource.org/licenses/MIT
* @author Ustym Ukhman - <[email protected]>
*
*/
/**
* Usage:
* ------
*
* <div id="container">
* <img src="./path/to/image/<image>.jpg">
* </div>
* _________________________________________
*
* $(div#container).drawImge({
* duration: 20, @number|@object - seconds it's take to draw the entire picture
*
* Instead of specifying the duration on the whole animation,
* || { it's also possible to set the duration of single drawing phases:
* borderPencil : 9, @number - seconds it's take to draw the picture by using only the pencil for borders
* pencilShades : 6, @number - seconds it's take to draw sharpest shades with black pencil
* colorShades : 7.5, @number - seconds it's take to draw first, basic, vanish colors
* fullColors : 7.5 @number - seconds it's take to define better all colors on the picture
* },
*
* || [9, 6, 7.5, 7.5], Same phase timing definition, but with the array method
*
*
* background: '#FFFFFF', @string - background color for image while it's been drawing
* callback: fn(), @function - function to execute after the last phase
*
* pencil: {
* width : '50px', @string|@number - pencil image width
* height : '50px', @string|@number - pencil image height
* marginTop : -40, @string|@number - initial margin top distance
* marginLeft: -15, @string|@number - initial margin left distance
*
* disappear : 5, @number - seconds for a disappearing pencil animation
* fromBottom: true, @boolean - if setted to "true" the pencil will start drawing from right bottom corner of the image ("false" by default)
* invertAxis: false, @boolean - if setted to "true" the pencil will draw vertically, if "false" - horizontally ("false" by default)
* src : './img/pencil.png' @string - path to the pencil image
* }
* });
*
*/
'use strict';
(function($) {
$.fn.extend({
/**
* $.drawImage() - function to imitate accelerated drawn image
*
* @param {object|function} - custom drawing options | callback function
* @function drawImage
* @memberof jQuery
* @public
*
* @returns {array} - object(s) selected by $
*/
drawImage: function(args) {
// Phase wacher:
var currPhase = 0,
// Callback function:
cb = null,
// Custom options:
options = { },
// Image reference:
$image = $(this).find('img'),
// Object with duration value of each phase:
timing = null,
// Drawing pencil icon:
$pencilImage = null,
// Drawing pencil animation:
pencilAnimationID = null;
// Checking for a calback or custom options:
if (typeof args === 'function') cb = args;
else if (typeof args === 'object') options = args;
var $imgBackground = null,
// Setting up custom or default options:
opts = {
duration: options.duration || { borderPencil: 6, pencilShades: 4,
colorShades: 5, fullColors: 5 },
callback: cb || options.callback,
pencil: options.pencil || null,
background: options.background || null
},
// Check for correct pencil options types:
checkOptionsType = function(pencilOpts) {
var ok = true;
$.each(pencilOpts, function(key, value) {
if (key === 'src' || key === 'invertAxis' || key === 'fromBottom') {
return;
}
if (typeof value !== 'number') {
console.warn('The value of \"' + key + '\" in \"pencil\" has to be a number.');
ok = false;
}
});
return ok;
},
// Set up the pencil animation function with passed (or default) options:
setPencilAnimation = function($pencil, opt) {
var x = (opt.pos.fromBottom) ? opt.width : opt.pos.marginLeft,
y = (opt.pos.fromBottom) ? opt.height : opt.pos.marginTop,
Y = (opt.pos.invertAxis) ? 5.925925 : 177.77777,
X = (opt.pos.invertAxis) ? 100 : 10,
xStep = opt.width / X,
yStep = opt.height / Y,
xNextStep = x + xStep,
yNextStep = y + yStep,
X = x,
Y = y;
if (opt.pos.fromBottom) {
X = opt.pos.marginLeft;
Y = opt.pos.marginTop;
xStep = -xStep;
yStep = -yStep;
}
// Pencil animation function:
var pencilAnim = function() {
$pencil.css({'transform': 'translate3d(' + x + 'px, ' + y + 'px, 0px)',
'-webkit-transform': 'translate3d(' + x + 'px, ' + y + 'px, 0px)'});
if (xNextStep >= opt.width || xNextStep <= X) xStep = -xStep;
x += xStep;
xNextStep = x + xStep;
if (yNextStep >= opt.height || yNextStep < Y) yStep = -yStep;
y += yStep;
yNextStep = y + yStep;
pencilAnimationID = requestAnimationFrame(pencilAnim);
};
pencilAnimationID = requestAnimationFrame(pencilAnim);
};
// Set up the duration of drawing phases:
if ($.isArray(opts.duration)) {
var d = 0;
timing = {
borderPencil: opts.duration[0] + 's',
pencilShades: opts.duration[1] + 's',
colorShades : opts.duration[2] + 's',
fullColors : opts.duration[3] + 's'
};
for (var i = 0; i < 4; i++) {
d += opts.duration[i];
}
}
if (typeof opts.duration === 'number') {
var quarter = opts.duration / 4,
tenth = opts.duration / 10;
timing = {
borderPencil: tenth * 6 + 's',
pencilShades: tenth * 4 + 's',
colorShades : quarter + 's',
fullColors : quarter + 's'
};
} else if (!$.isArray(opts.duration)) {
timing = {
borderPencil: opts.duration.borderPencil + 's',
pencilShades: opts.duration.pencilShades + 's',
colorShades : opts.duration.colorShades + 's',
fullColors : opts.duration.fullColors + 's'
};
}
// Set pencil image if defined
if (opts.pencil !== null && checkOptionsType(opts.pencil)) {
$pencilImage = $('<img>')
.attr({src: opts.pencil.src})
.css({'position': 'absolute', 'width': opts.pencil.width + 'px',
'height': opts.pencil.height + 'px', 'z-index': 1500});
$(this).prepend($pencilImage);
setPencilAnimation($pencilImage, {
height: $image.height() + opts.pencil.marginTop,
width: $image.width() + opts.pencil.marginLeft,
pos: opts.pencil
});
}
if (opts.background) {
// Creating a background:
$imgBackground = $('<div>')
.addClass('imgBackground')
.css({'background-color': opts.background,
'animation-duration': timing.borderPencil,
'-webkit-animation-duration': timing.borderPencil,
'width': $(this).width(), 'height': $(this).height()});
// Set up the background image:
$(this).prepend($imgBackground);
}
// Starting to draw the picture:
$image
// Phase 1:
.addClass('visibleImage borderPencil')
.css({'animation-duration': timing.borderPencil,
'-webkit-animation-duration': timing.borderPencil})
.on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
// Render image's style each phase:
function() {
var oldClass, newClass, duration;
switch (currPhase++) {
// Phase 2:
case 0:
oldClass = 'borderPencil';
newClass = 'pencilShades';
duration = timing.pencilShades;
if ($imgBackground) $imgBackground.remove();
break;
// Phase 3:
case 1:
oldClass = 'pencilShades';
newClass = 'colorShades';
duration = timing.colorShades;
break;
// Phase 4:
case 2:
oldClass = 'colorShades';
newClass = 'fullColors';
duration = timing.fullColors;
break;
case 3:
// Restore to initial state:
$image.css({'filter': 'brightness(1.05) saturate(1.05)',
'-webkit-filter': 'brightness(1.05) saturate(1.05)'})
.removeClass('fullColors').removeClass('visibleImage');
// Animate pencil image if setted
if (pencilAnimationID !== null) {
cancelAnimationFrame(pencilAnimationID);
if (opts.pencil.disappear) {
$pencilImage
.addClass('pencil')
.css({'top': $pencilImage.offset().top + 'px',
'left': $pencilImage.offset().left + 'px',
'animation-duration': opts.pencil.disappear + 's',
'-webkit-animation-duration': opts.pencil.disappear + 's'});
}
// Remove pencil and call the callback if defined:
setTimeout(function() {
$pencilImage.remove();
if (opts.callback !== null) {
opts.callback();
}
}, opts.pencil.disappear * 1000);
} else if (opts.callback !== null) {
opts.callback();
}
return;
}
// Updating new drawing phases by CSS classes:
$image.removeClass(oldClass).addClass(newClass)
.css({'animation-duration' : duration,
'-webkit-animation-duration': duration});
});
// jQuery style:
return this;
}
});
})(jQuery);