-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
31 lines (25 loc) · 770 Bytes
/
example.js
File metadata and controls
31 lines (25 loc) · 770 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
var def = document.querySelector('#default');
var lerp = document.querySelector('#lerp');
var direction = 1; // -1 for left, 1 for right
var speed = 1;
var updateRate = 5;
var timeToChangeDirection = 1500;
var moveInDirection = function() {
var defPosition = def.getAttribute('position');
var lerpPosition = lerp.getAttribute('position');
var x = defPosition.x;
var newX = x + direction * speed;
defPosition.x = newX;
lerpPosition.x = newX;
def.setAttribute('position', defPosition);
lerp.setAttribute('position', lerpPosition);
};
var changeDirection = function() {
if (direction > 0) {
direction = -1;
} else {
direction = 1;
}
};
setInterval(moveInDirection, 1000 / updateRate);
setInterval(changeDirection, timeToChangeDirection);