Skip to main content

Bug Tracker

Side navigation

#10040 closed bug (duplicate)

Opened August 12, 2011 09:01PM UTC

Closed September 04, 2011 07:58PM UTC

Last modified March 14, 2012 08:52AM UTC

.height/.width functions stretch canvas element

Reported by: Owned by:
Priority: low Milestone: None
Component: dimensions Version: 1.6.1
Keywords: Cc: dmethvin
Blocked by: Blocking:

When using the .height and .width function on a canvas element the element seems to be stretched as opposed to the actual width and height set for the element. I have included a simple html example below. In the first window the drawing in the canvas acts as expected. In the second window as the cursor moves from the 0,0 position the actual drawing point seems to move exponentially.



<style type="text/css">

canvas {border-style:dotted; border-width:1;cursor:url(pencil.cur),auto;}


<script type="text/javascript" src="jquery.js"></script>





<script type="text/javascript">

var windowH = $(window).height();

var windowW = $(window).width();

var colorChoice = '#000000';

var canvasa = $('<canvas></canvas>').attr('id','canvasa');





var canvasb = $('<canvas></canvas>').attr('id','canvasb');





var somex = -1;

var somey = -1;

var isDown = false;



var can = $(this).offset();

var a = e.pageX - can.left;

var b = e.pageY -;

if (isDown){

canvas = document.getElementById($(this).attr('id'));

context = canvas.getContext('2d');


context.moveTo(somex, somey);

context.lineTo(a, b);

context.strokeStyle = colorChoice;


somex = a;

somey = b;




isDown = true;

var can = $(this).offset();

somex = e.pageX - can.left;

somey = e.pageY -;







Attachments (0)
Change History (7)

Changed August 16, 2011 05:24AM UTC by addyosmani comment:1

component: unfileddimensions
owner: →
priority: undecidedlow
status: newpending

Hi. Could you put your test case on jsFiddle, please?

Changed August 16, 2011 05:56PM UTC by comment:2

status: pendingnew

Pardon my ignorance. I did save the example to befuddled but as I don't have an account I don't have any way to reference it nor did I see any type of link that would point to the saved example. Is there something else I should do?

Changed August 16, 2011 06:05PM UTC by comment:3

I just put it on jsFiddle again. I now see the link, it just did not stick out that the url had changed slightly the first time. The example can be found here

Changed August 27, 2011 08:26PM UTC by dmethvin comment:4

status: newpending

Sorry but the test case isn't making sense for me. Is the canvas supposed to be blank? It's hard to see if there is any stretching going on if that is the case.

Changed September 02, 2011 12:06AM UTC by comment:5

status: pendingnew

Both canvases start out blank. If you draw in the top canvas it will act as expected. If you draw in the bottom canvas the line will not be drawn correctly.

Changed September 04, 2011 07:58PM UTC by addyosmani comment:6

cc: → dmethvin
resolution: → duplicate
status: newclosed

So, I've spent some time reviewing this issue:

I can confirm that as temporaryacct20 mentions, the second canvas (which has its height set using width/height) appears to be actually stretching rather than just having its dimensions altered as per the .attr() example higher up in the code.

Reviewing our current documentation, I haven't been able to locate any known gotchas detailing the above, however I was able to find #10125 ( Here, timmywil, who was involved in the attributes rewrite for 1.6.x mentions that this is actually desired behaviour and .attr() should ideally be used rather than directly trying to set the width/height through .width()/.height() or .css('width',..)/.css('height',..).

For this reason I'll be closing this ticket as a duplicate of 10125, however if another member of the team feels we should look into this again, feel free to reopen.

Changed September 04, 2011 07:58PM UTC by addyosmani comment:7

Duplicate of #10125.