I have a canvas with a 400x300px coordinates system (set with width
and height
attributes of the <canvas>
DOM element)
Sometimes it has to be resized to a bigger size with the CSS scaling system.
How to change the image upscaling/downscaling algorithm? (bicubic, spline, nearest neighbour, etc.)
I have tested image-rendering: optimizeQuality
, image-rendering: crisp-edges
, image-rendering: -webkit-optimize-contrast
, image-rendering: optimize-contrast
, interpolation-mode: nearest-neighbor
without success.
Is there nowadays a direct CSS way, without JS low-level image modification (like in HTML5 Canvas Resize (Downscale) Image High Quality?)?
Example with default algorithm (is there a way to change this default algorithm?):
ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 100, 100);
ctx.fillStyle = "#00FF00";
ctx.fillRect(100, 100, 100, 100);
ctx.fillStyle = "#0000FF";
ctx.fillRect(200, 200, 100, 100);
ctx.fillStyle = "#000000";
ctx.fillRect(300, 0, 100, 100);
ctx.font = "50px sans-serif";
ctx.fillText("Hello world", 50, 50);
.canvas-wrapper {
width: 852px;
}
canvas {
width: 100%;
background-color: #ccc;
}
<div class="canvas-wrapper">
<canvas width="400" height="300"></canvas>
</div>