Advertisement
Guest User

Untitled

a guest
Jan 11th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = (function($){
  2.     var restorePoints = [],
  3.         canvas = $("canvas"),
  4.         drawing = false,
  5.         drawingCanvas = document.getElementById("can"),
  6.         canvasContext = drawingCanvas.getContext("2d"),
  7.         isMobile = true;
  8.  
  9.     canvasContext.strokeStyle = "red";
  10.     canvasContext.lineWidth = 10;
  11.     canvasContext.lineCap = "round";
  12.     canvasContext.fillStyle = "#FFF";
  13.     canvasContext.fillRect(0,0,canvasContext.width, canvasContext.height);
  14.  
  15.     function init(){
  16.         console.log('app has been initialized');
  17.         // prevent elastic scrolling
  18.         document.body.addEventListener('touchmove',function(event){
  19.           event.preventDefault();
  20.         },false);
  21.  
  22.         $("canvas").parents("*").css("overflow", "visible");
  23.  
  24.         $('#color > div').click(function(){
  25.             canvasContext.strokeStyle = $(this).css("background-color");
  26.             window.location.hash = "#";
  27.         });
  28.         $("#penSize").change(function(){
  29.             canvasContext.lineWidth = this.value;
  30.         });
  31.         $("#penColor").change(function(){
  32.             canvasContext.strokeStyle = this.value;
  33.             window.location.hash = "#";
  34.         });
  35.         $("#eraser").click(function(){
  36.             canvasContext.strokeStyle = "#FFF";
  37.         });
  38.         $("#save").click(function(){
  39.             window.location.href= canvasContext.toDataURL();
  40.         });
  41.         $("#clear").click(function(){
  42.             canvasContext.fillStyle = "#FFF";
  43.             canvasContext.fillRect(0,0,drawingCanvas.width,drawingCanvas.height);
  44.             canvasContext.strokeStyle = "red";
  45.             canvasContext.fillStyle = "red";
  46.         });
  47.         canvas.bind(
  48.             (isMobile ? "touchstart" : "mousedown"),
  49.             function( event ){
  50.                 onTouchStart( event );
  51.                 return false;
  52.             }
  53.         );
  54.         $("#undo").click(function(){
  55.             undoDrawOnCanvas();
  56.         });
  57.     }
  58.     function onTouchStart(event){
  59.         console.log('touchstart');
  60.         window.location.hash = "#";
  61.  
  62.         var touch = getTouchEvent( event ),
  63.             localPosition = getCanvasLocalCoordinates(
  64.                 touch.pageX,
  65.                 touch.pageY
  66.             );
  67.  
  68.         saveRestorePoint();
  69.         drawing=true;
  70.         canvasContext.save();
  71.         xPos = localPosition.x;
  72.         yPos = localPosition.y;
  73.  
  74.         if(drawing==true){
  75.             canvasContext.beginPath();
  76.             canvasContext.moveTo(localPosition.x, localPosition.y);
  77.             canvasContext.lineTo(localPosition.x+1,localPosition.y+1);
  78.             canvasContext.stroke();
  79.             canvasContext.closePath();
  80.             xPos = localPosition.x;
  81.             yPos = localPosition.y;
  82.         }
  83.  
  84.         lastPenPoint = {
  85.             x: localPosition.x,
  86.             y: localPosition.y
  87.         };
  88.  
  89.         canvas.bind(
  90.             (isMobile ? "touchmove" : "mousemove"),
  91.             onTouchMove
  92.         );
  93.         canvas.bind(
  94.             (isMobile ? "touchend" : "mouseup"),
  95.             onTouchEnd
  96.         );
  97.     }
  98.     function onTouchMove( event ){
  99.         console.log('touchmove');
  100.         var touch = getTouchEvent( event ),
  101.             localPosition = getCanvasLocalCoordinates(
  102.                 touch.pageX,
  103.                 touch.pageY
  104.             );
  105.  
  106.         lastPenPoint = {
  107.             x: localPosition.x,
  108.             y: localPosition.y
  109.         };
  110.  
  111.         if(drawing==true){
  112.             console.log(xPos, yPos);
  113.             canvasContext.beginPath();
  114.             canvasContext.moveTo(localPosition.x,localPosition.y);
  115.             canvasContext.lineTo(xPos,yPos);
  116.             canvasContext.stroke();
  117.             //canvasContext.closePath();
  118.             xPos = localPosition.x;
  119.             yPos = localPosition.y;
  120.         }
  121.     }
  122.     function onTouchEnd( event ){
  123.         console.log('touchend');
  124.         canvas.unbind(
  125.             (isMobile ? "touchmove" : "mousemove")
  126.         );
  127.         canvas.unbind(
  128.             (isMobile ? "touchend" : "mouseup")
  129.         );
  130.     }
  131.     function undoDrawOnCanvas(){
  132.         if(restorePoints.length > 0){
  133.             var oImg = new Image();
  134.             oImg.onload = function(){
  135.                 var canvasContext = document.getElementById("can").getContext("2d");
  136.                 canvasContext.drawImage(oImg, 0, 0);
  137.             }
  138.             oImg.src = restorePoints.pop();
  139.         }
  140.     }
  141.     function saveRestorePoint(){
  142.         var oCanvas = document.getElementById("can"),
  143.             imgSrc = oCanvas.toDataURL("image/png");
  144.  
  145.         restorePoints.push(imgSrc);
  146.     }
  147.     function getCanvasLocalCoordinates( pageX, pageY ){
  148.         var position = canvas.offset();
  149.  
  150.         return({
  151.             x: (pageX - position.left),
  152.             y: (pageY - position.top)
  153.         });
  154.     }
  155.     function getTouchEvent(ev){
  156.         return(
  157.             isMobile ?
  158.                 event.targetTouches[0] : event
  159.         );
  160.     }
  161.     return{
  162.         init: init,
  163.         onTouchStart: onTouchStart,
  164.     }
  165. }($));
  166.  
  167. document.addEventListener('deviceready', app.init(), false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement