HTML5 Canvas - 如何放大像素?
如何放大画布才能看到像素?目前,当我尝试使用scale()函数时,图像始终是抗锯齿的。
属性 mozImageSmoothingEnabled
似乎有效,但它仅适用于 Firefox。
How would one zoom in a Canvas to see the pixels? Currently when I try to use the scale() function the image is always antialiased.
The property mozImageSmoothingEnabled
seems to work, but it's only for Firefox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,浏览器所做的任何重新缩放都会导致平滑的插值。
所以只要你想用 JS 来做这件事,你就必须让 JS 来做所有的工作。这意味着要么找到一个不错的库,要么自己编写一个函数。它可能看起来像这样。但我希望可以让它更快。由于它是最简单的缩放算法之一,因此可能有很多人想出改进方法以使其更快。
该函数将在 (sx,sy) 处获取大小为 (sw,sh) 的矩形,将其大小调整为 (tw,th) 并在 (tx,ty) 处绘制。
As far as I know any rescaling done by the browser will result in a smooth interpolation.
So as long as you want to do this with JS you will have to let JS do all the work. This means either finding a nice library or writing a function yourself. It could look like this. But I hope it's possible to make it faster. As it's one of the simplest scaling algorithms there are probably many people who thought up improvements to do it even faster.
This function would take the rectangle of size (sw,sh) at (sx,sy), resize it to (tw,th) and draw it at (tx,ty).
据我所知,规范中没有定义抗锯齿行为,并且取决于浏览器。
您可以尝试的一件事是,如果您使用 CSS 将画布的宽度/高度设置为 300x300,然后将画布的宽度和高度属性设置为 150 和 150,它将显示为“放大”200%。我不确定这是否会触发抗锯齿,但请尝试一下。
As far as I know the antialiasing behavior is not defined in the spec and will depend on the browser.
One thing you could try is if you set the canvas's width/height with CSS into for example 300x300 and then give the canvas width and height attributes of 150 and 150, it will appear "zoomed in" by 200%. I'm not sure whether this will trigger the antialiasing, but do give it a shot.