-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoc.gear.html
More file actions
175 lines (144 loc) · 5.33 KB
/
Copy pathpoc.gear.html
File metadata and controls
175 lines (144 loc) · 5.33 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>PoC for gear animation</title>
<link rel="stylesheet" type="text/css" href="turingmachine.css" />
<style type="text/css">
div { float: left; width: 200px; height: 200px; }
img { width: 155px; height: 155px; }
.gear-animation { animation: none; }
</style>
<script type="text/javascript" src="deps/jquery-1.11.3.js"></script>
<script type="text/javascript">
var CountingQueue = function () {
var counter = [];
var inc = function () {
if (counter.length === 0)
counter.push(1);
else if (counter[counter.length - 1] > 0)
counter[counter.length - 1] += 1;
else if (counter[counter.length - 1] < 0)
counter.push(1);
};
var dec = function () {
if (counter.length === 0)
counter.push(-1);
else if (counter[counter.length - 1] > 0)
counter.push(-1);
else if (counter[counter.length - 1] < 0)
counter[counter.length - 1] -= 1;
};
var pop = function () {
return counter.splice(0, 1);
};
var total = function () {
return counter.map(function (v) { return Math.abs(v); })
.reduce(function (a, b) { return a + b; });
};
var isEmpty = function () { return counter.length === 0; }
return {
inc: inc, dec: dec, pop: pop,
total: total, isEmpty: isEmpty
};
};
var GearVisualization = function (queue) {
var currently_running = false;
// Turingmachine API
var addStepsLeft = function (count) {
if (count === undefined)
count = 1;
for (var i = 0; i < count; i++)
queue.dec();
if (!currently_running)
nextAnimation();
};
var addStepsRight = function (count) {
if (count === undefined)
count = 1;
for (var i = 0; i < count; i++)
queue.inc();
if (!currently_running)
nextAnimation();
};
// animation
var computeSpeed = function (total) {
// 1 step = 2s
// 10 steps = 10 * 0.25s
// 20 steps = 20 * 0.25s
if (total <= 1)
return '2s';
else if (total >= 10)
return '250ms';
else {
var val = (-1.0 * total) * (1750.0 / 9) + (1750.0 / 9) + 2000;
return "" + parseInt(val) + "ms";
}
};
var nextAnimation = function () {
if (queue.isEmpty())
return;
var speed = computeSpeed(queue.total());
var steps = queue.pop();
startAnimation({
animationTimingFunction: (Math.abs(steps) > 1) ? "linear" : "ease-in-out",
animationName: "gear-" + (steps < 0 ? "left" : "right"),
animationIterationCount: Math.abs(steps),
animationDuration: speed
});
};
var startAnimation = function (properties) {
var defaultProperties = {
animationName: 'gear-left',
animationDuration: '2s',
animationTimingFunction: 'ease',
animationDelay: '0s',
animationIterationCount: 1,
animationDirection: 'normal',
animationPlayState: 'paused'
};
currently_running = true;
for (var prop in properties)
defaultProperties[prop] = properties[prop];
defaultProperties['animationPlayState'] = 'running';
var oldGear = document.querySelector('.gear-animation');
var oldUid = parseInt(oldGear.getAttribute('data-uid'));
if (isNaN(oldUid))
oldUid = parseInt(Math.random() * Math.pow(2, 32));
var newUid = parseInt(Math.random() * Math.pow(2, 32));
if (newUid === oldUid)
newUid = oldUid + 1;
var newGear = $(oldGear).clone(true).attr("data-uid", newUid);
oldGear.setAttribute("data-uid", oldUid);
$(oldGear).before(newGear);
for (var prop in defaultProperties) {
newGear[0].style[prop] = defaultProperties[prop];
}
$("*[data-uid=" + oldUid + "]").remove();
newGear[0].addEventListener("animationend", function () {
currently_running = false;
nextAnimation();
}, false);
};
return { addStepsLeft: addStepsLeft, addStepsRight: addStepsRight, startAnimation: startAnimation };
};
$(document).ready(function () {
var rotations = new CountingQueue();
var gear = new GearVisualization(rotations);
$("#left1").click(function () { gear.addStepsLeft(1); });
$("#right1").click(function () { gear.addStepsRight(1); });
$("#left_").click(function () { gear.addStepsLeft(5); });
$("#right_").click(function () { gear.addStepsRight(5); });
});
</script>
</head>
<body>
<input type="submit" id="left1" value="1 step left" />
<input type="submit" id="right1" value="1 step right" />
<input type="submit" id="left_" value="scroll left" />
<input type="submit" id="right_" value="scroll right" />
<div class="gear-animation">
<img src="static/gear.svg" alt="Gear #1" />
</div>
</body>
</html>