HTML5 canvas:有没有办法使用“最近邻居”调整图像大小重新采样?
我有一些 JS 对图像进行一些操作。我想要类似像素艺术的图形,所以我必须在图形编辑器中放大原始图像。 但我认为用小图像进行所有操作然后使用 html5 功能放大它是个好主意。这将节省大量处理时间(因为现在我的演示(警告:域名可能会导致工作中出现一些问题等)例如,在 Firefox 中加载时间非常长)。 但是当我尝试调整图像大小时,它会以双三次方式重新采样。如何使其在不重新采样的情况下调整图像大小?有没有跨浏览器的解决方案?
I have some JS that makes some manipulations with images. I want to have pixelart-like graphics, so I had to enlarge original images in graphics editor.
But I think it'd be good idea to make all the manipulations with the small image and then enlarge it with html5 functionality. This will save bunch of processing time (because now my demo (warning: domain-name may cause some issues at work etc) loads extremely long in Firefox, for example).
But when I try to resize the image, it gets resampled bicubically. How to make it resize image without resampling? Is there any crossbrowser solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
http://phrogz.net/tmp/canvas_image_zoom.html 可以使用 canvas 和
getImageData< 提供后备情况/代码>。简而言之:
更多:有关图像渲染的 MDN 文档
http://phrogz.net/tmp/canvas_image_zoom.html can provide a fallback case using canvas and
getImageData
. In short:More: MDN docs on image-rendering
我不久前使用 ImageData 编写了一个 NN 调整大小脚本(大约第 1794 行)
https ://github.com/arahaya/ImageFilters.js/blob/master/imagefilters.js
您可以在此处查看演示
http://www.arahaya.com/imagefilters/
不幸的是,内置调整大小应该稍微快一些。
I wrote a NN resizing script a while ago using ImageData (around line 1794)
https://github.com/arahaya/ImageFilters.js/blob/master/imagefilters.js
You can see a demo here
http://www.arahaya.com/imagefilters/
unfortunately the builtin resizing should be slightly faster.
您只需设置
context.imageSmoothingEnabled
< /a> 到false
。这将使使用 context.drawImage() 绘制的所有内容使用最近邻居调整大小。这比使用有更好的支持
图像渲染:像素化
。You can simply set
context.imageSmoothingEnabled
tofalse
. This will make everything drawn withcontext.drawImage()
resize using nearest neighbor.This has better support than using
image-rendering: pixelated
.canvas 元素上的此 CSS 有效:
截至 2021 年 9 月,这适用于 Chrome 93。
This CSS on the canvas element works:
This works in Chrome 93, as of September 2021.
我会回应其他人所说的并告诉你这不是一个内置函数。遇到同样的问题后,我在下面做了一个。
它使用 fillRect() 而不是循环遍历每个像素并绘制它。所有内容都带有注释,以帮助您更好地了解其工作原理。
下面是它的使用示例。
您的图像将显示在 HTML 中,如下所示:
data-src
标记包含您要放大的图像的 URL。这是一个自定义数据标签。下面的代码将从数据标记中获取图像 URL,并将其通过调整大小函数,返回一个更大的图像(原始大小的 30 倍),然后将其注入到的
标签。src
属性中img请记住,在包含以下内容之前,请将函数
Resize_Nearest_Neighbour
(上面)放入标记中。
I'll echo what others have said and tell you it's not a built-in function. After running into the same issue, I've made one below.
It uses fillRect() instead of looping through each pixel and painting it. Everything is commented to help you better understand how it works.
Below is an example of it in use.
Your image would appear in the HTML like this:
The
data-src
tag contains the URL for the image you want to enlarge. This is a custom data tag. The code below will take the image URL from the data tag and put it through the resizing function, returning a larger image (30x the original size) which then gets injected into thesrc
attribute of theimg
tag.Remember to put the function
Resize_Nearest_Neighbour
(above) into the<script>
tag before you include the following.没有内置的方法。您必须使用
getImageData
自己完成此操作。There is no built-in way. You have to do it yourself with
getImageData
.基于 Paul Irish 的评论:
https://jsfiddle.net/djhyquon/69/
Based on Paul Irish's comment:
https://jsfiddle.net/djhyquon/69/