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!
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

Rahul Ramchand
6,218 PointsHow do I paint 2 strokes one over the other?
I wanted to experiment a little bit and I tried calling two functions separately but it didn't seem to work.
The code is here:
//No user interaction causes no change to application
//When the user is interactin cause a change to occur
//Even though the class selected doesn't have a bg color,it also has a class of color with it with a background with it (DO NOT GET CONFUSED)
var color=$(".selected").css("background-color");
var $canvas=$("canvas");
var context=$canvas[0].getContext("2d");
var lastEvent;
var mouseDown=true;
$("#newColor").css("background-color", "black");
$(".controls").on("click", "li", function(){
//Deselect selcted elements
$(this).siblings().removeClass("selected");
//Select the clicked on element
$(this).addClass("selected");
//Change the color after ranges
//This sets the color to the neccesary color chosen
var color=$(this).css("background-color");
});
$("#revealColorSelect").click(function(){
/*if ( $("#colorSelect").css('display') == 'none' ){
//If element is hidden
$("#colorSelect").show();
// element is hidden
}
else{
$("#colorSelect").hide();
}*/
changeColor();
$("#colorSelect").toggle();
});
function changeColor(){
var r=$("#red").val();
var b=$("#blue").val();
var g=$("#green").val();
var c= r+g+b;
$("#newColor").css("background-color", "rgb(" + r + "," + b + "," + g + ")");
}
$("input[type=range]").change(changeColor);
//I need to append an li element with the color in the background of #newcolor
//It also takes the same properties as the classes red blue and yellow
$("#addNewColor").click(function(){
var $newColor=$("<li></li>");
$newColor.css("background-color", $("#newColor").css("background-color"));
$(".controls ul").append($newColor);
});
drawCanvas();
//drawGreen();
function drawCanvas(){$canvas.mousedown(function(e){
color=$(".selected").css("background-color");
lastEvent=e;
mouseDown=true;
}).mousemove(function(e){
if(mouseDown){
context.beginPath();
context.moveTo(lastEvent.offsetX,lastEvent.offsetY);
context.lineWidth=10;
context.lineTo(e.offsetX, e.offsetY);
context.strokeStyle=color;
context.stroke();
lastEvent=e;
}
}).mouseup(function(){
mouseDown=false;
}).mouseleave(function(){
$canvas.mouseup();
});
}
function drawGreen(){$canvas.mousedown(function(e){
color=green;
lastEvent=e;
mouseDown=true;
}).mousemove(function(e){
if(mouseDown){
context.beginPath();
context.moveTo(lastEvent.offsetX,lastEvent.offsetY);
context.lineWidth=2;
context.lineTo(e.offsetX, e.offsetY);
context.strokeStyle=color;
context.stroke();
lastEvent=e;
}
}).mouseup(function(){
mouseDown=false;
}).mouseleave(function(){
$canvas.mouseup();
});
}

Nathan Crum
15,183 PointsHello Rahul,
Could you share a snapshot of your workspace and I would be happy to take a look and assist. If I understand you correctly you would like to draw a line with your first color, then select a second color and draw again on the canvas with both marks being on the canvas at the same time?

Rahul Ramchand
6,218 PointsWould you like to see the rest of the code?
Yes, I was actually trying to figure out how I can paint a laser, by painting two strokes one over the other simultaneously.
@Steven Parker I'm sorry, this might be a stupid question but what do you mean by code quoting?
Edit: Snapshot https://w.trhou.se/d8oi9uo3ej

Steven Parker
225,726 PointsI meant use the proper markdown — it looks like you already fixed it.
But I'm a bit confused about what you're after ... if you draw a second stroke over another, won't the second one just cover up the first at appear to be the only one?

Rahul Ramchand
6,218 PointsIn order to draw a laser I would require a white colour of lesser thickness (slightly transparent) passing over an another color which is blurred and transparent with a larger thickness.
1 Answer

Nathan Crum
15,183 PointsAfter researching a bit, the best answer I could find is here -> Drawing 2 Simultaneous Lines on HTML5 Canvas.
Instead of drawing immediately with the coordinates of the moving mouse you are storing the coordinates in an array and calling a function which draws the array of coordinates and then draws a modified version of the coordinates that you can make to be parallel to what you are drawing or a reflection of etc. The link I mentioned has a working example and the drawing occurs real time with no lag so I don't see any reason why this would not work. You will just need to experiment with how to modify your first set of coordinates to make the second line how you would like it to appear.
Sorry for the delay in getting back to you Rahul.
Steven Parker
225,726 PointsSteven Parker
225,726 PointsProper code quoting would help tremendously. See the Markdown Cheatsheet pop-up below the answer section.