File: animation-additive.html

package info (click to toggle)
node-zrender 5.4.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: javascript: 1,259; makefile: 2
file content (259 lines) | stat: -rw-r--r-- 8,583 bytes parent folder | download | duplicates (2)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Animation</title>
    <script src="../dist/zrender.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #main {
            margin-top: 80px;
            height: 800px;
        }
        .controller {
            position: fixed;
            top: 0;
            left: 0;
            padding: 5px;
            border-bottom: 1px solid #aaa;
            background: #fff;
            z-index: 999;
        }
        #rotate-timer, #scale-timer {
            color: red;
            font-size: 20px;
        }
    </style>
</head>
<body>

    <div class="controller">
        <div id="param">
            param:
            <input value="random" name="param" type="radio"/> random
            <input value="fixed" name="param" type="radio" checked="checked"/> fixed (switch between positive/negative)
        </div>
        <div id="easing">
            easing:
            <input value="cubicInOut" name="easing" type="radio"/> cubicInOut
            <input value="linear" name="easing" type="radio" checked="checked"/> linear
        </div>
        <div>
            <button id="rotate">rotate</button>
            <button id="rotate-and-scale">rotate and scale</button>
            <button id="scale">scale</button>
        </div>
        <div>
            Rotate remain: <span id="rotate-timer">-</span>ms Scale remain: <span id="scale-timer">-</span>ms
        </div>
    </div>
    <div id="main"></div>

    <script type="text/javascript">
        var main = document.getElementById('main');
        // 初始化zrender
        var zr = zrender.init(main);


        const MAX_SCALE_VALUE = 4;
        const MIN_SCALE_VALUE = 1;
        const DURATION = 3000;


        function createHexogon(text, position, scale, isBackbone) {
            const style = isBackbone
                ? {
                    fill: null,
                    stroke: '#000',
                    strokeNoScale: true,
                    lineWidth: 2
                }
                : {
                    fill: 'red',
                    opacity: 0.6
                };
            return new zrender.Isogon({
                position: position.slice(),
                scale: scale.slice(),
                shape: {
                    cx: 0,
                    cy: 0,
                    r: 40,
                    n: 6
                },
                style: style,
                textConfig: {
                    position: 'inside'
                },
                textContent: new zrender.Text({
                    style: {
                        fill: 'yellow',
                        text
                    }
                })
            });
        }

        let position;
        let rotation = 0;
        let scale = [MIN_SCALE_VALUE, MIN_SCALE_VALUE];
        let maxScale = [MAX_SCALE_VALUE, MAX_SCALE_VALUE];
        let rotationSign = 1;
        let scaleSign = 1;


        position = [180, 200];
        zr.add(createHexogon(null, position, scale, true));
        zr.add(createHexogon(null, position, maxScale, true));
        const hexogonAdditive = createHexogon('animationTo\nAdditive', position, scale);
        zr.add(hexogonAdditive);

        position = [530, 200];
        zr.add(createHexogon(null, position, scale, true));
        zr.add(createHexogon(null, position, maxScale, true));
        const hexogonNormal = createHexogon('animationTo\nNormal', position, scale);
        zr.add(hexogonNormal);

        position = [180, 550];
        zr.add(createHexogon(null, position, scale, true));
        zr.add(createHexogon(null, position, maxScale, true));
        const hexogon2Additive = createHexogon('animationFrom\nAdditive', position, scale);
        zr.add(hexogon2Additive);

        position = [530, 550];
        zr.add(createHexogon(null, position, scale, true));
        zr.add(createHexogon(null, position, maxScale, true));
        const hexogon2Normal = createHexogon('animationFrom\nNormal', position, scale);
        zr.add(hexogon2Normal);



        document.getElementById('rotate-and-scale').onclick = function () {
            updateAnimation({updateRotate: true, updateScale: true});
        };
        document.getElementById('rotate').onclick = function () {
            updateAnimation({updateRotate: true});
        };
        document.getElementById('scale').onclick = function () {
            updateAnimation({updateScale: true});
        };

        zr.on('click', function () {
            updateAnimation({updateRotate: true, updateScale: true});
        });

        function updateAnimation({updateRotate, updateScale}) {
            const paramMode = getRadioValue('param');
            const easing = getRadioValue('easing');

            if (paramMode === 'random') {
                if (updateRotate) {
                    const rand = (Math.random() * 3) + 10;
                    rotation += rand * rotationSign;
                }
                if (updateScale) {
                    const scaleRand = Math.random() * 1;
                    scale[0] += scaleRand * scaleSign;
                    scale[1] += scaleRand * scaleSign;
                }
            }
            else {
                if (updateRotate) {
                    rotation = Math.PI * 4 * rotationSign;
                }
                if (updateScale) {
                    scale[0] = scaleSign > 0 ? MAX_SCALE_VALUE : MIN_SCALE_VALUE;
                    scale[1] = scaleSign > 0 ? MAX_SCALE_VALUE : MIN_SCALE_VALUE;
                }
            }

            {
                const props = {};
                if (updateRotate) {
                    rotationSign = -rotationSign;
                    props.rotation = rotation;
                }
                if (updateScale) {
                    scaleSign = -scaleSign;
                    props.scale = scale;
                }
                hexogonAdditive.animateTo(props, {
                    duration: DURATION,
                    easing: easing,
                    additive: true
                });
                hexogonNormal.animateTo(props, {
                    duration: DURATION,
                    easing: easing
                });
            }

            {
                const currProps = {};
                if (updateRotate) {
                    currProps.rotation = hexogon2Additive.rotation;
                    hexogon2Additive.rotation = rotation;
                }
                if (updateScale) {
                    currProps.scale = hexogon2Additive.scale.slice();
                    hexogon2Additive.scale = scale.slice();
                }
                hexogon2Additive.animateFrom(currProps, {
                    duration: DURATION,
                    easing: easing,
                    additive: true
                });
            }

            {
                const currProps = {};
                if (updateRotate) {
                    currProps.rotation = hexogon2Normal.rotation;
                    hexogon2Normal.rotation = rotation;
                }
                if (updateScale) {
                    currProps.scale = hexogon2Normal.scale.slice();
                    hexogon2Normal.scale = scale.slice();
                }
                hexogon2Normal.animateFrom(currProps, {
                    duration: DURATION,
                    easing: easing
                });
            }

            updateRotate && resetTimer('rotate-timer');
            updateScale && resetTimer('scale-timer');
        }

        function getRadioValue(containerId) {
            const containerDom = document.getElementById(containerId);
            const radios = containerDom.getElementsByTagName('input');
            for (radioDom of radios) {
                if (radioDom.checked) {
                    return radioDom.getAttribute('value');
                }
            }
        }

        function resetTimer(containerId) {
            const timerDom = document.getElementById(containerId);
            let start = Date.now();
            function next() {
                let remain = DURATION - (Date.now() - start);
                timerDom.innerHTML = Math.max(0, remain);
                if (remain > 0) {
                    requestAnimationFrame(next);
                }
            }
            next();
        }

    </script>
</body>
</html>