canvas2d API を使用すると、imageSmoothingEnabled
パラメーターのおかげで IE10+ ブラウザーで実行できます。
var c = document.getElementById('c');
var ctx = c.getContext('2d');
var img = document.getElementById('original');
img.src = 'http://lorempixel.com/200/200';
img.onload = function() {
var scale = .1;
//scale down your context matrix
ctx.scale(scale, scale)
// remove the smoothing
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.msImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
// draw a first time your image
ctx.drawImage(img, 0, 0, img.width, img.height);
// reset your context's transformation matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);
// draw your canvas on itself using the cropping features of drawImage
ctx.drawImage(c, 0, 0, img.width * scale, img.height * scale, 0, 0, img.width, img.height)
};
<canvas id="c" width="200" height="200"></canvas>
<img id="original" />
このパラメーターをサポートしていない古いブラウザーの場合は、ctx から取得した imageData を操作して、自分で行うこともできます。getImageData。
いくつかのリンク:
ctx。imageSmoothingEnabled
ctx. drawImage
ctx.
ctx をスケーリングします。setTransform
Ps :完全な要件を満たしているかどうかはわかりません。また、ピクセル化された結果ではなく、アーティファクト化された結果が必要な場合は、imageSmoothingEnabled
フラグを false に設定しないでください:
var c = document.getElementById('c');
var ctx = c.getContext('2d');
var img = document.getElementById('original');
img.src = 'http://lorempixel.com/200/200';
img.onload = function() {
var scale = .1;
//scale down your context matrix
ctx.scale(scale, scale)
// draw a first time your image
ctx.drawImage(img, 0, 0, img.width, img.height);
// reset your context's transformation matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);
// draw your canvas on itself using the cropping features of drawImage
ctx.drawImage(c, 0, 0, img.width * scale, img.height * scale, 0, 0, img.width, img.height)
};
<canvas id="c" width="200" height="200"></canvas>
<img id="original"/>