Canvas 中的最近邻渲染
我有一个使用精灵表制作动画的精灵。他只有 16x16,但我想将他放大到 64x64 左右,以充分发挥像素的优势!
结果很糟糕,当然浏览器是反锯齿的吧。 :/
谢谢!
编辑: 不需要CSS,这是我的绘图函数。
function drawSprite(offsetx:number,offsety:number,posx:number,posy:number){
ctx.drawImage(img, offsetx*32, offsety*32, 32, 16, posx*32, posy*8, 128, 32);
}
此处(codepen)查看它是否正常工作
I have a sprite that animates using a sprite sheet. He is only 16x16, but I want to scale him up to around 64x64 in all its pixel-y goodness!
The results are terrible, of course the browser is anti aliasing it. :/
Thanks!
EDIT:
No css needed, here is my draw function.
function drawSprite(offsetx:number,offsety:number,posx:number,posy:number){
ctx.drawImage(img, offsetx*32, offsety*32, 32, 16, posx*32, posy*8, 128, 32);
}
See it kinda working here (codepen)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
万一有人再次偶然发现这个问题(像我一样),Webkit 支持
image-rendering
的-webkit-optimize-contrast
值,该值(当前)是最近邻升级。它可以应用于图像和画布。这是一个例子来说明它是如何进行的。
通过此设置,
WebkitSafari 和 Gecko/Firefox 都会使用最近邻算法将 16x16 画布空间和小图像放大到 128x128。更新 答案最初指出 Webkit 浏览器支持此功能;事实上,截至 2011 年 12 月 24 日,它适用于 Mac OS 上的 Safari 和 Mac OS 上的 Chrome,但不适用于 Windows 上的 Chrome(Windows 上的 Safari 尚未验证),如 Chromium 错误跟踪器的问题 106662。从词法上来说,Windows 上的 Chrome 将接受 CSS 样式表中的
-webkit-optimize-contrast
值,但不会产生任何效果。我期望有一天它会起作用,并且这种方法将是获得最近邻居升级的正确方法,但目前,使用这种方法意味着 Windows Chrome 用户将不得不忍受模糊。In case someone ever stumbles upon this again (like me), Webkit supports a
-webkit-optimize-contrast
value forimage-rendering
, which (currently) is an implementation of the nearest neighbor upscaling. It can be applied to both images and canvases.Here's an example of how it goes.
With this setup, both
WebkitSafari and Gecko/Firefox will enlarge the 16x16 canvas space and the small image to 128x128 with the nearest neighbor algorithm.UPDATE The answer initially stated that Webkit browsers supported this; in fact, as of 2011-12-24, it works with Safari on Mac OS and Chrome on Mac OS, but not with Chrome one Windows (Safari on Windows was not verified), as stated on Issue 106662 of the Chromium bug tracker. Lexically, Chrome on Windows will accept the
-webkit-optimize-contrast
value in a CSS style sheet, but it won't have any effect. It is my expectation that it will work someday, and this method will be the right way to get nearest neighbor upscaling, but for now, using this method means Windows Chrome users will have to live with the blurriness.在 Firefox 中,你可以使用这个:
但据我所知,对于所有其他浏览器,你几乎不走运。解决这个问题的一种方法可能是在 Photoshop(或其他)中将精灵放大到 64x64,然后在画布中使用缩小的尺寸。这样至少图像在“放大”时不会变得模糊。但它仍然不会完全给你纯粹的模式7,就像最近邻居的优点一样。
您可以使用 imageData 对象编写自己的缩放代码。这样做会导致显着的性能损失,但对于 16x16 图像,可能不会那么严重。
In Firefox you can use this:
But as far as I know for every other browser you are pretty much out of luck. One way around this perhaps is to scale up your sprite to 64x64 in Photoshop (or whatever), then use it scaled down in canvas. This way at least the image will not get blurry when "scaled up". It still won't exactly give you the pure mode7 like goodness of nearest neighbor though.
You could write your own scaling code using the imageData object. You will be incurring a significant performance penalty in doing so, but for a 16x16 image, it might not be that significant.