Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementsByTagName("canvas")[0];
  2. var context = canvas.getContext("2d");
  3. var height = canvas.height = window.innerHeight;
  4. var width = canvas.width = window.innerWidth;
  5. var mouseClicked = false, mouseReleased = true;
  6. var  x0 = 50;
  7.   y0 = 30;
  8.   bredd =40;
  9.   hojd = 40;
  10.   distance = 40;
  11.   color = "green";
  12.   radie = 10;
  13.   size = 22;
  14.  
  15. document.addEventListener("click", onMouseClick, false);
  16. document.addEventListener("mousemove", onMouseMove, false);
  17. function onMouseClick(e) {
  18.     mouseClicked = !mouseClicked;
  19. }
  20. function onMouseMove(e) {
  21.     if (mouseClicked) {
  22.         context.beginPath();
  23.         context.arc(e.clientX, e.clientY, 7.5, 0, Math.PI * 2, false);
  24.         context.lineWidth = 5;
  25.         context.strokeStyle = color;
  26.         context.fill();
  27.         context.stroke();
  28.         context.fillStyle = color;
  29.     }
  30. //en mouse over-funktion
  31. if ((e.clientX > x0) && (e.clientX < x0 + bredd) && (e.clientY > y0)
  32.       && (e.clientY < y0 + hojd))  { color = "green"; }
  33. }
  34.  
  35. // H�r b?rjar koden inspirerad av spelprogrammering.nu
  36.  
  37.  
  38. function myRectangle(x, y, w, l, color)
  39. {
  40.    context.fillStyle = color
  41.    context.fillRect(x, y, w, l);
  42. }
  43.  
  44. function myText(x, y, size, text, color)
  45. {
  46.   context.font = size + "pt Helvetica";
  47.   context.fillStyle = color;
  48.   context.fillText(text, x, y);
  49. }
  50.  
  51. // rita de f?rgade rutorna
  52.    this.myRectangle(x0, y0, bredd, hojd, "green");
  53.    this.myText(x0-30, y0 + distance-15, 18, "g", "green");
  54.  
  55.  
  56.    function blueColor(){
  57.    this.myRectangle(x0, y0 + distance, bredd, hojd, "blue");
  58.    this.myText(x0-30, y0 + distance+25, 18, "b", "blue");
  59.    }
  60.    function yellowColor(){
  61.    this.myRectangle(x0, y0 + 2 * distance, bredd, hojd, "yellow");
  62.    this.myText(x0-30, y0 + distance+65, 18, "y", "yellow");
  63.    }
  64.    function pinkColor(){
  65.    this.myRectangle(x0, y0 + 3 * distance, bredd, hojd, "pink");
  66.    this.myText(x0-30, y0 + distance+105, 18, "p", "pink");
  67.    }
  68.    function purpleColor(){
  69.    this.myRectangle(x0, y0 + 4 * distance, bredd, hojd, "purple");
  70.    this.myText(x0-30, y0 + distance+145, 18, "k", "purple");
  71.    }
  72.    function whiteColor(){
  73.    this.myRectangle(x0, y0 + 5 * distance, bredd, hojd, "white");
  74.    this.myText(x0-30, y0 + distance+185, 18, "w", "white");
  75.    }
  76.   function Eraser(){
  77.    this.myText(x0-30, y0 + distance+225, 18, "e  Eraser", "white");
  78.    }
  79.  
  80.    this.myText(x0-30, y0 + distance+265, 18, "c  Clear", "white");
  81. // v?lj f?rg att rita med
  82. window.addEventListener('keydown',this.check,false);
  83.  
  84. // function check(e) {
  85. //    alert(e.keyCode);
  86. //}
  87.  
  88. function check(e) {
  89.     var code = e.keyCode
  90. // �ndra f�rg
  91.     if (code == 71)        //Om knappen trycks så ändras färg till en bestämd färg
  92.         color = "green";
  93.     if (code == 66)
  94.         color = "blue";
  95.     if (code == 89)
  96.         color = "yellow";   // Ex. 80 står för knappen "P". If you press "P" color = "pink"
  97.     if (code == 80)
  98.         color = "pink";
  99.     if (code == 75)
  100.         color = "purple";
  101.     if (code == 87)
  102.        color = "white";
  103.     if (code == 69)
  104.        color = "black";
  105. // �ndra radie
  106. if (code == 49)
  107.     radie = 1;
  108. if (code == 50)
  109.     radie = 10;
  110. }
  111.  
  112. //ta bort allt
  113. if (code == 67)
  114.   window.location.reload();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement