forked from merri-ment/lazy-line-painter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazylinepainter-1.4.1.js
311 lines (230 loc) · 6.87 KB
/
jquery.lazylinepainter-1.4.1.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
311
/*
* Lazy Line Painter 1.4.1
* SVG Stroke animation.
*
* https://github.com/camoconnell/lazy-line-painter
* http://www.camoconnell.com
*
* Copyright 2013 Cam O'Connell
* Licensed under the MIT license.
*
*/
(function( $, window, undefined ){
var dataKey = 'lazyLinePainter';
var methods = {
// setup lazy line data
init : function( options ) {
return this.each(function(){
var $this = $(this),
d = $this.data( dataKey );
$this.addClass('lazy-line');
// If the plugin hasn't been initialized yet
if ( ! d ) {
/*
SETUP DATA
*/
// Collect settings, define defaults
var o = $.extend( {
'width' : null,
'height' : null,
'strokeWidth' : 2,
'strokeColor' : '#000',
'strokeCap' : 'round',
'strokeJoin' : 'round',
'strokeOpacity' : 1,
'strokeDash' : null,
'onComplete' : null,
'delay' : null,
'overrideKey' : null
}, options);
// Set up path information
// if overrideKey has been defined - use overrideKey as key within the svgData object.
// else - use the elements id as key within the svgData object.
var target = ( o.overrideKey === null ) ? $this.attr('id').replace('#','') : o.overrideKey;
var $w = o.svgData[target].dimensions.width,
$h = o.svgData[target].dimensions.height;
o.svgData = o.svgData[target].strokepath;
// Setup dimensions
if( o.width === null ) o.width = $w;
if( o.height === null ) o.height = $h;
// Setup Rapheal
var $s = $this.attr("id"); // Requires Id
var paper = new Raphael($s, $w, $h);
/*
BIND DATA TO ELEMENT
*/
$this.data( dataKey , {
'svgData' : o.svgData,
'width' : o.width,
'height' : o.height,
'strokeWidth' : o.strokeWidth,
'strokeColor' : o.strokeColor,
'strokeCap' : o.strokeCap,
'strokeJoin' : o.strokeJoin,
'strokeOpacity' : o.strokeOpacity,
'strokeDash' : o.strokeDash,
'onComplete' : o.onComplete,
'delay' : o.delay,
'overrideKey' : o.overrideKey,
'paper' : paper,
'count' : 1,
'complete' : false,
'playhead' : 0,
'setTimeOutHandler' : []
});
}
});
},
/*
PAINT LAZY LINE DATA
*/
paint : function( ) {
return this.each(function(){
var $this = $(this),
d = $this.data( dataKey );
var init = function(){
// Set width / height of container element
$this.css({'width' : d.width, 'height' : d.height});
// Loop paths
$.each(d.svgData, function (i, val) {
var p = d.paper.path(val.path);
p.attr({
"stroke" : "none",
"stroke-width": d.strokeWidth,
"fill-opacity": 0
});
var sto = setTimeout(function () {
var s = draw({
'count' : d.count,
'canvas' : d.paper,
'pathstr' : p,
'duration' : val.duration,
'attr' : applyStyles( d, val ),
'callback' : function (e) {
// remove reference to setTimeOut
d.setTimeOutHandler.splice(d.count,1);
d.count++;
if ((d.svgData.length+1) == d.count){
d.complete = true;
if(d.onComplete !== null) d.onComplete.call($this);
}
}
});
}, d.playhead);
d.playhead += val.duration;
// Keep track of setTimeOuts calls
d.setTimeOutHandler.push(sto);
});
};
// if delay isset
if(d.delay === null)
init();
else
setTimeout(init, d.delay);
});
},
/*
ERASE LAZY LINE DATA
*/
erase : function( ) {
return this.each(function(){
var $this = $(this);
$this.find('svg').empty();
d = $this.data( dataKey );
// reset properties
for (i = 0; i < d.setTimeOutHandler.length; i++) {
clearTimeout( d.setTimeOutHandler[i] );
}
d.playhead = 0;
d.count = 0;
d.complete = false;
});
},
/*
DESTROY LAZY LINE DATA & ELEMENT
*/
destroy : function( ) {
return this.each(function(){
var $this = $(this),
d = $this.data( dataKey );
$this.removeData( dataKey );
$this.remove();
});
},
/*
STAMP LAZY LINE DATA
*/
stamp : function( ) {
return this.each(function(){
var $this = $(this),
d = $this.data( dataKey );
var init = function(){
// Set width / height of container element
$this.css({'width' : d.width, 'height' : d.height});
// Loop paths
//$.each(d.svgData, function (i, val) {
for (i = 0; i < d.svgData.length; i++) {
d.paper.path( d.svgData[i].path ).attr( applyStyles( d, d.svgData[i] ) );
}
};
// if delay isset
if(d.delay === null)
init();
else
setTimeout(init, d.delay);
});
}
};
var applyStyles = function( data, value ) {
var styles = {
"stroke" : ( !value.strokeColor ) ? data.strokeColor : value.strokeColor,
"fill-opacity" : 0,
"stroke-dasharray": ( !value.strokeDash ) ? data.strokeDash : value.strokeDash,
"stroke-opacity" : ( !value.strokeOpacity )? data.strokeOpacity : value.strokeOpacity,
"stroke-width" : ( !value.strokeWidth ) ? data.strokeWidth : value.strokeWidth,
"stroke-linecap" : ( !value.strokeCap ) ? data.strokeCap : value.strokeCap,
"stroke-linejoin" : ( !value.strokeJoin ) ? data.strokeJoin : value.strokeJoin
};
return styles;
};
var draw = function( settings ) {
var canvas = settings.canvas,
pathstr = settings.pathstr,
duration = settings.duration,
attr = settings.attr,
callback = settings.callback;
var guide_path;
if ( typeof( pathstr ) == "string" )
guide_path = canvas.path( pathstr ).attr( { stroke: "none", fill: "none" } );
else
guide_path = pathstr;
var path = canvas.path( guide_path.getSubpath( 0, 1 ) ).attr( attr ),
total_length = guide_path.getTotalLength( guide_path ),
last_point = guide_path.getPointAtLength( 0 ),
start_time = new Date().getTime(),
interval_length = 25;
var interval_id = setInterval( function()
{
var elapsed_time = new Date().getTime() - start_time,
this_length = elapsed_time / duration * total_length,
subpathstr = guide_path.getSubpath( 0, this_length );
attr.path = subpathstr;
path.animate( attr, interval_length );
if ( elapsed_time >= duration )
{
clearInterval( interval_id );
if ( callback !== undefined ) callback();
guide_path.remove();
}
}, interval_length );
};
$.fn.lazylinepainter = function(method){
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
// error
}
};
})( jQuery, window );