Struggling to display an image in a canvas element that needs to be a certain percentage of its parent container's width. Despite my efforts, the image seems to blur once added to the canvas, which is not the desired effect.
I attempted to disable the blurring by setting
ctx.imageSmoothingEnabled = false
, but unfortunately, it did not solve the issue.
If anyone has suggestions on how to prevent image blurring within this canvas, I would greatly appreciate any insights!
var img = new Image();
img.src = 'https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg';
img.onload = function() {
var canvas = document.querySelector('canvas');
canvas.width = hidden.clientWidth;
canvas.height = hidden.clientHeight;
var ctx = canvas.getContext('2d');
ctx.webkitImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(hidden,
0, 0, this.naturalWidth, this.naturalHeight,
0, 0, hidden.clientWidth, hidden.clientHeight)
}
#container {
width: 800px;
background: #efefef;
}
#hidden {
position: absolute;
top: -1000%;
}
#hidden,
#canvas {
width: 60%;
}
<div id='container'>
<img src='https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg' id='hidden'>
<canvas></canvas>
</div>