index.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>vue.js version</title>
  5. <script src="https://cdn.jsdelivr.net/stats.js/r11/stats.min.js"></script>
  6. <script src="../../dist/vue.min.js"></script>
  7. <style>
  8. html, body {
  9. height: 100%;
  10. width: 100%;
  11. padding: 0;
  12. margin: 0;
  13. }
  14. svg {
  15. width: 800px;
  16. height: 600px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>Animating 1000 SVG dots</h1>
  22. <div id="app">
  23. <p>
  24. <button @click="toggleOptimization">
  25. {{ optimized ? 'disable' : 'enable' }} optimization (Object.freeze)
  26. </button>
  27. </p>
  28. <svg>
  29. <circle v-for='point in model.points' :cx='point.x' :cy='point.y' r='2px' fill='#FC309D'></circle>
  30. </svg>
  31. </div>
  32. <script type="text/javascript" charset="utf-8">
  33. var stats = new Stats()
  34. stats.setMode(0)
  35. stats.domElement.style.position = 'absolute'
  36. stats.domElement.style.right = '0px'
  37. stats.domElement.style.top = '0px'
  38. document.body.appendChild(stats.domElement)
  39. var WIDTH = 800
  40. var HEIGHT = 600
  41. new Vue({
  42. el: '#app',
  43. data: {
  44. model: createModel(1000),
  45. optimized: false
  46. },
  47. created: function () {
  48. var self = this
  49. requestAnimationFrame(render)
  50. stats.begin()
  51. function render () {
  52. stats.end()
  53. stats.begin()
  54. requestAnimationFrame(render)
  55. self.model.step()
  56. if (self.optimized) {
  57. self.$forceUpdate()
  58. }
  59. }
  60. },
  61. methods: {
  62. toggleOptimization: function () {
  63. this.model = this.optimized
  64. ? createModel(1000)
  65. : Object.freeze(createModel(1000))
  66. this.optimized = !this.optimized
  67. }
  68. }
  69. });
  70. function createModel (count) {
  71. var points = []
  72. for (var i = 0; i < count; ++i) {
  73. points.push({
  74. x: Math.random() * WIDTH,
  75. y: Math.random() * HEIGHT,
  76. vx: Math.random() * 4 - 2,
  77. vy: Math.random() * 4 - 2
  78. })
  79. }
  80. return {
  81. points: points,
  82. step: step
  83. }
  84. function step () {
  85. points.forEach(move)
  86. }
  87. function move (p) {
  88. if (p.x > WIDTH || p.x < 0) p.vx *= -1
  89. if (p.y > HEIGHT || p.y < 0) p.vy *= -1
  90. p.y += p.vy
  91. p.x += p.vx
  92. }
  93. }
  94. </script>
  95. </body>
  96. </html>