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

Lijie Zhou
Lijie Zhou
4,498 Points

Why the canvas pen doesn't show in the browser?

I finished the JQuery canvas project and it works perfectly fine in the workspace. But when I put it in my code editor and open in the browser, it has some issues:

1) In Firefox, the pen doesn't show up. 2) In Chrome, the pen becomes a cross but it could draw.

I checked the Css3 canvas selector can be used in most of the browsers, then what is this problem?

2 Answers

Sreng Hong
Sreng Hong
15,083 Points

Hi Lijie!!! I've downloaded the project into my computer. It's working fine with Chrome and Safari, but in Firefox the pen show up but I can't draw anything. You can post your code here. I'll help you look through it. Hope this help.

Lijie Zhou
Lijie Zhou
4,498 Points

Hi Sreng, thanks for your reply. Here's my code:

//Defining the global variables

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 elements $(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 //Draw lines

$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(); });

Sreng Hong
Sreng Hong
15,083 Points

Hi Lijie, your welcome.

I just realized that you don't include <li></li> in the $("") of the variable $newColor.

So, the right code should be like this and it will be working find.

//Append the color to the controls ul
var $newColor = $("<li></li>");

By the way, it still doesn't work in Firefox and I don't really know the reason, but with Chrome or Safari it's working smoothy. Hope this help.