开发者

Canvas Size HTML

开发者 https://www.devze.com 2023-02-06 07:33 出处:网络
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.

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 automati开发者_StackOverflowcally , How should i do that ? on a $(document).ready event ?

How shall i do that too ?


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消