Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template id="mytemplate">
  2. <content></content><br><canvas width="400" height="300" id="draw"></canvas>
  3.  
  4.  
  5. </template>
  6. <script>
  7. (function() {
  8.     var myPrototype = Object.create(HTMLElement.prototype);
  9.     var myDocument = document.currentScript.ownerDocument;
  10.  
  11.     myPrototype.createdCallback = function() {
  12.         var shadow = this.createShadowRoot();
  13.         var template = myDocument.querySelector("#mytemplate");
  14.         var clone = document.importNode(template.content, true);
  15.         shadow.appendChild(clone);
  16.     }
  17.  
  18.  
  19.  
  20.     var MyElement = document.registerElement('my-draw', {
  21.         prototype: myPrototype
  22.     });
  23.  
  24. }());
  25. </script>
  26. <script>
  27.  
  28.  
  29.     var mtd280 = mtd280 || {};
  30.  
  31.     mtd280.module = (function($) {
  32.  
  33.         // private
  34.         var ctx,
  35.             drawing;
  36.  
  37.         function mDown() {
  38.             var r,g,b;
  39.  
  40.             r = ~~(Math.random() * 255); //~~ = Math.floor()
  41.             g = ~~(Math.random() * 255);
  42.             b = ~~(Math.random() * 255);
  43.             ctx.fillStyle = ctx.strokeStyle = 'rgb('+ [r,g,b].join(',')+')';
  44.  
  45.             drawing = true;
  46.         }
  47.  
  48.         function mUp() {
  49.             drawing = false;
  50.         }
  51.         function mMove(event) {
  52.             if (drawing) {
  53.                 ctx.beginPath();
  54.                 ctx.arc(
  55.                     event.clientX - this.offsetLeft,
  56.                     event.clientY - this.offsetTop,
  57.                     5,0,Math.PI*2,true);
  58.                 ctx.fill();
  59.             }
  60.         }
  61.        
  62.       //------------------//       
  63.         function init() {
  64.       //------------------//
  65.      
  66.             ctx = document.querySelector("my-draw").shadowRoot
  67.         .querySelector("#draw").getContext('2d');
  68.             drawing = false;
  69.            
  70.             ctx.canvas.onmousedown = mDown;
  71.             ctx.canvas.onmouseup = mUp;
  72.             ctx.canvas.onmousemove = mMove;
  73.            
  74.        
  75.            
  76.              //while (true) { console.log("Hello"); }
  77.      
  78.         }; 
  79.    
  80.         document.addEventListener('DOMContentLoaded',init);
  81.    
  82.         // public
  83.         return {};
  84.            
  85.     }($));
  86.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement