Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 5

Niall Maher
Niall Maher
16,985 Points

My simple drawing app simply won't work HELP PLEASE :P

Here's my code and basically won't draw on to the canvas so I can't debug it any further after that. I ran in the console and got lists of errors but too many to even pin point what the immediate problem is

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

$('.controls').on("click", "li", function(){
  $(this).siblings().removeClass('selected');
  $(this).addClass('selected');
  color = $(this).css('background-color');
});
$('#revealColorSelect').click(function(){
  changeColor();
  $('#colorSelect').toggle();
});

function changeColor(){
  var r = $('#red').val();
  var g = $('#green').val();
  var b = $('#blue').val();
  $('#newColor').css('background-color', "rgb(" + r + "," + g + "," + b + ")");
}
$('input[type=range]').change(changeColor);
//when add color is pressed
$("#addNewColor").click(function(){
var $newColor = $("<li></li>");
  $newColor.css("background-color", $('#newColor').css('background-color'));
 $(".controls ul").append($newColor); 
 $newColor.click() ;
});


$canvas.mousedown(function(e) {
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e){ 
//draw lines
if (mouseDown) = true {  
ctx.beginPath();
ctx.moveTo(lastEvent.offsetX, lastEvent.offsetY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.strokeStyle = color;
  ctx.stroke();
lastEvent = e;
}
}).mouseup(function(){
mouseDown = false
});

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,240 Points

Hi, Niall. It looks like you have an error in your if statement near the end. I'm not sure if it will fix the entire thing, but try fixing this and see what happens:

//draw lines
if (mouseDown) = true {

should be this:

//draw lines
if (mouseDown == true) {  //even simpler form would be just (mouseDown)
Niall Maher
Niall Maher
16,985 Points

Thanks a million for the quick reply! I changed that and still the initial problem of nothing happening on the canvas :/

Ryan Field
Ryan Field
Courses Plus Student 21,240 Points

No problem! I'm just working a bit now, but I'll take a look at your code in a bit (if someone else hasn't gotten to it by then or you haven't figured it out). :)

Niall Maher
Niall Maher
16,985 Points

Would really appreciate it! Thanks pal :)

Alec Lajoie
Alec Lajoie
11,400 Points

Hi Niall,

I think Ryan was on the right track, in your IF statement though I don't think you want to be setting mousedown to true as you just set it above with

$canvas.mousedown(function(e) {
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e){ 

and then the mousemove function is invoked, so I don't think you need to set mousedown to true. So instead I think you want this

$canvas.mousedown(function(e) {
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e){ 
//draw lines
if (mouseDown) { 

Pretty sure that will get the application running for you. Best of luck!

Niall Maher
Niall Maher
16,985 Points

Yeah that was definitely an error but it's just the way i was shown in the tutorial.

I am starting to think it's the computer because still nothing is happening. I'll download the finished code when I'm on my personal computer and check them against each other.