Canvas 中的最近邻渲染

发布于 2024-09-10 12:48:32 字数 499 浏览 0 评论 0原文

我有一个使用精灵表制作动画的精灵。他只有 16x16,但我想将他放大到 64x64 左右,以充分发挥像素的优势!

alt text

结果很糟糕,当然浏览器是反锯齿的吧。 :/

谢谢!

编辑: 不需要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!

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街道布景 2024-09-17 12:48:32

万一有人再次偶然发现这个问题(像我一样),Webkit 支持 image-rendering-webkit-optimize-contrast 值,该值(当前)是最近邻升级。它可以应用于图像和画布。

这是一个例子来说明它是如何进行的。

.nn128 {
    width: 128px;
    height: 128px;
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-optimize-contrast;
}

<canvas class="nn128" width="16" height="16"></canvas>
<img src="path/to/small/image.png" alt="Small image" class="nn128">

通过此设置,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 for image-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.

.nn128 {
    width: 128px;
    height: 128px;
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-optimize-contrast;
}

<canvas class="nn128" width="16" height="16"></canvas>
<img src="path/to/small/image.png" alt="Small image" class="nn128">

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.

护你周全 2024-09-17 12:48:32

在 Firefox 中,你可以使用这个:

canvas {
  image-rendering: -moz-crisp-edges;
}

但据我所知,对于所有其他浏览器,你几乎不走运。解决这个问题的一种方法可能是在 Photoshop(或其他)中将精灵放大到 64x64,然后在画布中使用缩小的尺寸。这样至少图像在“放大”时不会变得模糊。但它仍然不会完全给你纯粹的模式7,就像最近邻居的优点一样。

您可以使用 imageData 对象编写自己的缩放代码。这样做会导致显着的性能损失,但对于 16x16 图像,可能不会那么严重。

In Firefox you can use this:

canvas {
  image-rendering: -moz-crisp-edges;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文