gpt4 book ai didi

javascript - 为什么更改 Canvas 大小会重置其参数?

转载 作者:行者123 更新时间:2023-12-03 03:14:29 26 4
gpt4 key购买 nike

当我更改 Canvas 大小时,我注意到参数“mozImageSmoothingEnabled”被重置。

HTML

<canvas id='canv'>Your browser don't support canvas.</canvas>

Javascript

var cnv = document.getElementById('canv');
var ctx = cnv.getContext('2d');
console.log(ctx.mozImageSmoothingEnabled); // default 'true'
ctx.mozImageSmoothingEnabled = false;
console.log(ctx.mozImageSmoothingEnabled); // shows 'false'
cnv.width = 100;
console.log(ctx.mozImageSmoothingEnabled); // shows 'true'

JSFiddle:https://jsfiddle.net/epvtuz37/

这是一个错误,还是预期的行为?

最佳答案

因为当您更改 Canvas 宽度高度参数时,附加上下文的每个属性都会重置为其默认值。

From specs

When the canvas element is created, and subsequently whenever the width and height attributes are set (whether to a new value or to the previous value), the bitmap and any associated contexts must be cleared back to their initial state and reinitialized with the newly specified coordinate space dimensions.

关于javascript - 为什么更改 Canvas 大小会重置其参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58277286/

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