Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. <canvas id="cnv" width="800" height="800"></canvas>
  2.  
  3. <style>
  4. body {
  5. margin: 0px auto;
  6. }
  7.  
  8. </style>
  9.  
  10. <script>
  11. 'use strict'
  12.  
  13. void function () {
  14. //Config
  15. const frameRate = 25
  16. const backgroundColor = 'black'
  17. const wireframeColor = 'green'
  18. const size = 20
  19. let dots = [
  20. [-100, -100, -100, -100],
  21. [-100, -100, -100, 100],
  22. [-100, -100, 100, -100],
  23. [-100, -100, 100, 100],
  24. [-100, 100, -100, -100],
  25. [-100, 100, -100, 100],
  26. [-100, 100, 100, -100],
  27. [-100, 100, 100, 100],
  28. [100, -100, -100, -100],
  29. [100, -100, -100, 100],
  30. [100, -100, 100, -100],
  31. [100, -100, 100, 100],
  32. [100, 100, -100, -100],
  33. [100, 100, -100, 100],
  34. [100, 100, 100, -100],
  35. [100, 100, 100, 100]
  36. ]
  37.  
  38.  
  39.  
  40. const cnv = document.getElementById('cnv')
  41. const ctx = cnv.getContext('2d')
  42. let timestamp = new Date().getTime()
  43. const frameStep = Math.round(1000 / frameRate)
  44. let offsetX = Math.round(cnv.width / 2)
  45. let offsetY = Math.round(cnv.height / 2)
  46. function init() {
  47.  
  48.  
  49. function renderFrame() {
  50. ctx.fillStyle = backgroundColor
  51. ctx.fillRect(0, 0, cnv.width, cnv.height)
  52. ctx.closePath()
  53. dots.forEach( dot => {
  54. let renderSize = (size + dot[3] / 10)
  55. let minX = Math.round(offsetX + dot[0]) + dot[2]
  56. let minY = Math.round(offsetY + dot[1]) + dot[2]
  57.  
  58.  
  59. ctx.fillStyle = wireframeColor
  60. ctx.strokeStyle = wireframeColor
  61. ctx.beginPath()
  62. ctx.arc(minX,minY,renderSize,0,2*Math.PI)
  63. ctx.fill()
  64. //ctx.fillRect(minX, minY, renderSize, renderSize)
  65. ctx.closePath()
  66. })
  67. dots.forEach( (dot, index, arr) => {
  68. let renderSize = (size + dot[3] / 10)
  69. let minX = Math.round(offsetX + dot[0]) + dot[2]
  70. let minY = Math.round(offsetY + dot[1]) + dot[2]
  71.  
  72. ctx.beginPath()
  73. ctx.strokeStyle = wireframeColor
  74. ctx.moveTo(minX, minY)
  75. ctx.lineTo(minX - dot[0], minY)
  76. ctx.stroke()
  77. ctx.closePath()
  78.  
  79. ctx.beginPath()
  80. ctx.strokeStyle = wireframeColor
  81. ctx.moveTo(minX, minY)
  82. ctx.lineTo(minX, minY - dot[1])
  83. ctx.stroke()
  84. ctx.closePath()
  85.  
  86. ctx.beginPath()
  87. ctx.strokeStyle = wireframeColor
  88. ctx.moveTo(minX, minY)
  89. ctx.lineTo(minX - dot[2], minY - dot[2])
  90. ctx.stroke()
  91. ctx.closePath()
  92.  
  93. })
  94.  
  95. //dots = dots.map( dot => rotatePoint2(dot, 1) )
  96. dots = dots.map( dot => rotatePoint(dot, 1) )
  97. }
  98.  
  99. function playAnimation() {
  100.  
  101.  
  102. if (timestamp + frameStep < new Date().getTime()) {
  103. timestamp = new Date().getTime()
  104. renderFrame()
  105. }
  106. requestAnimationFrame(playAnimation)
  107. }
  108. requestAnimationFrame(playAnimation)
  109. }
  110. function rotatePoint(dot, angle, axis) {
  111. let cx = size/2
  112. let cy = size/2
  113. let x = dot[0]
  114. let y = dot[1]
  115. let z = dot[2]
  116. let q = dot[3]
  117. let rotOne = y
  118. let rotTwo = q
  119. let radians = (Math.PI / 180) * angle,
  120. cos = Math.cos(radians),
  121. sin = Math.sin(radians)
  122. rotOne = (cos * (rotOne - cx)) + (sin * (rotTwo - cy)) + cx
  123. rotTwo = (cos * (rotTwo - cy)) - (sin * (rotOne - cx)) + cy
  124. return [x, rotOne, z, rotTwo]
  125. }
  126. function rotatePoint2(dot, angle, axis) {
  127. let cx = size/2
  128. let cy = size/2
  129. let x = dot[0]
  130. let y = dot[1]
  131. let z = dot[2]
  132. let q = dot[3]
  133. let rotOne = q
  134. let rotTwo = x
  135. let radians = (Math.PI / 180) * angle,
  136. cos = Math.cos(radians),
  137. sin = Math.sin(radians)
  138. rotOne = (cos * (rotOne - cx)) + (sin * (rotTwo - cy)) + cx
  139. rotTwo = (cos * (rotTwo - cy)) - (sin * (rotOne - cx)) + cy
  140. return [rotTwo, y, z, rotOne]
  141. }
  142.  
  143.  
  144. document.addEventListener('DOMContentLoaded', event => {
  145. cnv.width = window.innerWidth
  146. cnv.height = window.innerHeight
  147. offsetX = Math.round(cnv.width / 2)
  148. offsetY = Math.round(cnv.height / 2)
  149. init()
  150. })
  151.  
  152. window.onresize = event => {
  153. cnv.width = window.innerWidth
  154. cnv.height = window.innerHeight
  155. offsetX = Math.round(cnv.width / 2)
  156. offsetY = Math.round(cnv.height / 2)
  157. }
  158.  
  159. }()
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement