Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. var c=document.getElementById("myCanvas");
  2. var ctx=c.getContext("2d");
  3. ctx.rect(20,20,150,100);
  4. ctx.stroke();
  5.  
  6. $('c.[rectangle]').hover(function(this){
  7. this.fillStyle = 'red';
  8. this.fill();
  9. });
  10.  
  11. <!DOCTYPE html>
  12. <html>
  13. <body>
  14.  
  15. <canvas id="myCanvas" width="700" height="500" style="border:1px solid #c3c3c3;">
  16. Your browser does not support the HTML5 canvas tag.
  17. </canvas>
  18.  
  19. <script>
  20. var myRect={x:150, y:75, w:50, h:50, color:"red"};
  21. var c = document.getElementById("myCanvas");
  22. var ctx = c.getContext("2d");
  23. ctx.fillStyle = myRect.color;
  24. ctx.fillRect(myRect.x, myRect.y, myRect.w, myRect.h);
  25.  
  26. c.addEventListener("mousemove", function(e){
  27. if ((e.clientX>=myRect.x)&(e.clientX<=myRect.x+myRect.w)&(e.clientY>=myRect.y)&(e.clientY<=myRect.y+myRect.h)){
  28. myRect.color = "green";}
  29. else{
  30. myRect.color = "red";}
  31. updateCanvas();
  32. }, false);
  33.  
  34.  
  35. function updateCanvas(){
  36. ctx.fillStyle = myRect.color;
  37. ctx.fillRect(myRect.x, myRect.y, myRect.w, myRect.h);
  38. }
  39. </script>
  40.  
  41. </body>
  42. </html>
  43.  
  44. var canvas = document.getElementById('canvas0');
  45. canvas.addEventListener('mouseover', function() { /*your code*/ }, false);
  46.  
  47. var c=document.getElementById("myCanvas");
  48. var ctx=c.getContext("2d");
  49. ctx.rect(20,20,150,100);
  50. ctx.stroke();
  51.  
  52. c.addEventListener("mouseover", doMouseOver, false);//added event to canvas
  53.  
  54. function doMouseOver(e){
  55. ctx.fillStyle = 'red';
  56. ctx.fill();
  57. }
  58.  
  59. const canvas = document.querySelector('canvas');
  60. const ctx = canvas.getContext('2d');
  61. const width = canvas.width = window.innerWidth;
  62. const height = canvas.height = window.innerHeight;
  63. const cx = width / 2;
  64. const cy = height / 2;
  65. const twoPie = Math.PI * 2;
  66. const points = []; // This will be the array we store our hover points in later
  67.  
  68. class Point {
  69. constructor(x, y, r) {
  70. this.x = x;
  71. this.y = y;
  72. this.r = r || 0;
  73. }
  74. }
  75.  
  76. class HoverPoint extends Point {
  77. constructor(x, y, r, color, hoverColor) {
  78. super(x, y, r);
  79. this.color = color;
  80. this.hoverColor = hoverColor;
  81. this.hovered = false;
  82. this.path = new Path2D();
  83. }
  84.  
  85. draw() {
  86. this.hovered ? ctx.fillStyle = this.hoverColor : ctx.fillStyle = this.color;
  87. this.path.arc(this.x, this.y, this.r, 0, twoPie);
  88. ctx.fill(this.path);
  89. }
  90. }
  91.  
  92. class Cursor extends Point {
  93. constructor(x, y, r) {
  94. super(x, y, r);
  95. }
  96.  
  97. collisionCheck(points) {
  98. // This is the method that will be called during the animate function that
  99. // will check the cursors position against each of our objects in the points array.
  100. document.body.style.cursor = "default";
  101. points.forEach(point => {
  102. point.hovered = false;
  103. if (ctx.isPointInPath(point.path, this.x, this.y)) {
  104. document.body.style.cursor = "pointer";
  105. point.hovered = true;
  106. }
  107. });
  108. }
  109. }
  110.  
  111. function createPoints() {
  112. // Create your points and add them to the points array.
  113. points.push(new HoverPoint(cx, cy, 100, 'red', 'coral'));
  114. points.push(new HoverPoint(cx + 250, cy - 100, 50, 'teal', 'skyBlue'));
  115. // ....
  116. }
  117.  
  118. function update() {
  119. ctx.clearRect(0, 0, width, height);
  120. points.forEach(point => point.draw());
  121. }
  122.  
  123. function animate(e) {
  124. const cursor = new Cursor(e.offsetX, e.offsetY);
  125. update();
  126. cursor.collisionCheck(points);
  127. }
  128.  
  129. createPoints();
  130. update();
  131. canvas.onmousemove = animate;
  132.  
  133. class Cursor extends Point {
  134. constructor(x, y, r) {
  135. super(x, y, r);
  136. }
  137.  
  138. collisionCheck(points) {
  139. document.body.style.cursor = "default";
  140. points.forEach(point => {
  141. let dx = point.x - this.x;
  142. let dy = point.y - this.y;
  143. let distance = Math.hypot(dx, dy);
  144. let dr = point.r + this.r;
  145.  
  146. point.hovered = false;
  147. // If the distance between the two objects is less then their combined radius
  148. // then they must be touching.
  149. if (distance < dr) {
  150. document.body.style.cursor = "pointer";
  151. point.hovered = true;
  152. }
  153. });
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement