gpt4 book ai didi

javascript - 调整 HTML5 Canvas 大小以适应窗口

转载 作者:bug小助手 更新时间:2023-10-28 10:54:54 25 4
gpt4 key购买 nike

如何自动缩放 HTML5 <canvas>适合页面的元素?

例如,我可以得到 <div>通过设置 height 进行缩放和 width属性为 100%,但 <canvas>不会扩展,是吗?

最佳答案

我相信我已经找到了一个优雅的解决方案:

JavaScript

/* important! for alignment, you should make things
* relative to the canvas' current width/height.
*/
function draw() {
var ctx = (a canvas context);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
//...drawing code...
}

CSS

html, body {
width: 100%;
height: 100%;
margin: 0;
}

到目前为止,对我的性能没有任何大的负面影响。

关于javascript - 调整 HTML5 Canvas 大小以适应窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1664785/

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