Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.63 KB | None | 0 0
  1. <script>
  2.     (function() {
  3.         let myPrototype = Object.create(HTMLElement.prototype);
  4.         let myDocument = document.currentScript.ownerDocument;
  5.  
  6.         myPrototype.createdCallback = function() {
  7.             let shadow = this.createShadowRoot();
  8.  
  9.             let canvas = document.createElement("CANVAS");
  10.             canvas.setAttribute("width", "400");
  11.             canvas.setAttribute("height", "300");
  12.             let ctx = canvas.getContext('2d');
  13.             let drawing = false;
  14.  
  15.             shadow.appendChild(canvas);
  16.  
  17.             function mDown() {
  18.                 let r,g,b;
  19.                 r = Math.floor(Math.random() * 255);
  20.                 g = Math.floor(Math.random() * 255);
  21.                 b = Math.floor(Math.random() * 255);
  22.                 ctx.fillStyle = ctx.strokeStyle = 'rgb('+ [r,g,b].join(',') +')';
  23.                 drawing = true;
  24.             }
  25.  
  26.             function mUp() {
  27.                 drawing = false;
  28.             }
  29.  
  30.             function mMove(event) {
  31.                 if (drawing) {
  32.                     ctx.beginPath();
  33.                     ctx.arc(
  34.                         event.clientX - this.offsetLeft,
  35.                         event.clientY - this.offsetTop,
  36.                         5, 0, Math.PI*2, true
  37.                     );
  38.                     ctx.fill();
  39.                 }
  40.             }
  41.  
  42.             ctx.canvas.onmousedown = mDown;
  43.             ctx.canvas.onmouseup = mUp;
  44.             ctx.canvas.onmousemove = mMove;
  45.         };
  46.  
  47.         let MyElement = document.registerElement('drawing-canvas', {
  48.             prototype: myPrototype
  49.         });
  50.  
  51.     } () );
  52. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement