-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.html
100 lines (89 loc) · 2.83 KB
/
animation.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type="text/css" id="style">
#root {
width: 52px;
height: 52px;
overflow: hidden;
white-space: nowrap;
}
#root div {
vertical-align: top;
width: 50px;
line-height: 50px;
height: 50px;
text-align: center;
font-size: 20px;
font-weight: bold;
border: 1px solid;
display: inline-block;
}
#root div:first-child {
animation-name: slide;
animation-fill-mode: none;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-duration: 1s;
}
@keyframes slide {}
</style>
</head>
<body>
<div id="root"></div>
<script type="application/javascript">
main();
function main() {
const root = document.getElementById('root');
const style = document.getElementById('style');
const fps = getFpsFromQuery() || 30;
injectFrames(root, fps);
injectKeyFrames(style, fps);
}
function getFpsFromQuery() {
const params = new URLSearchParams(location.search);
const fps = params.get('fps');
return fps ? parseInt(fps, 10) : undefined;
}
function injectFrames(container, frames) {
let current = frames;
while(current--) {
container.appendChild(createFrameElement(frames - current));
}
}
function createFrameElement(number) {
const el = document.createElement('div');
el.innerHTML = number;
return el;
}
function injectKeyFrames(style, fps) {
style.innerHTML = style.innerHTML.replace('@keyframes slide {}', `@keyframes slide {${generateKeyFramesContent(fps)}`);
}
function generateKeyFramesContent(fps) {
const step = (100 / fps).toFixed(5) * 1;
let content = '';
for (let i = 0; i < fps; i++) {
content += createKeyFrame(
i * step + (i === 0 ? 0 : 0.00001),
i * -52,
i > 0
);
content += createKeyFrame(
i + 1 === fps ? 100 : (i + 1) * step,
i * -52,
true
);
}
return content;
}
function createKeyFrame(percent, marginValue, gap) {
let content = '';
content += (gap ? ' ' : '') + percent + '% {';
content += 'margin-left: ' + marginValue + 'px';
content += '}';
return content;
}
</script>
</body>
</html>