gpt4 book ai didi

HTML5 Canvas : is there a way to resize image with "nearest neighbour" resampling?

转载 作者:太空狗 更新时间:2023-10-29 13:10:47 29 4
gpt4 key购买 nike

我有一些 JS 可以对图像进行一些操作。我想要像像素艺术一样的图形,所以我不得不在图形编辑器中放大原始图像。但我认为最好对小图像进行所有操作,然后使用 html5 功能将其放大。这将节省大量处理时间(因为现在 my demo(警告:域名可能会导致工作中出现一些问题等)在 Firefox 中加载时间非常长,例如)。但是当我尝试调整图像大小时,它会以双三次方式重新采样。如何在不重新采样的情况下调整图像大小?有没有跨浏览器的解决方案?

最佳答案

image-rendering: -webkit-optimize-contrast; /* webkit */
image-rendering: -moz-crisp-edges /* Firefox */

http://phrogz.net/tmp/canvas_image_zoom.html可以使用 canvas 和 getImageData 提供后备案例。简而言之:

// Create an offscreen canvas, draw an image to it, and fetch the pixels
var offtx = document.createElement('canvas').getContext('2d');
offtx.drawImage(img1,0,0);
var imgData = offtx.getImageData(0,0,img1.width,img1.height).data;

// Draw the zoomed-up pixels to a different canvas context
for (var x=0;x<img1.width;++x){
for (var y=0;y<img1.height;++y){
// Find the starting index in the one-dimensional image data
var i = (y*img1.width + x)*4;
var r = imgData[i ];
var g = imgData[i+1];
var b = imgData[i+2];
var a = imgData[i+3];
ctx2.fillStyle = "rgba("+r+","+g+","+b+","+(a/255)+")";
ctx2.fillRect(x*zoom,y*zoom,zoom,zoom);
}
}

更多:MDN docs on image-rendering

关于HTML5 Canvas : is there a way to resize image with "nearest neighbour" resampling?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809345/

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