Advertisement
Jambix64

scene

Jan 16th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1.  
  2. var canvas = document.getElementById("myCanvas");
  3.  
  4. // styles begin
  5. canvas.style.border = "solid 1px black";
  6. // styles end
  7.  
  8. var ctx = canvas.getContext("2d");
  9.  
  10. console.log(canvas.offsetWidth + "-" + canvas.offsetHeight);
  11.  
  12. if (window.devicePixelRatio > 1) {
  13.  
  14. canvas.width = canvas.clientWidth * 2;
  15. canvas.height = canvas.clientHeight * 2;
  16.  
  17.  
  18. }
  19.  
  20. console.log(canvas.width);
  21. console.log(canvas.height);
  22.  
  23.  
  24. //HEIGHT
  25. ctx.moveTo(canvas.width / 2, 20);
  26.  
  27. ctx.lineTo(canvas.width / 2, canvas.height);
  28.  
  29.  
  30. //WIDTH
  31. ctx.moveTo(20, canvas.height / 2);
  32. ctx.lineTo(canvas.width - 20, canvas.height / 2);
  33.  
  34. /*
  35.  
  36. */
  37. ctx.moveTo(20, canvas.height / 2);
  38. ctx.lineTo(20, 20);
  39.  
  40.  
  41. ctx.moveTo(canvas.width / 2, canvas.height / 2);
  42. ctx.lineTo(canvas.width - 20, 20);
  43.  
  44. ctx.moveTo(canvas.width / 2, canvas.height / 2);
  45. ctx.lineTo(20, canvas.height - 20);
  46.  
  47. ctx.strokeStyle = "red";
  48.  
  49. ctx.stroke();
  50.  
  51. ctx.beginPath();
  52.  
  53. for(var i = 50; i >= 0; i--)
  54. ctx.arc(canvas.width - canvas.width / 3, canvas.height - canvas.height / 3, i, 0, 2 * Math.PI);
  55.  
  56. ctx.stroke();
  57.  
  58. function getMousePosition(canvas, event) {
  59.  
  60. let rect = canvas.getBoundingClientRect();
  61. let x = event.clientX - rect.left;
  62. let y = event.clientY - rect.top;
  63.  
  64. let locX = 0;
  65. let locY = 0;
  66.  
  67. if (x >= canvas.width / 2 && y >= canvas.height / 2) {
  68.  
  69. locX = x;
  70. locY = y;
  71. console.log(" Positivo Coordinate x: " + x, "Coordinate y: " + y);
  72.  
  73. } else{
  74.  
  75. locX = -x;
  76. locY = -y;
  77.  
  78. console.log(" Negativo Coordinate x: " + locX, "Coordinate y: " + locY);
  79. }
  80.  
  81. console.log("Coordinate x: " + x, "Coordinate y: " + y);
  82.  
  83. var location = { posX: x, posY: y }
  84. return location;
  85. }
  86.  
  87. let canvasElem = document.querySelector("canvas");
  88.  
  89. canvasElem.addEventListener("mousemove", function (e) {
  90.  
  91. this.style.cursor = "pointer";
  92. var getPosition = getMousePosition(canvasElem, e);
  93.  
  94. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement