Requirement: So I am using a html5 canvas to perform drawings on an iPad(2732 * 2048) and store the pixels drawn so I can reuse them.
Now I need to show the same drawing on a website as a downscaled version (615 * 460) by redrawing it per pixel.
Implementation
I am using fabric.js for all rendering of canvases.
To downscale :
- multiple the height and width of the canvas by the resize ratio so the drawing is scaled to the original size.
- Force the height and width of the container to be fixed as the dimensions I need for the drawing to be shown.
- Fill the container to 100% of its dimensions using CSS.
Issues On rendering in Chrome the drawing is aliased but it renders properly in Safari on Non retina displays.
Have tested with pure canvas also and it's the same behavior.
Drawing on Chrome 85
Drawing on Safari 14
Pseudo Code: (Showing only the required settings and styles to avoid complication)
// resizeRatio = 4.45 (2732 / 615);
canvas = new fabric.Canvas('someId');
canvas.enableRetinaScaling = false;
canvas.freeDrawingBrush.color = '#000000';
canvas.setWidth(460 * resizeRatio);
canvas.setHeight(615 * resizeRatio);
container = document.getElementbyId('container');
container.style.width = (this.width) + 'px';
container.style.height = (this.height) + 'px';
brush = new fabric.PencilBrush(canvas);
brush.width = 2.5;
// Loop over each of the coordinates using mouse events on the brush to render the
// canvas.
HTML
<div id=container style=" width: 100% !important;height: 100% !important;">
<canvas style=" width: 100% !important; height: 100% !important;" id="canvas"></canvas>.
</div>
Thank you