Yahoo
Yahoo

Reputation: 4187

Canvas Size HTML

I want to dynamically resize the canvas size according to the content of a DIV. i am using the following Code but it doesn't seems to work.

<canvas id="canvas1" width="800" height="2000"  > <canvas>

Javascript

document.getElementById("canvas1").style.height = document.getElementById("div").style.height;
document.getElementById("canvas1").style.width= document.getElementById("div").style.width;

Also i want that the canvas is loaded automatically , How should i do that ? on a $(document).ready event ?

How shall i do that too ?

Upvotes: 2

Views: 12489

Answers (1)

gblazex
gblazex

Reputation: 50109

Use offsetWidth to get the dimensions of the element (including borders).

// loaded automatically on page load
window.onload = function() {
    var div = document.getElementById("div");
    var canvas = document.getElementById("canvas1");
    canvas.height = div.offsetHeight;
    canvas.width  = div.offsetWidth;
}

Upvotes: 8

Related Questions