Guest User

Untitled

a guest
Oct 12th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. <!Doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel ="stylesheet" href ="stylesheet/stylesheet.css">
  6. </head>
  7.  
  8. <body>
  9. <h1>
  10. Welcome to the Wheel of Misfortune.
  11. </h1>
  12. <div id="head" class="head">
  13. </div>
  14. <div id="wheel" class="wheel">
  15. </div>
  16. <div id="question" class="question">
  17. </div>
  18.  
  19. <!-- <script src="Javascript/event_listener.js"></script> -->
  20. <script>
  21.  
  22. alert("Load 1")
  23.  
  24. var head = document.getElementById('head')
  25. var question = document.getElementById('question')
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. ///Number of frames per second to draw.
  33. var framesPerSecond = 144
  34.  
  35. //// How quickly the wheel should lose speed.
  36. var deceleration = 0.05
  37.  
  38. //// The number of frames to use when attempting to
  39. /// calculate the average angular velocity of the wheel
  40. // while it is being dragged.
  41. var velocityAverageSpan = 10
  42.  
  43. //
  44. //Wheel State
  45. //
  46.  
  47. // Current angle the wheel is rotated by in degrees.
  48. var currentWheelAngle = 0
  49.  
  50. //set wheel variable to wheel div in index.html
  51. var wheel = document.getElementById('wheel')
  52. var wheelHeight = 0
  53. var wheelWidth = 0
  54.  
  55. //Inject wheel.svg into wheel div html
  56. wheel.innerHTML = '<img id="wheel_image" src="images/wheel_of_fortune.png"></img>';
  57. //
  58. //set wheelImage variable to wheel.svg
  59. var wheelImage = document.getElementById('wheel_image')
  60. //
  61. //SET SURROUNDING DIV DIMENSIONS TO WHEEL SIZER
  62. wheelImage.onload = function(){
  63. wheelImage.setAttribute("style",`width${800}px; height:${800}px`)
  64. var wheelHeight = wheelImage.height
  65. var wheelWidth = wheelImage.width
  66.  
  67. wheel.setAttribute("style",`width:${wheelWidth}px; height:${wheelHeight}px`)
  68.  
  69. var rect = wheel.getBoundingClientRect()
  70.  
  71. console.log(wheelWidth)
  72. console.log(wheelHeight)
  73. wheelCenterX = rect.left + (wheelWidth / 2)
  74. wheelCenterY = rect.top + (wheelHeight / 2)
  75.  
  76. }
  77. //set rect variable to wheel image location
  78.  
  79.  
  80.  
  81.  
  82. // The Center of the wheel
  83.  
  84.  
  85. // A queue of the recent values of the wheel angle.
  86. var angleHistoryQueue = []
  87.  
  88. //Angular Velocity of the wheel (degrees per frame)
  89. var angularVelocity = 0
  90.  
  91.  
  92. /////
  93. //// MOUSE STATE
  94. ///
  95.  
  96.  
  97. /// prevents image being dragged during application usage.
  98. wheelImage.ondragstart = function() { return false; }
  99.  
  100.  
  101.  
  102. // True if the mouse is currently dragging.
  103. var isDragging = false;
  104.  
  105. var lastFrameMouseAngle = 0
  106.  
  107. var mouseX = 0
  108. var mouseY = 0
  109.  
  110. //////////
  111.  
  112. wheelImage.addEventListener("mousemove",function(){
  113. handleMouseLocation(event)
  114. })
  115.  
  116. wheelImage.addEventListener("mousedown",function(){
  117. handleMouseDown(event)
  118. })
  119.  
  120. wheelImage.addEventListener("mouseup",function(){
  121. handleMouseUp(event)
  122. })
  123.  
  124.  
  125. var handleMouseDown = function(event){
  126. isDragging = true
  127. lastFrameMouseAngle = getMouseAngle()
  128. }
  129.  
  130. var handleMouseUp = function(event){
  131. updateAngularVelocity()
  132. isDragging = false
  133. }
  134.  
  135. var handleMouseLocation = function(event){
  136. mouseX = event.pageX
  137. mouseY = event.pageY
  138. }
  139.  
  140. var getMouseAngle = function(){
  141. console.log(Math.atan2(mouseX - wheelCenterX, mouseY - wheelCenterY) * 180 / Math.PI)
  142. console.log(wheelCenterX,wheelCenterY)
  143. console.log(mouseX,mouseY)
  144. return Math.atan2(mouseX - wheelCenterX, mouseY - wheelCenterY) * 180 / Math.PI
  145. }
  146.  
  147. var getAngleDelta = function(angle1,angle2){
  148. return(angle1 - angle2 + 180) % 360 - 180
  149. }
  150.  
  151. var updateAngularVelocity = function(){
  152. if (angleHistoryQueue.length - 1 === 0 ){
  153. angularVelocity = 0
  154. return;
  155. }
  156. var firstAngle = angleHistoryQueue[0]
  157. var lastAngle = angleHistoryQueue[angleHistoryQueue.length - 1]
  158. angularVelocity = (lastAngle - firstAngle) / angleHistoryQueue.length
  159.  
  160. }
  161.  
  162. var drawFrame = function(){
  163. if(isDragging){
  164.  
  165. var mouseAngle = getMouseAngle()
  166.  
  167. var delta = getAngleDelta(lastFrameMouseAngle, mouseAngle);
  168.  
  169. currentWheelAngle += delta;
  170. lastFrameMouseAngle = mouseAngle
  171.  
  172. angleHistoryQueue.push(currentWheelAngle)
  173. if (angleHistoryQueue.length > velocityAverageSpan){
  174. angleHistoryQueue.shift()
  175. }
  176. }
  177. else{
  178. currentWheelAngle += angularVelocity
  179. if( angularVelocity != 0){
  180. var direction = angularVelocity / Math.abs(angularVelocity);
  181. angularVelocity = direction * Math.max(Math.abs(angularVelocity) - deceleration, 0)
  182. }
  183. }
  184. head.innerHTML = `<h1>${currentWheelAngle}</h1>`
  185. wheelImage.style.transform = `rotate(${currentWheelAngle}deg)`
  186. }
  187.  
  188. setInterval(drawFrame, 1000 / framesPerSecond)
  189.  
  190.  
  191.  
  192.  
  193. alert("load 2")
  194.  
  195. </script>
  196.  
  197. </body>
  198. </html>
Advertisement
Add Comment
Please, Sign In to add comment