-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemoanimations.html
165 lines (153 loc) · 3.78 KB
/
demoanimations.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo Animations for Canvas Shifting</title>
<style>
#canvas {
width: 300px;
height: 200px;
position: relative;
background: darkkhaki;
margin: 1em 3em;
}
#slice {
background: burlywood;
position: absolute;
z-index: 2;
}
#rest {
position: absolute;
background: saddlebrown;
}
#buttons {
margin: 1em 1.5em;
}
button {
font-family: verdana;
background: olive;
color: white;
border: none;
padding: 5px 10px;
}
.animating #buttons {
opacity: .2
}
</style>
</head>
<body>
<div id="canvas">
<div id="slice"></div>
<div id="rest"></div>
</div>
<div id="buttons">
<button id="left">Shift left</button>
<button id="right">Shift right</button>
<button id="up">Shift up</button>
<button id="down">Shift down</button>
</div>
<script>
let buttons = document.querySelector('#buttons');
let slice = document.querySelector('#slice');
let rest = document.querySelector('#rest');
let cssprops = {
x:'left', y:'top', w:'width', h:'height', o:'opacity'
};
let i = 0;
let ani = null;
let delay = 1000;
let animation = {
left:{
setup: [
{element: '#slice', x: 0, y: 0, w: 20, h: 200},
{element: '#rest', x: 20, y: 0, w: 280, h: 200},
],
frames: [
{element: '#slice', x: -15, y: -10, up: 1},
{element: '#rest', x: 0},
{element: '#slice', x: 295, y: -10, up: 1},
{element: '#slice', x: 280, y: 0}
]
},
right:{
setup: [
{element: '#slice', x: 280, y: 0, w: 20, h: 200},
{element: '#rest', x: 0, y: 0, w: 280, h: 200}
],
frames: [
{element: '#slice', x: 295, y: -10, up:1},
{element: '#rest', x: 20, y: 0},
{element: '#slice', x: -15, y: -10, up: 1},
{element: '#slice', x: 0, y: 0}
]
},
up: {
setup: [
{element: '#slice', x: 0, y: 0, w: 300, h: 20},
{element: '#rest', x: 0, y: 20, w: 300, h: 180}
],
frames: [
{element: '#slice', x: 10, y: 5, up:1},
{element: '#rest', x: 0, y: 0},
{element: '#slice', x: 10, y: 175, up:1},
{element: '#slice', x: 0, y: 180},
]
},
down: {
setup: [
{element: '#slice', x: 0, y: 180, w: 300, h: 20},
{element: '#rest', x: 0, y: 0, w: 300, h: 180}
],
frames: [
{element: '#slice', x: 10, y: 175, up:1},
{element: '#rest', x: 0, y: 20},
{element: '#slice', x: 10, y: 5, up:1},
{element: '#slice', x: 0, y: 0},
]
},
};
document.body.addEventListener('click', e => {
if (!document.body.classList.contains('animating')){
let id = e.target.id;
if (id) {
animation[id].setup.forEach(a => {
let elm = document.querySelector(a.element);
elm.style.transition = '';
setprops(elm, a);
});
i = 0;
ani = animation[id].frames;
document.body.className = 'animating';
window.setTimeout(shift, delay);
}
}
});
const setprops = (elm, a) => {
['x','y','w','h'].forEach(p => {
if (a[p] !== undefined) {
elm.style[cssprops[p]] = a[p] + 'px';
}
});
if (a.o !== undefined) {
elm.style.opacity = `${a.o}`;
}
}
const shift = e => {
if (ani[i]) {
let elm = document.querySelector(ani[i].element);
let time = ani[i].time || '1000';
let delay = ani[i].delay ? ' ' + ani[i].delay+'ms' : '';
setprops(elm,ani[i]);
elm.style.boxShadow = (ani[i].up) ? `5px 5px 4px #333` : '';
elm.style.transition = `all ${time}ms${delay}`;
i++;
let recall = +time + (delay ? delay.replace(' ','') : 0);
window.setTimeout(shift, recall)
} else {
document.body.className = '';
}
};
</script>
</body>
</html>