Advertisement
Guest User

Paint

a guest
Apr 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Innlogging</title>
  6. <style>
  7. #tegneflate {
  8. border-style: solid;
  9. }
  10.  
  11. #farge {
  12. width: 10px;
  13. height: 10px;
  14. display: inline-block;
  15. background-color: black;
  16. }
  17. </style>
  18. <script>
  19. window.onload = oppstart;
  20.  
  21. var ctx;
  22. var canvasTop = 0;
  23. var canvasLeft = 0;
  24. var taster = { R: 82, G:71, B: 66, S: 83};
  25.  
  26. function oppstart() {
  27. ctx = document.getElementById("tegneflate").getContext("2d");
  28. document.getElementById("tegneflate").onmousedown = musNed;
  29. document.getElementById("tegneflate").onmouseup = musOpp;
  30. document.getElementById("tegneflate").onselectstart = function () { return false; }
  31. document.onkeyup = tastSluppet;
  32.  
  33. var rect = document.getElementById("tegneflate").getBoundingClientRect();
  34. canvasTop = rect.top;
  35. canvasLeft = rect.left;
  36. }
  37.  
  38. function musNed(evt) {
  39. document.getElementById("tegneflate").onmousemove = musFlytt;
  40. ctx.beginPath();
  41. ctx.moveTo(evt.clientX - canvasLeft, evt.clientY - canvasTop);
  42. }
  43.  
  44. function musOpp(evt) {
  45. document.getElementById("tegneflate").onmousemove = null;
  46. }
  47.  
  48. function musFlytt(evt) {
  49. ctx.lineTo(evt.clientX - canvasLeft, evt.clientY - canvasTop);
  50. ctx.stroke();
  51. }
  52.  
  53. function tastSluppet(evt) {
  54. if (evt.keyCode === taster.R) {
  55. ctx.strokeStyle = "red";
  56. }
  57. else if (evt.keyCode === taster.G) {
  58. ctx.strokeStyle = "green";
  59. }
  60. else if (evt.keyCode === taster.B) {
  61. ctx.strokeStyle = "blue";
  62. }
  63. else if (evt.keycode === taster.S) {
  64. ctx.strokeStyle = "black";
  65. }
  66.  
  67. document.getElementById("farge").style.backgroundColor = ctx.strokeStyle;
  68. }
  69. </script>
  70. </head>
  71. <body>
  72. <canvas id="tegneflate" width="500" height="500"></canvas><br />
  73. Farge: <span id="farge"></span> (R=Rød, G=Grønn, B=Blå, S=Sort)
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement