Advertisement
Guest User

webpaint.js

a guest
Sep 27th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Drawing area
  2. var wbCanvas;
  3. var wbContext;
  4. var left;
  5. var top;
  6. // variables for managing drawing
  7. var started = false;
  8. var dtool = "pencil";
  9. var dcolor = "#000000";
  10. var refX = null;
  11. var refY = null;
  12. var points = [];
  13. // variables for managing text typing
  14. var text = null;
  15. var typing=false;
  16. var textX = null;
  17. var textY = null;
  18.  
  19. function initCanvas() {
  20.     if (!Modernizr.canvas) {
  21.         return
  22.     } else {
  23.         wbCanvas = document.getElementById("canvas");
  24.         var rect = wbCanvas.getBoundingClientRect();
  25.         left = rect.left;
  26.         top = rect.top;
  27.         console.log("left "+left);
  28.         console.log("top "+top);
  29.         console.log("width "+wbCanvas.width);
  30.         console.log("height="+wbCanvas.height);
  31.         wbContext = wbCanvas.getContext("2d");
  32.         wbContext.strokeStyle = "#000000";
  33.         wbContext.strokeRect(0, 0, wbCanvas.width, wbCanvas.height);
  34.        
  35.         wbCanvas.addEventListener('mousedown', mouseEventHandler, false);
  36.         wbCanvas.addEventListener('mousemove', mouseEventHandler, false);
  37.         wbCanvas.addEventListener('mouseup',   mouseEventHandler, false);
  38.         document.addEventListener('mousedown', function (event) {
  39.             var point = mouseCoordinates(event);
  40.             var x=point.x, y=point.y;
  41.             if(x<left || x>left+wbCanvas.width){// && y<top+wbCanvas.height) {
  42.                 saveText();
  43.                 typing = false;        
  44.                 textX = null;
  45.                 textY = null;
  46.             }
  47.         }, false);
  48.     }
  49. }
  50.  
  51. function getSelectElementValue(id) {
  52.     sElmt = document.getElementById(id);   
  53.     return sElmt.options[sElmt.selectedIndex].value;
  54. }
  55.  
  56. /** Event handlers */
  57.  
  58. // Called when user type a key from its keyboard
  59. function keyPressHandler(event) {      
  60.     if (typing == true) {
  61.         var chCode = ('charCode' in event) ? event.charCode : event.keyCode;
  62.         reDraw();
  63.         text = text + String.fromCharCode(chCode);
  64.         typeText(textX, textY, dcolor, text);      
  65.     }  
  66. }
  67.  
  68. // Called when user select a drawing color or a drawing tool
  69. function selectChangeHandler() {
  70.     refX = null;
  71.     refY = null;
  72.     dtool = getSelectElementValue("dtool");
  73.     dcolor = "#"+getSelectElementValue("dcolor");
  74.     // stop typing if it was the previous activity 
  75.     if(dtool != "text" && typing==true) {
  76.         typing = false;
  77.         addDrawing({tool:"text", x:textX, y:textY, color:dcolor, text:text});
  78.         text = "";
  79.     }
  80. }
  81.  
  82. // returns the coordinates (x, y) of a mouse after click event
  83. function mouseCoordinates(event) {
  84.     var x, y;
  85.     // Get the mouse position relative to the canvas element
  86.     if(event.layerX || event.layerX == 0) { //Firefox
  87.         x = event.layerX;
  88.         y = event.layerY;
  89.     } else if (event.offsetX || event.offsetX == 0) { //Opera
  90.         x = event.offsetX;
  91.         y = event.offsetY;
  92.     }
  93.     return {x:x, y:y};
  94. }
  95.  
  96. function saveText() {
  97.     if(typing==true && text!="") {
  98.         addDrawing({tool:"text", x:textX, y:textY, color:dcolor, text:text});                  
  99.     }
  100. }
  101.  
  102. // Called when a mouse event occurs: mouse down/move/up
  103. function mouseEventHandler(event) {
  104.     // Get the mouse position relative to the canvas element
  105.     var point = mouseCoordinates(event);
  106.     var x=point.x, y=point.y;
  107.        
  108.     // Call the event handler of the tool
  109.     if (event.type == "mousedown") {
  110.         // Called when you start holding down the mouse button, it starts the pencil drawing       
  111.         started = true;
  112.         refX = x;
  113.         refY = y;
  114.        
  115.         if (dtool == "pencil") {
  116.             wbContext.beginPath();
  117.             wbContext.moveTo(x, y);
  118.             addDrawing({tool:dtool, begin:true, color:dcolor, x:x, y:y});
  119.  
  120.         }else if(dtool == "text") {        
  121.             saveText();
  122.             typing=true;
  123.             text = "";                     
  124.             textX = x;
  125.             textY = y; 
  126.            
  127.         }
  128.  
  129.     } else if (event.type == "mousemove") {
  130.         // Every time you move the mouse. It draws only if the tool.started state is set to true (when you are holding down the mouse).
  131.         if (started) {         
  132.             wbContext.strokeStyle = dcolor;
  133.             if (dtool == "pencil") {
  134.                 wbContext.lineTo(x, y);
  135.                 wbContext.stroke();            
  136.                 addDrawing({tool:dtool, begin: false, color:dcolor, x:x, y:y});
  137.  
  138.             }else if (dtool == "rect" && refX != null && refY != null) {
  139.                 reDraw();
  140.                 leftCornerX = refX;
  141.                 leftCornerY = refY;
  142.                 width  = Math.abs(refX - x);
  143.                 height = Math.abs(refY - y);               
  144.                 if (refX > x)
  145.                     leftCornerX = x;
  146.                 if (refY > y)
  147.                     leftCornerY = y;               
  148.                 if(document.getElementById("fill").checked == true) {
  149.                     wbContext.fillStyle = dcolor;
  150.                     wbContext.fillRect(leftCornerX, leftCornerY, width, height);
  151.                 }else
  152.                     wbContext.strokeRect(leftCornerX, leftCornerY, width, height);
  153.             }else if (dtool == "circle" && refX != null && refY != null) {         
  154.                 reDraw();
  155.                 var filled = document.getElementById("fill").checked;
  156.                 var radius = circleRadius(refX, refY, x, y);
  157.                 drawCircle(refX, refY, radius, filled, dcolor);
  158.             }          
  159.         }
  160.  
  161.     } else if(event.type == "mouseup") { // When the mouse is releazed
  162.         if (started) {
  163.             if(dtool == "pencil") {
  164.                 wbContext.lineTo(x, y);
  165.                 wbContext.stroke();
  166.  
  167.             }else if(dtool == "rect") {
  168.                 reDraw();
  169.                 var corner = rectCorner(x, y);
  170.                 var filled = document.getElementById("fill").checked;
  171.                 if(filled == true) {
  172.                     wbContext.fillStyle = dcolor;
  173.                     wbContext.fillRect(corner.x, corner.y, width, height);
  174.                 }else {
  175.                     wbContext.strokeStyle = dcolor;
  176.                     wbContext.strokeRect(corner.x, corner.y, width, height);
  177.                 }
  178.                 addDrawing({tool:dtool, filled:filled, color:dcolor, x:corner.x, y:corner.y, width: width, height:height});
  179.  
  180.             }else if(dtool == "circle") {
  181.                 reDraw();
  182.                 var filled = document.getElementById("fill").checked;
  183.                 var radius = circleRadius(refX, refY, x, y);
  184.                 drawCircle(refX, refY, radius, filled, dcolor);
  185.                 addDrawing({tool:dtool, filled:filled, color:dcolor, x:refX, y:refY, radius:radius});
  186.             }
  187.            
  188.             started = false;
  189.         }  
  190.     }
  191.    
  192. }
  193.  
  194. /** Drawing functions */
  195. function typeText(x, y, color, text) {
  196.     wbContext.fillStyle = color;
  197.     wbContext.font = '30px_sans';
  198.     wbContext.textBaseline = 'top';
  199.     wbContext.fillText (text, x, y);
  200. }
  201.  
  202. function circleRadius(centerX, centerY, x, y) {
  203.     var width  = Math.abs(centerX - x);
  204.     var height = Math.abs(centerY - y);            
  205.     var radius = Math.sqrt(width*width+height*height);
  206.     return radius;
  207. }
  208.  
  209. function drawCircle(centerX, centerY, radius, filled, color) { 
  210.     wbContext.beginPath();
  211.     if(filled == true) {
  212.         wbContext.fillStyle = color;
  213.         wbContext.arc(centerX,centerY,radius,0,2*Math.PI);
  214.         wbContext.fill();
  215.     }else {
  216.         wbContext.strokeStyle = color;
  217.         wbContext.arc(centerX,centerY,radius,0,2*Math.PI);
  218.         wbContext.stroke();
  219.     }  
  220. }
  221.  
  222. function rectCorner(x, y) {
  223.     leftCornerX = refX;
  224.     leftCornerY = refY;
  225.     width  = Math.abs(refX - x);
  226.     height = Math.abs(refY - y);               
  227.     if (refX > x)
  228.         leftCornerX = x;
  229.     if (refY > y)
  230.         leftCornerY = y;
  231.     return {x: leftCornerX, y: leftCornerY};
  232. }
  233.  
  234. // add a drawing point (line/rectangle/circle/text) to the list
  235. function addDrawing(point) {
  236.     points.push(point);
  237. }
  238.  
  239. // Draw a point on the canvas
  240. function displayDrawing(point) {
  241.     if (point.tool == "pencil") {
  242.         if (point.begin == true) {
  243.             wbContext.beginPath();
  244.             wbContext.moveTo(point.x, point.y);
  245.         }else {
  246.             wbContext.strokeStyle = point.color;
  247.             wbContext.lineTo(point.x, point.y);
  248.             wbContext.stroke();
  249.         }
  250.  
  251.     }else if(point.tool == "rect") {
  252.         if(point.filled == true) {
  253.             wbContext.fillStyle = point.color;
  254.             wbContext.fillRect(point.x, point.y, point.width, point.height);
  255.         }else {
  256.             wbContext.strokeStyle = point.color;
  257.             wbContext.strokeRect(point.x, point.y, point.width, point.height);
  258.         }
  259.     }else if(point.tool == "circle") {
  260.         drawCircle(point.x, point.y, point.radius, point.filled, point.color)
  261.  
  262.     }else if (point.tool == "text") {
  263.         typeText(point.x, point.y, point.color, point.text);
  264.     }
  265. }
  266.  
  267. function reDraw() {
  268.     wbContext.fillStyle = "#FFFFFF";
  269.     wbContext.fillRect(0, 0, wbCanvas.width, wbCanvas.height); 
  270.     wbContext.strokeStyle = "#000000";
  271.     wbContext.strokeRect(0, 0, wbCanvas.width, wbCanvas.height);
  272.     for(var i=0; i<points.length; i++) {
  273.         var point = points[i];     
  274.         displayDrawing(point);
  275.     }
  276. }
  277.  
  278. // clear (reset) the drawing area
  279. function clear() {
  280.     points = [];
  281.     reDraw();
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement