-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.js
128 lines (93 loc) · 2.15 KB
/
interact.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
;(function() {
var pressed = false;
var t = 0;
var el;
var obj;
var animations = [];
var grow = function(object, dt) {
if (object._remove) {
object._remove = false;
return false;
}
object.radius += dt / 10;
object.el.style.width = object.radius + 'px';
object.el.style.height = object.radius + 'px';
return true;
}
var shrink = function(object, dt) {
if (object._remove) {
object._remove = false;
return false;
}
var keep = true;
object.radius -= dt / 3;
if (object.radius < 0) {
object.radius = 0;
keep = false;
}
object.el.style.width = object.radius + 'px';
object.el.style.height = object.radius + 'px';
return keep;
}
function animate() {
var t1 = Date.now();
var dt = t1 - t;
t = t1;
var _keep = false;
for (var i = 0; i < animations.length; i++) {
var animation = animations[i];
var keep = animation.step(dt);
if (!keep) {
animations.splice(i, 1);
} else {
_keep = true;
}
}
if (_keep) requestAnimationFrame(animate);
console.log(dt);
/*
var min = 100;
el.style.width = (dt + min) + 'px';
el.style.height = (dt + min) + 'px';
*/
}
function addAnimation(data, f) {
var animation = {
step: function(dt) {
return f(data, dt);
}
}
animations.push(animation);
}
function onMouseDown(e) {
e.preventDefault();
pressed = true;
t = Date.now();
el = document.createElement('div');
el.className = 'aura';
var px = e.touches?e.touches[0].pageX:e.pageX;
var py = e.touches?e.touches[0].pageY:e.pageY;
el.style.left = px + 'px';
el.style.top = py + 'px';
document.body.appendChild(el);
obj = {};
obj.el = el;
obj.radius = 100;
addAnimation(obj, grow);
console.log('--- --- ---')
t = Date.now();
animate();
}
function onMouseUp(e) {
pressed = false;
var _obj = obj;
_obj._remove = true;
addAnimation(_obj, shrink);
t = Date.now();
animate();
}
document.addEventListener('mousedown', onMouseDown);
document.addEventListener('mouseup', onMouseUp);
document.addEventListener('touchstart', onMouseDown);
document.addEventListener('touchend', onMouseUp);
})();