Skip to content

Commit

Permalink
[docs] Add Touch Support to the whiteboard example (#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadirlaskar authored and darrachequesne committed Nov 20, 2018
1 parent a7fbd1a commit d3c653d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions examples/whiteboard/public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
canvas.addEventListener('mouseup', onMouseUp, false);
canvas.addEventListener('mouseout', onMouseUp, false);
canvas.addEventListener('mousemove', throttle(onMouseMove, 10), false);

//Touch support for mobile devices
canvas.addEventListener('touchstart', onMouseDown, false);
canvas.addEventListener('touchend', onMouseUp, false);
canvas.addEventListener('touchcancel', onMouseUp, false);
canvas.addEventListener('touchmove', throttle(onMouseMove, 10), false);

for (var i = 0; i < colors.length; i++){
colors[i].addEventListener('click', onColorUpdate, false);
Expand Down Expand Up @@ -51,21 +57,21 @@

function onMouseDown(e){
drawing = true;
current.x = e.clientX;
current.y = e.clientY;
current.x = e.clientX||e.touches[0].clientX;
current.y = e.clientY||e.touches[0].clientY;
}

function onMouseUp(e){
if (!drawing) { return; }
drawing = false;
drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true);
drawLine(current.x, current.y, e.clientX||e.touches[0].clientX, e.clientY||e.touches[0].clientY, current.color, true);
}

function onMouseMove(e){
if (!drawing) { return; }
drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true);
current.x = e.clientX;
current.y = e.clientY;
drawLine(current.x, current.y, e.clientX||e.touches[0].clientX, e.clientY||e.touches[0].clientY, current.color, true);
current.x = e.clientX||e.touches[0].clientX;
current.y = e.clientY||e.touches[0].clientY;
}

function onColorUpdate(e){
Expand Down

0 comments on commit d3c653d

Please sign in to comment.