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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Animation</title>
<script src="../dist/zrender.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/zrender@4.2.0/dist/zrender.js"></script> -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h2>Move mouse. It is expected that animation start immediately without lag.</h2>
<div>
Circles: <input id="count" value="5000" />
Throttle Rate: <input id="throttle-rate" value="50" />
<button id="ok">OK</button>
</div>
<div id="main" style="width:800px;height:600px;"></div>
<script type="text/javascript">
refresh();
var zr;
var throttleRate;
var count;
function refresh() {
count = +document.getElementById('count').value;
throttleRate = +document.getElementById('throttle-rate').value;
zr && zr.dispose();
init();
}
function init() {
zr = zrender.init(document.getElementById('main'));
var lastTrigger = +new Date();
var timer;
var record = window.record = [];
var recordLimit = 300;
var lastTick = 0;
// zr.animation.on('frame', function () {
// if (record.length > recordLimit) {
// record.length = 0;
// }
// var now = +new Date();
// var gap = now - lastTick;
// record.push(gap < 30 ? '-' : gap);
// lastTick = now;
// }, this);
var gradient = new zrender.LinearGradient();
gradient.addColorStop(0, 'red');
gradient.addColorStop(1, 'black');
var viewWidth = zr.getWidth();
var viewHeight = zr.getHeight();
var center = [viewWidth / 2, viewHeight / 2];
var baseRadius = Math.min(viewWidth, viewHeight) * 0.3;
var els = [];
for (var i = 0; i < count; i++) {
var theta = Math.random() * Math.PI * 2;
var circle = new zrender.Circle({
position: [
center[0] + (baseRadius + Math.random() * 60) * Math.cos(theta),
center[1] + (baseRadius + Math.random() * 60) * Math.sin(theta)
],
shape: {
cx: 0,
cy: 0,
r: 3
},
style: {
fill: 'red',
opacity: 0.2
},
onclick: (function (i) {
return function () {
alert(i);
}
})(i)
});
circle.___ii = i;
els.push(circle);
zr.add(circle);
}
var update = throttle(function (point) {
console.time('animation');
for (var i = 0; i < els.length; i++) {
var el = els[i];
var dist = Math.pow(
Math.pow(point[0] - el.position[0], 2)
+ Math.pow(point[1] - el.position[1], 2),
0.5
);
if (dist < 0.1) {
return;
}
var ratio = (baseRadius + Math.random() * 60) / dist;
var targetPostion = [
point[0] + (el.position[0] - point[0]) * ratio,
point[1] + (el.position[1] - point[1]) * ratio
];
// el.animateTo({
// position: targetPostion
// }, { duration: 3000, easing: 'elasticOut' });
// el.stopAnimation().animate('')
// .all(3000, {
// position: targetPostion
// })
// .start('elasticOut');
el.stopAnimation().animate('')
.when(3000, {
position: targetPostion
})
.start('elasticOut');
}
console.timeEnd('animation');
}, throttleRate);
zr.on('mousemove', function (e) {
update([e.offsetX, e.offsetY]);
});
}
function throttle(fn, delay, debounce) {
var currCall;
var lastCall = 0;
var lastExec = 0;
var timer = null;
var diff;
var scope;
var args;
var debounceNextCall;
delay = delay || 0;
function exec() {
lastExec = (new Date()).getTime();
timer = null;
fn.apply(scope, args || []);
}
var cb = function () {
currCall = (new Date()).getTime();
scope = this;
args = arguments;
var thisDelay = debounceNextCall || delay;
var thisDebounce = debounceNextCall || debounce;
debounceNextCall = null;
diff = currCall - (thisDebounce ? lastCall : lastExec) - thisDelay;
clearTimeout(timer);
if (thisDebounce) {
timer = setTimeout(exec, thisDelay);
}
else {
if (diff >= 0) {
exec();
}
else {
timer = setTimeout(exec, -diff);
}
}
lastCall = currCall;
};
return cb;
}
</script>
</body>
</html>
|