canvas 中的最近邻插值(Internet Explorer 9)
Nearest-neighbor interpolation in canvas (internet explorer 9)
我创建了一个简单的图像查看器(带有网格和图像),带有 canvas(仅图像)和一些 svg-gridlines 感谢 d3.js(类似于 this).我使用 d3.js 的缩放功能来缩放和平移我的 image/gridlines.
我在 canvas 上的图像应该使用最近邻插值法。我已经设置了许多样式元素来实现这个目标。 Chrome 和 Firefox 运行良好,但我在使用 Internet Explorer 9 时遇到问题(我需要支持 ie9)。在IE9中图像总是模糊的。
Internet Explorer 9 是否支持 canvas 的最近邻插值?
我的设置正确吗?
我的 canvas 代码在 d3.js:
canvas
.attr("width", dx)
.attr("height", dy)
.attr("style", "outline: thin solid black;")
.style("width", width + "px")
.style("height", height + "px")
.style("transform", "translate(" + marginleft + "px,0)")
.style("image-rendering","-moz-crisp-edges")
.style("image-rendering","-o-crisp-edges")
.style("image-rendering","-webkit-optimize-contrast")
.style("image-rendering","optimize-contrast")
.style("image-rendering","pixelated")
.style("-ms-interpolation-mode","nearest-neighbor")
.call(drawImage);
这个d3.js代码应该代表(大部分)这个css
img {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges; // Firefox
image-rendering: -o-crisp-edges; // Opera
image-rendering: -webkit-optimize-contrast; // Chrome (and eventually Safari)
image-rendering: pixelated; // Chrome
image-rendering: optimize-contrast; // CSS3 Proposed
-ms-interpolation-mode: nearest-neighbor; // IE8+
}
这是我的绘制函数
function drawImage(canvas2: any) {
var context = canvas.node().getContext("2d");
var image = context.createImageData(dx, dy);
for (var y = 0, p = -1; y < dy; ++y) {
for (var x = 0; x < dx; ++x) {
let pixval = heatmap[y][x];
var c = d3.rgb(color(pixval));
if(pixval <= min){
c = d3.rgb(255,255,255);
}
image.data[++p] = c.r;
image.data[++p] = c.g;
image.data[++p] = c.b;
image.data[++p] = 255;
}
}
context.mozImageSmoothingEnabled = false;
context.ImageSmoothingEnabled = false; //context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
context.putImageData(image, 0, 0);
imageObj.src = canvas.node().toDataURL();
}
请参阅 MDN's image-rendering page 上的兼容性说明
(强调我的)
[1] Internet Explorer 7 and 8 supports the non-standard -ms-interpolation-mode property with two values (bicubic and nearest-neighbor):
applies only to images (JPG, GIF, PNG, ...)
in IE7 only for images without transparency
does not inherit
default value IE7: nearest-neighbor (low quality)
default value IE8: bicubic (high quality)
obsolete as of IE9
看来答案是否定的,IE9不支持。
我创建了一个简单的图像查看器(带有网格和图像),带有 canvas(仅图像)和一些 svg-gridlines 感谢 d3.js(类似于 this).我使用 d3.js 的缩放功能来缩放和平移我的 image/gridlines.
我在 canvas 上的图像应该使用最近邻插值法。我已经设置了许多样式元素来实现这个目标。 Chrome 和 Firefox 运行良好,但我在使用 Internet Explorer 9 时遇到问题(我需要支持 ie9)。在IE9中图像总是模糊的。
Internet Explorer 9 是否支持 canvas 的最近邻插值?
我的设置正确吗?
我的 canvas 代码在 d3.js:
canvas
.attr("width", dx)
.attr("height", dy)
.attr("style", "outline: thin solid black;")
.style("width", width + "px")
.style("height", height + "px")
.style("transform", "translate(" + marginleft + "px,0)")
.style("image-rendering","-moz-crisp-edges")
.style("image-rendering","-o-crisp-edges")
.style("image-rendering","-webkit-optimize-contrast")
.style("image-rendering","optimize-contrast")
.style("image-rendering","pixelated")
.style("-ms-interpolation-mode","nearest-neighbor")
.call(drawImage);
这个d3.js代码应该代表(大部分)这个css
img {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges; // Firefox
image-rendering: -o-crisp-edges; // Opera
image-rendering: -webkit-optimize-contrast; // Chrome (and eventually Safari)
image-rendering: pixelated; // Chrome
image-rendering: optimize-contrast; // CSS3 Proposed
-ms-interpolation-mode: nearest-neighbor; // IE8+
}
这是我的绘制函数
function drawImage(canvas2: any) {
var context = canvas.node().getContext("2d");
var image = context.createImageData(dx, dy);
for (var y = 0, p = -1; y < dy; ++y) {
for (var x = 0; x < dx; ++x) {
let pixval = heatmap[y][x];
var c = d3.rgb(color(pixval));
if(pixval <= min){
c = d3.rgb(255,255,255);
}
image.data[++p] = c.r;
image.data[++p] = c.g;
image.data[++p] = c.b;
image.data[++p] = 255;
}
}
context.mozImageSmoothingEnabled = false;
context.ImageSmoothingEnabled = false; //context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
context.putImageData(image, 0, 0);
imageObj.src = canvas.node().toDataURL();
}
请参阅 MDN's image-rendering page 上的兼容性说明 (强调我的)
[1] Internet Explorer 7 and 8 supports the non-standard -ms-interpolation-mode property with two values (bicubic and nearest-neighbor):
applies only to images (JPG, GIF, PNG, ...)
in IE7 only for images without transparency
does not inherit
default value IE7: nearest-neighbor (low quality)
default value IE8: bicubic (high quality)
obsolete as of IE9
看来答案是否定的,IE9不支持。