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
Bartek Matuszewski
5,300 Pointsadding new drawing functions
.Have any of you got an idea how to organize code to switch off different drawing functions? I'd like to develop app and add at least two drawing functions (for example drawing a rectangle). I've implemented it, but i can't find a way to switch off first function (drawing) when a second function is selected(rectangle), so app is drawing line and rectangular at the same time.
3 Answers
Jake Lundberg
13,965 PointsSo it looks like all your functionality is placed in anonymous functions within the event handlers. I would separate these into their own functions and just call the function inside the event handler. This will make it easier for you to modify later, and will allow you to more easily select which drawing method to use when the mouse is clicked.
Also, you have two 'click' handlers being called here, so both of these are being run when you click the mouse. You need to get rid of one of these, and just have one 'click' handler that will execute the appropriate function based on the selection. (see my example above).
Hope this helps!
Jake Lundberg
13,965 PointsWould need to see your code to help more, but I personally like to switch statements in cases like this. Here is an example using jquery:
function Drawing() {...}
function Rectangle() {...}
$(document).on('mousedown', function() {
var selection = $("#radioDiv input[type='radio']:checked");
switch(selection) {
case "drawing":
Drawing();
break;
case "rectangle":
Rectangle();
break;
...
}
});
Again, would need to see your code to see how exactly to help. But this is how I would break it up.
Bartek Matuszewski
5,300 PointsThank you Jake Lundberg for your advice. I 've seperated anonymous functions into their own functions, and now functions pencil() and drawRectangle() are called inside an event handler. But it still doesn't work.
http://jsfiddle.net/bmat1/h1fjfsdp/
After refreshing the page, when function rectangle is selected first, it draws rectangle and than after selecting pencil it draws, but after selecting rectangle again it still draws with pencil. On the other hand when pencil is selected first it is impossible to draw rectangle. Have you got any idea how to solve this
Bartek Matuszewski
5,300 PointsBartek Matuszewski
5,300 Pointshere is fiddle link http://jsfiddle.net/bmat1/btw85Lb8/