This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circleFactory.js
345 lines (295 loc) · 12.2 KB
/
circleFactory.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(function ($) {
// Plugin Definition.
$.fn.circleFactory = function (methodOrOptions) {
var method = (typeof methodOrOptions === 'string') ? methodOrOptions : undefined;
var $canvas;
var element = $(this);
var Circle = function (x = 0, y = 0, xc = false, yc = false, a = 0.01) {
var _ = this;
_.x = x;
_.y = y;
_.radius = 3;
_.alpha = a;
_.xcenter = xc;
_.ycenter = yc;
};
var CircleFactory = function ($el, options) {
var _ = this;
_.canvas = $el;
_.ctx = $el[0].getContext('2d');
_.delta = 0.3;
_.circleArray = [];
_.gap = 20;
_.dpi = window.devicePixelRatio;
_.totalCount = 0;
_._defaults = {
color: '#828487',
x: 15,
y: 15,
path: [],
};
_.pathCrawler = 0;
_.currentDirection = '';
_.xCenter = false;
_.directionCount = 0;
_.pathCounter = 1;
_._options = $.extend(true, {}, _._defaults, options);
_.options = function (options) {
return (options) ?
$.extend(true, _._options, options) :
_._options;
};
};
CircleFactory.prototype.stop = function () {
window.cancelAnimationFrame(this.animationFunc);
};
CircleFactory.prototype.resizeCanvas = function () {
var _ = this;
_.canvas.attr('width', _.canvas.parent().outerWidth());
_.canvas.attr('height', _.canvas.parent().outerHeight());
window.addEventListener('resize', function () {
_.canvas.attr('width', _.canvas.parent().outerWidth());
_.canvas.attr('height', _.canvas.parent().outerHeight());
_.clearCanvas();
_.resizeDrawCircles();
}, false);
};
/**
* Add a new circle object to the circle array.
*
* @param int x new circle x axis.
* @param int y new circle y axis.
*/
CircleFactory.prototype.addCircle = function (x, y, xc, yc, a) {
var _ = this;
_.circleArray[_.circleArray.length] = new Circle(x, y, xc, yc, a);
};
CircleFactory.prototype.fixCanvasDpi = function () {
var _ = this;
//get CSS height
//the + prefix casts it to an integer
//the slice method gets rid of "px"
var style_height = +getComputedStyle(_.canvas[0]).getPropertyValue("height").slice(0, -2);
//get CSS width
var style_width = +getComputedStyle(_.canvas[0]).getPropertyValue("width").slice(0, -2);
//scale the canvas
_.canvas[0].setAttribute('height', style_height * _.dpi);
_.canvas[0].setAttribute('width', style_width * this.dpi);
};
/**
* Clear the canvas inserted to the DOM by the plugin.
*
* @param object el DOM element were the plugin is initialized.
*/
CircleFactory.prototype.clearCanvas = function () {
var _ = this;
_.ctx.clearRect(0, 0, $(_.canvas).width(), $(_.canvas).height());
};
/**
* Updates the circle's opacity within the CircleArray.
*/
CircleFactory.prototype.updateCircles = function () {
var _ = this;
for (var i = 0; i < _.circleArray.length; i++) {
// Increase circle opacity if aplha is less than 1
if (_.circleArray[i].alpha < 1) {
_.circleArray[i].alpha += _.delta;
}
if (_.circleArray[_.circleArray.length - 1].alpha >= 1) {
var newX = _.circleArray[_.circleArray.length - 1].x;
var newY = _.circleArray[_.circleArray.length - 1].y;
var xCenter = false;
// Dont add if we reach to end of totalCount;
if (_.circleArray.length === _.totalCount) {
continue;
}
_.currentDirection = _._options.path[_.pathCrawler].direction;
_.xCenter = _._options.path[_.pathCrawler].xCenter;
_.directionCount = _._options.path[_.pathCrawler].count;
// Determine the direction.
if (_.pathCounter === _.directionCount) {
_.pathCrawler++;
if (_.pathCrawler === _._options.path.length) {
_.pathCrawler = _._options.path.length - 1;
}
// Change Particle Direction.
_.currentDirection = _._options.path[_.pathCrawler].direction;
_.xCenter = _._options.path[_.pathCrawler].xCenter;
_.pathCounter = 0;
}
// Get the direction to use here.
switch (_.currentDirection) {
case 'bottom':
newY = _.circleArray[_.circleArray.length - 1].y + _.gap;
break;
case 'top':
newY = _.circleArray[_.circleArray.length - 1].y - _.gap;
break;
case 'left':
newX = _.circleArray[_.circleArray.length - 1].x - _.gap;
break;
case 'right':
newX = _.circleArray[_.circleArray.length - 1].x + _.gap;
break;
}
if (_.xCenter === true) {
newX = _.canvas.width() / 2;
xCenter = true;
}
_.addCircle(newX, newY, xCenter);
_.pathCounter++;
}
}
};
/**
* Draw Final circle resulat upon resize
*/
CircleFactory.prototype.resizeDrawCircles = function () {
var _ = this;
var pathCounter = 0;
var pathCrawler = 0;
var arcX = _._options.x;
var arcY = _._options.y;
if ( 'center' === arcX ) {
arcX = _.canvas.width() / 2;
}
_.ctx.save();
for (var i = 0; i < _.circleArray.length; i++) {
_.ctx.globalAlpha = 1;
_.ctx.fillStyle = _._options.color;
_.ctx.beginPath();
// Variables
var currentDirection = _._options.path[pathCrawler].direction;
// var xCenter = _._options.path[pathCrawler].xCenter;
var directionCount = _._options.path[pathCrawler].count;
// Determine the direction.
if (pathCounter === directionCount) {
pathCrawler++;
if (pathCrawler === _._options.path.length) {
pathCrawler = _._options.path.length - 1;
}
// Change Particle Direction.
currentDirection = _._options.path[pathCrawler].direction;
// Reset Path Counter.
pathCounter = 0;
}
// Get the direction to use here.
switch (currentDirection) {
case 'bottom':
arcY += _.gap;
break;
case 'top':
arcY -= _.gap;
break;
case 'left':
arcX -= _.gap;
break;
case 'right':
arcX += _.gap;
break;
}
if (true === _.circleArray[i].xcenter) {
arcX = _.canvas.width() / 2;
}
_.ctx.arc(arcX, arcY, _.circleArray[i].radius, 0, Math.PI * 2, true);
_.ctx.fill();
_.ctx.closePath();
pathCounter++;
}
_.ctx.restore();
}
/**
* Draw all the circles within the circleArray to the canvas.
*/
CircleFactory.prototype.drawCircles = function () {
var _ = this;
_.ctx.save();
for (var i = 0; i < _.circleArray.length; i++) {
_.ctx.globalAlpha = _.circleArray[i].alpha;
_.ctx.fillStyle = _._options.color;
_.ctx.beginPath();
_.ctx.arc(_.circleArray[i].x, _.circleArray[i].y, _.circleArray[i].radius, 0, Math.PI * 2, true);
_.ctx.fill();
_.ctx.closePath();
}
_.ctx.restore();
};
CircleFactory.prototype.getTotalCount = function () {
var _ = this;
for (var i = 0; i < _._options.path.length; i++) {
_.totalCount += _._options.path[i].count;
}
};
CircleFactory.prototype.animate = function () {
var _ = this;
var circleX = 0;
if ('center' === _._options.x) {
circleX = $canvas.width() / 2;
_.addCircle(circleX, _._options.y, true);
} else {
_.addCircle(_._options.x, _._options.y);
}
_.getTotalCount();
window.requestAnimationFrame(animateCircles);
};
// Animating function
function animateCircles() {
var _ = element.data('circleFactory');
if (_._options.path.length > 0) {
_.clearCanvas();
_.updateCircles();
_.drawCircles();
if (_.circleArray.length !== _.totalCount || _.circleArray[_.circleArray.length - 1].alpha < 1) {
window.requestAnimationFrame(animateCircles);
}
}
}
/**
* Inserts canvas element to the dom element specified.
*
* @param string $el canvas id inserted in the dom element.
*/
function insertCanvas($el) {
$($el).append('<canvas id="' + $($el).attr('id') + '-canvas" class="section-canvas">Your browser does not support canvas!</canvas>');
return '#' + $($el).attr('id') + '-canvas';
}
if (method) {
var circleFactorys = [];
function getCircleFactory() {
var $el = $(this);
var circleFactory = $el.data('circleFactory');
circleFactorys.push(circleFactory);
}
this.each(getCircleFactory);
var args = (arguments.length > 1) ? Array.prototype.slice.call(arguments, 1) : undefined;
var results = [];
function applyMethod(index) {
var circleFactory = circleFactorys[index];
if (!circleFactory) {
console.warn('$.circleFactory not instantiated yet');
console.info(this);
results.push(undefined);
return;
}
if (typeof circleFactory[method] === 'function') {
var result = circleFactory[method].apply(circleFactory, args);
results.push(result);
} else {
console.warn('Method \'' + method + '\' not defined in $.circleFactory');
}
}
this.each(applyMethod);
return (results.length > 1) ? results : results[0];
} else {
var options = (typeof methodOrOptions === 'object') ? methodOrOptions : undefined;
function init() {
$canvas = insertCanvas($(this));
$canvas = $($canvas);
var circleFactory = new CircleFactory($canvas, options);
circleFactory.resizeCanvas();
$(this).data('circleFactory', circleFactory);
}
return this.each(init);
}
};
})(jQuery);