Advertisement
Guest User

cajigger

a guest
Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Strikk</title>
  6. </head>
  7. <body>
  8. <canvas id="myCanvas" width="600" height="600" style="border:1px solid #d3d3d3;"></canvas>
  9.  
  10. <script type="text/javascript">
  11. /*=========================================================
  12. ==================== DOM ELEMENTS AND GLOBAL VARIABLES ============
  13. =========================================================*/
  14. var cvs = document.getElementById("myCanvas");
  15. var ctx = cvs.getContext("2d");
  16.  
  17. //var mouseState = 0;
  18. var isGrabbed = false;
  19.  
  20. /*=========================================================
  21. ==================== DRAWING =========================
  22. =========================================================*/
  23.  
  24. draw();
  25.  
  26. /*=========================================================
  27. ==================== EVENT LISTENERS =========================
  28. =========================================================*/
  29.  
  30. //Mousedown
  31. cvs.addEventListener("mousedown", function(e) {
  32. //Set mouse state
  33. //mouseState = EMouseState.down;
  34.  
  35. //Get coordinates on canvas
  36. var currX = e.clientX - cvs.offsetLeft;
  37. var currY = e.clientY - cvs.offsetTop;
  38.  
  39. //Check if near central circle
  40. var d = Math.sqrt((currX-300)*(currX-300) + (currY-300)*(currY-300));
  41. if(d<=4){
  42. isGrabbed = true;
  43. }
  44. });
  45.  
  46. //Mouseup
  47. cvs.addEventListener("mouseup", function(e){
  48. isGrabbed = false;
  49. //mouseState = EMouseState.up;
  50. draw();
  51. });
  52.  
  53. //Mousemove
  54. cvs.addEventListener("mousemove", function(e) {
  55. //Get coordinates on canvas
  56. var currX = e.clientX - cvs.offsetLeft;
  57. var currY = e.clientY - cvs.offsetTop;
  58.  
  59. //Check if near central circle
  60. var d = Math.sqrt((currX-300)*(currX-300) + (currY-300)*(currY-300));
  61. if(d<=4){
  62. cvs.style.cursor = "pointer";
  63. } else {
  64. cvs.style.cursor = "auto";
  65. }
  66.  
  67. if(isGrabbed ){
  68. //Call draw function
  69. draw(currX,currY);
  70. }
  71.  
  72. });
  73.  
  74. /*=========================================================
  75. ==================== FUNCTIONS AND OBJECTS ================
  76. =========================================================*/
  77. EMouseState = {
  78. up: 0,
  79. down: 1
  80. };
  81.  
  82. function draw(){
  83. //Clear canvas
  84. ctx.clearRect(0,0,cvs.width, cvs.height);
  85. //Background
  86. ctx.fillStyle = "yellow";
  87. ctx.fillRect(0,0,cvs.width, cvs.height);
  88.  
  89. //Center square
  90. ctx.fillStyle = "red";
  91. ctx.fillRect(150,150,300,300);
  92.  
  93. if(arguments.length === 0){
  94. //Lines to center
  95. ctx.beginPath();
  96. ctx.moveTo(150,300);
  97. ctx.lineTo(300,300);
  98. ctx.stroke();
  99.  
  100. ctx.beginPath();
  101. ctx.moveTo(450,300);
  102. ctx.lineTo(300,300);
  103. ctx.stroke();
  104.  
  105. //Center circle
  106. ctx.beginPath();
  107. ctx.arc(300,300,4,0,2*Math.PI);
  108. ctx.stroke();
  109. } else if (arguments.length === 2){
  110. ctx.beginPath();
  111. ctx.moveTo(150,300);
  112. ctx.lineTo(arguments[0],arguments[1]);
  113. ctx.stroke();
  114.  
  115. ctx.beginPath();
  116. ctx.moveTo(450,300);
  117. ctx.lineTo(arguments[0],arguments[1]);
  118. ctx.stroke();
  119.  
  120. //Center circle
  121. ctx.beginPath();
  122. ctx.arc(arguments[0],arguments[1],4,0,2*Math.PI);
  123. ctx.stroke();
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130. </script>
  131. </body>
  132. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement