2016-04-16 09:39:07 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<title>vue.js version</title>
|
2016-04-18 13:26:58 +08:00
|
|
|
<script src="https://cdn.jsdelivr.net/stats.js/r11/stats.min.js"></script>
|
2016-04-21 03:53:14 +08:00
|
|
|
<script src="../../dist/vue.min.js"></script>
|
2016-04-16 09:39:07 +08:00
|
|
|
<style>
|
2016-04-18 13:26:58 +08:00
|
|
|
html, body {
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
svg {
|
|
|
|
width: 800px;
|
|
|
|
height: 600px;
|
|
|
|
}
|
2016-04-16 09:39:07 +08:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2016-04-18 13:26:58 +08:00
|
|
|
<h1>Animating 1000 SVG dots</h1>
|
|
|
|
<div id="app">
|
|
|
|
<p>
|
|
|
|
<button @click="toggleOptimization">
|
|
|
|
{{ optimized ? 'disable' : 'enable' }} optimization (Object.freeze)
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
<svg>
|
|
|
|
<circle v-for='point in model.points' :cx='point.x' :cy='point.y' r='2px' fill='#FC309D'></circle>
|
|
|
|
</svg>
|
|
|
|
</div>
|
2016-04-16 09:39:07 +08:00
|
|
|
<script type="text/javascript" charset="utf-8">
|
2016-04-18 13:26:58 +08:00
|
|
|
var stats = new Stats()
|
|
|
|
stats.setMode(0)
|
|
|
|
stats.domElement.style.position = 'absolute'
|
|
|
|
stats.domElement.style.right = '0px'
|
|
|
|
stats.domElement.style.top = '0px'
|
|
|
|
document.body.appendChild(stats.domElement)
|
2016-04-16 09:39:07 +08:00
|
|
|
|
|
|
|
var WIDTH = 800
|
|
|
|
var HEIGHT = 600
|
|
|
|
|
|
|
|
new Vue({
|
|
|
|
el: '#app',
|
2016-04-18 13:26:58 +08:00
|
|
|
data: {
|
|
|
|
model: createModel(1000),
|
|
|
|
optimized: false
|
|
|
|
},
|
2016-04-16 09:39:07 +08:00
|
|
|
created: function () {
|
|
|
|
var self = this
|
2016-04-18 13:26:58 +08:00
|
|
|
requestAnimationFrame(render)
|
2016-04-16 09:39:07 +08:00
|
|
|
stats.begin()
|
|
|
|
function render () {
|
|
|
|
stats.end()
|
|
|
|
stats.begin()
|
2016-04-18 13:26:58 +08:00
|
|
|
requestAnimationFrame(render)
|
|
|
|
self.model.step()
|
|
|
|
if (self.optimized) {
|
|
|
|
self.$forceUpdate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
toggleOptimization: function () {
|
|
|
|
this.model = this.optimized
|
|
|
|
? createModel(1000)
|
|
|
|
: Object.freeze(createModel(1000))
|
|
|
|
this.optimized = !this.optimized
|
2016-04-16 09:39:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function createModel (count) {
|
|
|
|
var points = []
|
|
|
|
for (var i = 0; i < count; ++i) {
|
|
|
|
points.push({
|
|
|
|
x: Math.random() * WIDTH,
|
|
|
|
y: Math.random() * HEIGHT,
|
|
|
|
vx: Math.random() * 4 - 2,
|
|
|
|
vy: Math.random() * 4 - 2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
points: points,
|
|
|
|
step: step
|
|
|
|
}
|
|
|
|
|
|
|
|
function step () {
|
|
|
|
points.forEach(move)
|
|
|
|
}
|
|
|
|
|
|
|
|
function move (p) {
|
|
|
|
if (p.x > WIDTH || p.x < 0) p.vx *= -1
|
|
|
|
if (p.y > HEIGHT || p.y < 0) p.vy *= -1
|
|
|
|
p.y += p.vy
|
|
|
|
p.x += p.vx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|