Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Last active November 3, 2021 15:34
Show Gist options
  • Save jdmichaud/cf151d46eb88bbffa53cecafb858009c to your computer and use it in GitHub Desktop.
Save jdmichaud/cf151d46eb88bbffa53cecafb858009c to your computer and use it in GitHub Desktop.
Canvas interpolation investigation
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Canvas interpolation test</title>
<script>
function main() {
const canvas = document.getElementById('canvas');
canvas.width = 2;
canvas.height = 2;
const canvasCtx = canvas.getContext('2d');
const imageData = canvasCtx.createImageData(2, 2);
const view = new Uint32Array(imageData.data.buffer);
const zoomCanvas = document.getElementById('zoom');
zoomCanvas.width = 256;
zoomCanvas.height = 256;
const zoomCtx = zoomCanvas.getContext('2d');
zoomCtx.webkitImageSmoothingEnable= false;
zoomCtx.msImageSmoothingEnabled = false;
zoomCtx.imageSmoothingEnabled = false;
zoomCtx.drawImage(zoomCanvas, 0, 0, 256, 256);
const xcontrol = document.getElementById('xcontrol');
const ycontrol = document.getElementById('ycontrol');
function updateXY() {
const xvalue = Number(xcontrol.value) / 100;
const yvalue = Number(ycontrol.value) / 100;
console.log(xvalue, yvalue);
canvasCtx.fillStyle = 'white';
canvasCtx.fillRect(0, 0, 2, 2);
canvasCtx.fillStyle = 'black';
canvasCtx.fillRect(xvalue, yvalue, 2, 2);
zoomCtx.fillStyle = 'white';
zoomCtx.fillRect(0, 0, 256, 256);
zoomCtx.drawImage(canvas, 0, 0, 2, 2, 0, 0, 256, 256);
zoomCtx.strokeStyle = 'red';
zoomCtx.strokeRect(xvalue * 256 / 2, yvalue * 256 / 2, 256, 256);
}
updateXY();
xcontrol.addEventListener('input', e => updateXY());
ycontrol.addEventListener('input', e => updateXY());
const interpolateInput = document.getElementById('interpolate');
function updateInterpolate() {
const interpolate = interpolateInput.checked;
console.log(interpolate);
zoomCtx.webkitImageSmoothingEnabled = interpolate;
zoomCtx.msImageSmoothingEnabled = interpolate;
zoomCtx.imageSmoothingEnabled = interpolate;
updateXY();
}
interpolateInput.addEventListener('input', e => updateInterpolate());
}
window.onload = main;
</script>
</head>
<body>
X <input id="xcontrol" type="range" min="0" max="100" value="0" class="slider">
Y <input id="ycontrol" type="range" min="0" max="100" value="0" class="slider">
interpolation <input id="interpolate" type="checkbox">
<p>ready</p>
<div><canvas id="canvas"></canvas></div>
<div><canvas id="zoom"></canvas></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment