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 jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 5

Shawn Parrotte
Shawn Parrotte
6,689 Points

Why is my drawing acting weird?

I decided to rebuild this drawing application from the ground up to test my skills at the moment.

But when I copied the drawing functionality line by line, it made my the drawing part really weird... it's like it's multiplying the offsetX and offsetY values by something.

Anyway, here's a codepen version if anyone wants to look at it and maybe see what's up:

http://codepen.io/shawnparrotte/pen/epGYqV

Thanks in advance!

-Shawn

1 Answer

Stephan L
Stephan L
17,821 Points

Hi there, I know this question is a few months old at this point, and your CodePen link looks like you solved the issue. But just in case anyone else is still having this problem, here's how I solved it.

The problem may be if you are resizing the canvas in CSS, or inline in the HTML. The canvas html element is set to a default size (300px x 150px), and if you try to re-size it using CSS it only scales the element based on the default size. That means if you make it larger than the default size your offset values will change, and the line may look pixelated or otherwise weird. You can instead change the actual .height and .width properties of the element using jQuery. And it even provided a neat opportunity to add a "resize" input field to the document that can easily be set up to interact with the document.

First, I created a function:

function setCanvasSize(h, w) {
        var h= $('#canvasHeight').val(); 
        var w = $('#canvasWidth').val();
        $canvas[0].height = $('#canvasHeight').val(); 
        $canvas[0].width = $('#canvasWidth').val();
        }

Then I just created a click event that triggers when the button with the id "#submitSize" is pressed by calling the function:

$('#submitSize').click(function() { 
        setCanvasSize();
        })

It's probably apparent from the above code, but for the HTML I just created text inputs with the id's "canvasHeight" and "canvasWidth". Hope that helps anyone else who was struggling with this!

Aurelien Roux
Aurelien Roux
25,567 Points

thank you so much for the solution!! It was driving me crazy!!