-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimator.js
More file actions
43 lines (36 loc) · 996 Bytes
/
animator.js
File metadata and controls
43 lines (36 loc) · 996 Bytes
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
function Animator(myId,myRate){
//properties
this.target = document.getElementById(myId);
this.isMoving = false;
//overloading
if(myRate == null || typeof myRate == "string"){
this.framerate = 1;
}else{
this.framerate = myRate;
}
//start the animation
this.start = function(){
var animator = this;
this.timer = setInterval(function(){
//you have to do the math on it before you add the px, otherwise your just adding on a string
animator.target.style.left = (parseInt(animator.target.style.left)+2) + "px";
},this.framerate * 1000);
this.isMoving = true;
};
//stop the animation
this.stop = function(){
clearInterval(this.timer);
this.isMoving = false;
};
this.toggle = function(){
if(this.isMoving){
this.stop();
}else{
this.start();
}
};
}
//this must be called as a string in the first param of setInterval -> "enterframe()"
// function enterFrame(){
// animator.target.style.left = (parseInt(animator.target.style.left)+2) + "px";
// }