How I can resize uploaded image before apply in canvas? My Codes are as follow:
<input type="file" id="imgLoader">
$("#imgLoader").change(function(){
var file = this.files[0];
importImage(file);
});
function importImage(e){
var reader = new FileReader();
reader.onload = function (event) {
var imgObj = new Image();
imgObj.src = event.target.result;
imgObj.onload = function () {
var image = new fabric.Image(imgObj);
image.set({
//left: 250,
//top: 250,
angle: 0,
padding: 5,
cornersize: 10
});
canvas.add(image);
}
}
reader.readAsDataURL(e);
}
How can I implement it so that images will be resize in fixed width & height before apply in canvas?
Try this:
function importImage(e){
var reader = new FileReader();
reader.onload = function (event) {
var imgObj = new Image();
imgObj.src = event.target.result;
imgObj.onload = function () {
var image = new fabric.Image(imgObj);
image.set({
//left: 250,
//top: 250,
angle: 0,
padding: 5,
cornersize: 10
});
image.filters.push(new fabric.Image.filters.Resize({scaleX: 0.2, scaleY: 0.2}));
image.applyFilters(canvas.renderAll.bind(canvas));
canvas.add(image);
}
}
reader.readAsDataURL(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With