简体   繁体   English

HTML5“画布高度和宽度”属性

[英]Html5 Canvas Height and Width attribute

So far I have been assigning my canvas attributes as such:- 到目前为止,我一直在将画布属性分配为:

<canvas id="myCanvas" width="915" height="650" style="border: 2px double #000000;"></canvas>

However, now I have come across a scenario, where my canvas height is not static. 但是,现在我遇到了一个场景,其中我的画布高度不是静态的。 It needs to be dynamic. 它必须是动态的。

for example at times, I am drawing an XY graph on my canvas with 20 rows and other times 100 rows. 例如,有时我在画布上绘制XY图,其中包含20行,有时还有100行。

which means my y-axis labeling is dynamic and variable. 这意味着我的y轴标签是动态且可变的。

I don't know this at the beginning of my script where in my old method I am assigning my height. 我不知道在脚本开始处是在我的旧方法中分配高度的地方。

So what I need is a counter that will, keep track of no of labels on the y-axis (say count` = 0, count ++) 所以我需要一个计数器,该计数器将跟踪y轴上的标签数(例如count` = 0,count ++)

Now, how do I dynamically update the height of the canvas from within my script ? 现在,如何在脚本中动态更新画布的高度?

Can any one help? 有人可以帮忙吗?

You can increase the height of you canvas like this: 您可以像这样增加画布的高度:

var c=document.getElementById("myCanvas");
c.height = whatEverHeightYouNeed;

If you change the height with CSS, your canvas drawings will be distorted. 如果使用CSS更改高度,则画布图形将变形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM