zero width and height on dynamically created canvas

16 views
Skip to first unread message

henrik

unread,
Sep 10, 2008, 5:59:32 AM9/10/08
to google-excanvas
Hello, I am trying ExCanvas (svn revision 36) with a dynamically
created canvas. I have found one problem,
the width and height of the canvas style is not copied down to the div
that excanvas creates for
the context.

In this function:
CanvasRenderingContext2D_(surfaceElement) {
There is a line:
el.style.width = surfaceElement.clientWidth + 'px';

The clientWidth is 0 on my surfaceElement (the canvas tag), but the
style.width has a
correct value. I don't understand the particular details of when
clientWidth is updated,
but I believe a simple fix could be to grab style.width if clientWidth
is 0.

However, in initElement there is a comment that suggest that there is
a fix already
in progress. I felt that the comment was a little to brief for me to
step in to hack...

// TODO: use runtimeStyle and coordsize
// el.getContext().setHeight_(attrs.height.nodeValue);

Instead I did a little fix outside of exCanvas, below if someone is
interested.

Now it works like a charm, this is great stuff.

Thanks, Henrik Hjelte



var c=$(this.id);

// we must set this.canvas to be the initialized version.
this.canvas=G_vmlCanvasManager.initElement(c);

function exCanvasDimensionsFix(canvas) {
canvas.getContext('2d'); // This initializies a new div, with
the wrong width on its style
var to=canvas.firstChild.style;
if (to.width === "0px") { // If not, the bug might have been
fixed
var from=canvas.style;
to.width=from.width;
to.height=from.height;
}
}
exCanvasDimensionsFix(this.canvas);

Reply all
Reply to author
Forward
0 new messages