Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Simple Drawing App for mobile devices

Hey guys I finished the Making a Simple drawing App using JQuery and it works great on desktop devices but the canvas does not recognize touch input on say a mobile device.

The action handlers they use in the app are mousedown, mouseup, mousemove etc events but on a mobile device they do nothing. I did some research and I saw a touch() method in the api but my initial attempt of swapping the mouse() methods with touch() methods was unsuccessful. Anyone tried this before?

Here is the code for app.js

 var color = $(".selected").css("background-color"); var $canvas = $("canvas"); var context = $canvas[0].getContext("2d"); var lastEvent; var mouseDown = false;

//when clicking on control list items $(".controls").on("click", "li", function(){ //deselect sibling items $(this).siblings().removeClass("selected"); //select clicked element $(this).addClass("selected"); //cache current color color = $(this).css("background-color"); });

//when "new color" is pressed $("#revealColorSelect").click(function(){ //show color select or hide the color select changeColor(); $("#colorSelect").toggle(); });

//update the new color span function changeColor() { var r = $("#red").val(); var g = $("#green").val(); var b = $("#blue").val();

$("#newColor").css("background-color", "rgb(" + r + "," + g + "," + b + ")"); }

//when color sliders change $("input[type=range]").change(changeColor);

//when "add color" is pressed $("#addNewColor").click(function(){ //append the color to the controls ul var $newColor = $("<li></li>"); $newColor.css("background-color", $("#newColor").css("background-color")); $(".controls ul").append($newColor); // select the new color $newColor.click(); });

//On mouse events on the canvas $canvas.mousedown(function(e){ lastEvent = e; mouseDown = true; }).mousemove(function(e){ //draw lines if (mouseDown){
context.beginPath(); context.moveTo(lastEvent.offsetX, lastEvent.offsetY); context.lineTo(e.offsetX, e.offsetY); context.strokeStyle = color; context.stroke(); lastEvent = e; } }).mouseup(function(){ mouseDown = false; }).mouseleave(function(){ $canvas.mouseup(); });