Advertisement
Senselessness

Testpaint

Apr 25th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8">
  5.     <title>Paint</title>
  6.     <style type="text/css">
  7.       #content { position: relative; }
  8.       #cvs { border: 1px solid #c00; }
  9.       #cvsTmp { position: absolute; top: 1px; left: 1px; }
  10.     </style>
  11.   </head>
  12.   <body>
  13.     <p>
  14.        
  15.        
  16. <label>
  17. Object type:
  18.     <select id="drawTool">
  19.         <option value="line">Linje</option>
  20.         <option value="pencil">Blyant</option>
  21.         <option value="rect">Rektangel</option>
  22.         <option value="circle">Sirkel</option>
  23.         <option value="oval">Oval</option>
  24.         <option value="polygon">Polygon</option>
  25.     </select>
  26.    
  27. Shape drawn:
  28.     <select id="shapeDrawn">
  29.         <option value=""></option>
  30.     </select>
  31.    
  32.    
  33. <button id="deleteLastAction">Slett</button>
  34. </label>     
  35.      
  36.       </p>
  37.       <div id="content">
  38.           <canvas id="cvs" width="1024" height="512"></canvas>
  39.       </div>
  40.      
  41.       <script type="text/javascript">
  42.  
  43. if(window.addEventListener) {
  44. window.addEventListener('load', function () {
  45.   var canvas;
  46.   var context;
  47.   var canvasTmp;
  48.   var contextTmp;
  49.   var tool;
  50.   var tool_default = 'line';
  51.  
  52.   function init () {
  53.     // Find the canvas element.
  54.     canvasTmp = document.getElementById('cvs');
  55.     if (!canvasTmp) {
  56.       alert('Error: I cannot find the canvas element!');
  57.       return;
  58.     }
  59.  
  60.     if (!canvasTmp.getContext) {
  61.       alert('Error: no canvas.getContext!');
  62.       return;
  63.     }
  64.  
  65.     contextTmp = canvasTmp.getContext('2d');
  66.     if (!contextTmp) {
  67.       alert('Error: failed to getContext!');
  68.       return;
  69.     }
  70.  
  71.     var content = canvasTmp.parentNode;
  72.     canvas = document.createElement('canvas');
  73.     if (!canvas) {
  74.       alert('Error: I cannot create a new canvas element!');
  75.       return;
  76.     }
  77.  
  78.     canvas.id     = 'cvsTmp';
  79.     canvas.width  = canvasTmp.width;
  80.     canvas.height = canvasTmp.height;
  81.     content.appendChild(canvas);
  82.  
  83.     context = canvas.getContext('2d');
  84.  
  85.     var tool_select = document.getElementById('drawTool');
  86.     if (!tool_select) {
  87.       alert('Error: failed to get the drawTool element!');
  88.       return;
  89.     }
  90.     tool_select.addEventListener('change', ev_tool_change, false);
  91.  
  92.     if (tools[tool_default]) {
  93.       tool = new tools[tool_default]();
  94.       tool_select.value = tool_default;
  95.     }
  96.  
  97.     canvas.addEventListener('mousedown', ev_canvas, false);
  98.     canvas.addEventListener('mousemove', ev_canvas, false);
  99.     canvas.addEventListener('mouseup',   ev_canvas, false);
  100.   }
  101.  
  102.   function ev_canvas (ev) {
  103.     if (ev.layerX || ev.layerX == 0) { // Firefox
  104.       ev._x = ev.layerX;
  105.       ev._y = ev.layerY;
  106.     }
  107.  
  108.     var func = tool[ev.type];
  109.     if (func) {
  110.       func(ev);
  111.     }
  112.   }
  113.  
  114.   function ev_tool_change (ev) {
  115.     if (tools[this.value]) {
  116.       tool = new tools[this.value]();
  117.     }
  118.   }
  119.  
  120.   function img_update () {
  121.         contextTmp.drawImage(canvas, 0, 0);
  122.         context.clearRect(0, 0, canvas.width, canvas.height);
  123.   }
  124.  
  125.   var tools = {};
  126.  
  127.   // The drawing pencil.
  128.   tools.pencil = function () {
  129.     var tool = this;
  130.     this.started = false;
  131.  
  132.     this.mousedown = function (ev) {
  133.         context.beginPath();
  134.         context.moveTo(ev._x, ev._y);
  135.         tool.started = true;
  136.     };
  137.  
  138.     this.mousemove = function (ev) {
  139.       if (tool.started) {
  140.         context.lineTo(ev._x, ev._y);
  141.         context.stroke();
  142.       }
  143.     };
  144.  
  145.     this.mouseup = function (ev) {
  146.       if (tool.started) {
  147.         tool.mousemove(ev);
  148.         tool.started = false;
  149.         img_update();
  150.       }
  151.     };
  152.   };
  153.  
  154.   // The rectangle tool.
  155.   tools.rect = function () {
  156.     var tool = this;
  157.     this.started = false;
  158.  
  159.     this.mousedown = function (ev) {
  160.       tool.started = true;
  161.       tool.x0 = ev._x;
  162.       tool.y0 = ev._y;
  163.     };
  164.  
  165.     this.mousemove = function (ev) {
  166.       if (!tool.started) {
  167.         return;
  168.       }
  169.  
  170.       var x = Math.min(ev._x,  tool.x0),
  171.           y = Math.min(ev._y,  tool.y0),
  172.           w = Math.abs(ev._x - tool.x0),
  173.           h = Math.abs(ev._y - tool.y0);
  174.  
  175.       context.clearRect(0, 0, canvas.width, canvas.height);
  176.  
  177.       if (!w || !h) {
  178.         return;
  179.       }
  180.       context.fillRect(x, y, w, h);
  181.         context.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)';
  182.     };
  183.  
  184.     this.mouseup = function (ev) {
  185.       if (tool.started) {
  186.         tool.mousemove(ev);
  187.         tool.started = false;
  188.         img_update();
  189.       }
  190.     };
  191.   };
  192.  
  193.   // The line tool.
  194.   tools.line = function () {
  195.     var tool = this;
  196.     this.started = false;
  197.  
  198.     this.mousedown = function (ev) {
  199.       tool.started = true;
  200.       tool.x0 = ev._x;
  201.       tool.y0 = ev._y;
  202.     };
  203.  
  204.     this.mousemove = function (ev) {
  205.       if (!tool.started) {
  206.         return;
  207.       }
  208.  
  209.       context.clearRect(0, 0, canvas.width, canvas.height);
  210.  
  211.       context.beginPath();
  212.       context.moveTo(tool.x0, tool.y0);
  213.       context.lineTo(ev._x,   ev._y);
  214.       context.stroke();
  215.       context.closePath();
  216.     };
  217.  
  218.     this.mouseup = function (ev) {
  219.       if (tool.started) {
  220.         tool.mousemove(ev);
  221.         tool.started = false;
  222.         img_update();
  223.       }
  224.     };
  225.   };
  226.    
  227.   // The circle tool.
  228.   tools.circle = function () {
  229.     var tool = this;
  230.     this.started = false;
  231.  
  232.     this.mousedown = function (ev) {
  233.       tool.started = true;
  234.       tool.x0 = ev._x;
  235.       tool.y0 = ev._y;
  236.     };
  237.  
  238.     this.mousemove = function (ev) {
  239.       if (!tool.started) {
  240.         return;
  241.       }
  242.  
  243. context.clearRect(0, 0, canvas.width, canvas.height);
  244.  
  245.     var x = (mouse.x + start_mouse.x) / 2;
  246.     var y = (mouse.y + start_mouse.y) / 2;
  247.  
  248.     var radius = Math.max(
  249.         Math.abs(mouse.x - start_mouse.x),
  250.         Math.abs(mouse.y - start_mouse.y)
  251.     ) / 2;
  252.  
  253.     context.beginPath();
  254.     context.arc(x, y, radius, 0, Math.PI*2, false);
  255.     // context.arc(x, y, 5, 0, Math.PI*2, false);
  256.     context.stroke();
  257.     context.closePath();
  258.  
  259. };
  260.  
  261.     this.mouseup = function (ev) {
  262.       if (tool.started) {
  263.         tool.mousemove(ev);
  264.         tool.started = false;
  265.         img_update();
  266.       }
  267.     };
  268.   };
  269.    
  270.    
  271.  
  272.   init();
  273.  
  274. }, false); }
  275.  
  276.       </script>
  277.   </body>
  278. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement