Advertisement
teknogeek

Snow Draw

Dec 21st, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript: var DrawLine = function () {
  2.  
  3.     var context, offsetLeft, offsetTop;
  4.  
  5.     function _drawCircle(mouseX, mouseY) {
  6.         var x = mouseX - offsetLeft,
  7.             y = mouseY - offsetTop;
  8.         context.beginPath();
  9.         context.arc(x, y, 15, 0, Math.PI * 2);
  10.         context.closePath();
  11.         context.fill();
  12.     }
  13.  
  14.     function _setLineStyle() {
  15.         context.strokeStyle = "#df4b26";
  16.         context.lineJoin = "round";
  17.         context.lineWidth = 20;
  18.     }
  19.  
  20.     return {
  21.         init: function (canvas) {
  22.             var isPainting = false;
  23.  
  24.             context = canvas.getContext("2d"),
  25.             offsetLeft = canvas.offsetLeft,
  26.             offsetTop = canvas.offsetTop;
  27.  
  28.             var isMobile = +("ontouchstart" in window);
  29.  
  30.             var events = {
  31.                 start: ["mousedown", "touchstart"],
  32.                 end: ["mouseup", "touchend"],
  33.                 move: ["mousemove", "touchmove"]
  34.             };
  35.  
  36.             canvas.addEventListener(events.start[isMobile], function (e) {
  37.                 isPainting = true;
  38.                 e = isMobile ? e.targetTouches[0] : e;
  39.                 _drawCircle(e.pageX, e.pageY);
  40.             }, false);
  41.             canvas.addEventListener(events.end[isMobile], function (e) {
  42.                 isPainting = false;
  43.             }, false);
  44.             canvas.addEventListener(events.move[isMobile], function (e) {
  45.                 if (isPainting) {
  46.                     e = isMobile ? e.targetTouches[0] : e;
  47.                     _drawCircle(e.pageX, e.pageY);
  48.                 }
  49.             }, false);
  50.         }
  51.     }
  52. }();
  53. ! function () {
  54.     var canvas = document.createElement("canvas");
  55.     canvas.width = window.outerWidth;
  56.     canvas.height = window.outerHeight;
  57.     canvas.style.position = "absolute";
  58.     canvas.style.top = "0px";
  59.     canvas.style.left = "0px";
  60.     document.body.appendChild(canvas);
  61.  
  62.     var context = canvas.getContext("2d");
  63.  
  64.     context.globalAlpha = 0.8;
  65.     context.fillStyle = "rgb(255, 255, 255)";
  66.     context.fillRect(0, 0, canvas.width, canvas.height);
  67.  
  68.     context.globalCompositeOperation = "destination-out";
  69.  
  70.     DrawLine.init(canvas);
  71. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement