gpt4 book ai didi

jquery - 设置 Canvas 元素的尺寸

转载 作者:太空宇宙 更新时间:2023-11-04 14:18:33 25 4
gpt4 key购买 nike

我有一个链接到 JSchart 的 Canvas .我希望通过 JQuery 动态设置 Canvas 高度和宽度。

HTML

<canvas id="keyFiguresChart"></canvas>

JS

this.drawChart();
this.$("#keyFiguresChart").width(800);
this.$("#keyFiguresChart").height(200);

当我在创建图表之前设置属性高度和宽度时,它不起作用。然后由 JSchart 库计算标准高度和宽度。

在创建图表后设置高度和宽度时,canvas 的尺寸是正确的,但内容被拉伸(stretch)以匹配尺寸。

最佳答案

您正在设置 canvas 的 CSS widthheight 属性,这会导致它拉伸(stretch)其内容。请参见 jQuery documentation :

尝试像这样设置元素的 widthheight 属性:

// Assuming you've only got one Canvas on page.
this.$("#keyFiguresChart").attr('width', 800);
this.$("#keyFiguresChart").attr('height', 200);

关于jquery - 设置 Canvas 元素的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20116255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com