Is there a way to antialiase image that added to canvas using fabric.js I'm having trouble to do this with scaleToHeight function. I couldn't find any suitable option in fabric library.
var canvas = new fabric.Canvas('canvas');
var url = 'https://grafikoan.files.wordpress.com/2012/10/spheres-grafic-design-grafikoan-hd-300-p-2.png';
fabric.Image.fromURL(url, function (oImg) {
oImg.scaleToHeight(canvas.getHeight());
canvas.add(oImg);
canvas.renderAll();
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="canvas" width=400 height=300></canvas>
As you can see this image looks jagged.
fabricjs has its own resize filtering to avoid jaggies. Resize filter, with type "sliceHack" is the fastest/good result one. If you do not need dynamic filtering on resize you can also use lanzcos filtering, bilinear, fast hermit.
Please check this link for a working example with no CoR issue.
http://www.deltalink.it/andreab/fabric/resize.html
Compare normale image and sliceHack image, the filter will kick in once you resize.
Here more examples: https://github.com/kangax/fabric.js/issues/1741
The original image is 10X the size of the desired size.
Scaling downward by 10X is causing your "jaggies". Yes, scaling downward can cause jaggies just like scaling upward can cause jaggies.
You can lessen the jaggies by incrementally scaling the original image downward towards the desired size.
Here is example code and a Demo:
The original image is downscaled 4 times by half and then
scaleToHeight
is applied to get the final size of the fabric.Image.