Advertisement
conmac7

canvas_mouse__touchevents

Mar 24th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById('paint');
  2. var ctx = canvas.getContext('2d');
  3.  
  4. var sketch = document.getElementById('sketch');
  5. var sketch_style = getComputedStyle(sketch);
  6.  
  7. /*canvas.width = 500;
  8. canvas.height = 250;*/
  9.  
  10. // variable to track where the mouse is
  11. var mouse = {x: 0, y: 0};
  12.  
  13. // Variable to track where the touch position
  14. // is on the canvas
  15. var touch = {x: 0, y:0};
  16.  
  17.  
  18.  
  19. // Drawing on Paint App
  20. ctx.lineJoin = 'round';
  21. ctx.lineCap = 'round';
  22. ctx.strokeStyle = "black";
  23.  
  24. function getColor(colour){
  25.  
  26.     ctx.strokeStyle = colour;
  27. }
  28.  
  29. function getSize(size){
  30.    
  31.     ctx.lineWidth = size;
  32. }
  33.  
  34.  
  35.  
  36. // Get the touch position relative to the top-left of the canvas
  37. // When we get the raw values of pageX and pageY below, they take into account the scrolling on the page
  38. // but not the position relative to our target div. We'll adjust them using "target.offsetLeft" and
  39. // "target.offsetTop" to get the correct values in relation to the top left of the canvas.
  40. function getTouchPos(e) {
  41.    
  42.    if (!e)
  43.      var e = event;
  44.  
  45.     if(e.touches) {
  46.        if (e.touches.length == 1) {
  47.             // Only deal with one finger
  48.            
  49.             // Get the information for finger #1
  50.             var touchFig = e.touches[0];
  51.             touch.x = touchFig.pageX - touchFig.target.offsetLeft;
  52.             touch.y = touchFig.pageY - touchFig.target.offsetTop;
  53.        }
  54.      }
  55. }
  56.  
  57.  
  58. // Draw something when a touch start is detected
  59. function sketchpad_touchStart() {
  60.    
  61.         // Update the touch x,y co-ordinates
  62.         getTouchPos();
  63.  
  64.             //draw
  65.           ctx.beginPath();
  66.           ctx.moveTo(touch.x, touch.y);
  67.           ctx.lineTo(touch.x, touch.y);
  68.           ctx.stroke();
  69.  
  70.         // Prevents an additional mousedown event being triggered
  71.         event.preventDefault();
  72. }
  73.  
  74.  
  75. // Draw something and prevent the default scrolling when touch movement is detected
  76. function sketchpad_touchMove(e) {
  77.  
  78.         // Update the touch x,y co-ordinates
  79.         getTouchPos(e);
  80.  
  81.         // During a touchmove event, unlike a mousemove event,
  82.         // we don't need to check if the touch is engaged, since there
  83.         // will always be contact with the screen by definition.
  84.         ctx.beginPath();
  85.           ctx.moveTo(touch.x, touch.y);
  86.           ctx.lineTo(touch.x, touch.y);
  87.           ctx.stroke();
  88.  
  89.         // Prevent a scrolling action as a result of this touchmove triggering.
  90.         event.preventDefault();
  91. }
  92.  
  93.  
  94. //Event listeners for touch moves
  95. // =================================
  96.  
  97. canvas.addEventListener('touchstart', sketchpad_touchStart, false);
  98. canvas.addEventListener('touchmove', sketchpad_touchMove, false);
  99.  
  100.  
  101.  
  102. // Event listeners for mouse moves
  103. // =================================
  104.  
  105. //event listener to calculate the position of the mouse tip
  106. canvas.addEventListener('mousemove', function(e) {
  107.     mouse.x = e.pageX - this.offsetLeft;
  108.     mouse.y = e.pageY - this.offsetTop;
  109.     }, false);
  110.  
  111.  
  112. //event listener that listens to the event of holding
  113. //the mouse down
  114. canvas.addEventListener('mousedown', function(e) {
  115.    
  116.     ctx.beginPath();
  117.     ctx.moveTo(mouse.x, mouse.y);
  118.     canvas.addEventListener('mousemove', onPaint, false);
  119.    
  120.      }, false);
  121.  
  122. canvas.addEventListener('mouseup', function() {
  123.     canvas.removeEventListener('mousemove', onPaint, false);
  124. }, false);
  125.  
  126. var onPaint = function() {
  127.     ctx.lineTo(mouse.x, mouse.y);
  128.     ctx.stroke();
  129. };
  130.  
  131.  
  132. var nPadLines = 12;
  133.  
  134. function createPadLines(nlines){
  135.  
  136.     console.log("Creating Pad lines");
  137.     var c=document.getElementById("paint");
  138.     var ctx=c.getContext("2d");
  139.     var yHeight = 40;
  140.  
  141.     for(var line=0; line<nlines; ++line){
  142.  
  143.         ctx.beginPath();
  144.         ctx.moveTo(0,line*yHeight);
  145.         ctx.lineTo(900,line*yHeight);
  146.         ctx.stroke();
  147.     }
  148. }
  149.  
  150. /*create the pad lines*/
  151. createPadLines(nPadLines);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement