forked from merri-ment/lazy-line-painter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazy-line-painter.js
256 lines (191 loc) · 6.13 KB
/
jquery.lazy-line-painter.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
/*
* Lazy Line Painter 1.1
* SVG Stroke animation.
*
* https://github.com/camoconnell/lazy-line-painter
*
* Copyright 2013
* Cam O'Connell - http://www.camoconnell.com http://www.behance.net/camoconnell
* Released under the WTFPL license - http://sam.zoy.org/wtfpl/
*
*/
(function( $, window, undefined ){
var methods = {
// setup lazy line data
init : function( options ) {
return this.each(function(){
var $this = $(this),
data = $this.data('lazyLinePainter');
$this.addClass('lazy-line');
// If the plugin hasn't been initialized yet
if ( ! data ) {
/*
SETUP DATA
*/
// Collect settings, define defaults
var settings = $.extend( {
'width' : null,
'height' : null,
'strokeWidth' : 2,
'strokeColor' : '#000',
'strokeCap' : 'round',
'strokeJoin' : 'round',
'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 = (settings.overrideKey == null ) ? $this.attr('id').replace('#','') : settings.overrideKey;
var $thisWidth = settings.svgData[target].dimensions.width,
$thisHeight = settings.svgData[target].dimensions.height;
settings.svgData = settings.svgData[target].strokepath;
// Setup dimensions
if( settings.width == null ){ settings.width = $thisWidth }
if( settings.height == null ){ settings.height = $thisHeight }
// Setup Rapheal
var paper = new Raphael($this.attr("id"), $thisWidth, $thisHeight);
/*
BIND DATA TO ELEMENT
*/
$this.data('lazyLinePainter', {
'svgData' : settings.svgData,
'width' : settings.width,
'height' : settings.height,
'strokeWidth' : settings.strokeWidth,
'strokeColor' : settings.strokeColor,
'strokeCap' : settings.strokeCap,
'strokeJoin' : settings.strokeJoin,
'onComplete' : settings.onComplete,
'delay' : settings.delay,
'overrideKey' : settings.overrideKey,
'paper' : paper,
'count' : 1,
'complete' : false,
'playhead' : 0,
'setTimeOutHandler' : []
});
}
});
},
// Paint Lazy Line data
paint : function( ) {
return this.each(function(){
var $this = $(this),
data = $this.data('lazyLinePainter');
var init = function(){
// Set width / height of container element
$this.css({'width' : data.width, 'height' : data.height});
// Loop paths
$.each(data.svgData, function (i, val) {
var p = data.paper.path(val.path);
p.attr({
stroke: 'none',
"stroke-width": data.strokeWidth,
'fill-opacity': 0
});
var sto = setTimeout(function () {
var s = draw({
'canvas' : data.paper,
'pathstr' : p,
'duration' : val.duration,
'attr' : {
stroke: data.strokeColor,
"fill-opacity" : 0,
"stroke-width" : data.strokeWidth,
"stroke-linecap" : data.strokeCap,
"stroke-linejoin" : data.strokeJoin
},
'callback' : function (e) {
// remove reference to setTimeOut
data.setTimeOutHandler.splice(data.count,1);
data.count++;
if (data.svgData.length == data.count){
data.complete = true;
if(data.onComplete != null) data.onComplete.call($this);
}
}
})
}, data.playhead);
data.playhead += val.duration;
// Keep track of setTimeOuts calls
data.setTimeOutHandler.push(sto);
});
}
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,
result = path;
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 );
return result;
}
// if delay isset
if(data.delay == null){
init();
} else {
setTimeout(init, data.delay);
}
})
},
// erase lazy line art but keep element and data.
erase : function( ) {
return this.each(function(){
var $this = $(this);
$this.find('svg').empty();
data = $this.data('lazyLinePainter');
// reset properties
$.each(data.setTimeOutHandler,function(){
clearTimeout(this);
});
data.playhead = 0;
data.count = 0;
data.complete = false;
})
},
// destroy element and data
destroy : function( ) {
return this.each(function(){
var $this = $(this),
data = $this.data('lazyLinePainter');
$this.removeData('lazyLinePainter');
$this.remove();
})
}
};
$.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 );