forked from binarymax/guilloche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimate.js
65 lines (55 loc) · 1.78 KB
/
animate.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
//------------------------------------------------------------------
//Animation Frame Polyfill
window.requestAnimFrame = (function(){ return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
}
);})();
//------------------------------------------------------------------
var Animate = (function(){
var Animator = function(id,width,height,settings) {
var self = this;
this.img = Pixels(id,width||Guilloche.width,height||Guilloche.height,settings.classic);
this.can = document.getElementById(id);
this.settings = (settings instanceof Array) ? settings:[settings];
this.on = false;
this.render();
this.loop();
this.can.onclick = function(e){ self.toggle(); };
};
Animator.prototype.render = function() {
var self = this;
self.img.clean();
self.settings = self.settings.map(function(settings){
settings.fn(self.img, settings.R, settings.r, settings.p, settings.step, settings.zoom);
if(settings.Rs) {
settings.R += settings.Rs;
if(settings.R<=settings.Rmin || settings.R>=settings.Rmax) settings.Rs*=-1;
}
if(settings.rs) {
settings.r += settings.rs;
if(settings.r<=settings.rmin || settings.r>=settings.rmax) settings.rs*=-1;
}
if(settings.ps) {
settings.p += settings.ps;
if(settings.p<=settings.pmin || settings.p>=settings.rmax) settings.ps*=-1;
}
return settings;
});
self.img.render();
};
Animator.prototype.loop = function() {
var self = this;
requestAnimFrame(function(){self.loop();});
if (self.on) self.render();
};
Animator.prototype.toggle = function(){
this.on = !this.on;
};
return function(id,width,height,settings) {
return new Animator(id,width,height,settings);
}
})();