Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
How can I make transition smooth in Canvas (Fabric.js)

I made a small project in Fabri.js . In this project user can upload a image on canvas and start zoom in and zoom out on mouse wheel rotate by user. The maximum zoom out is 1. If the user reach the maximum zoom out point which is 1 then the image is get back to its original position. But I am facing the problem is that if the user can zoom in at one point and then zoom out at some other point on the canvas the image reaches to the original position but it reaches there with a jerk at the end. I need some transition smooth so that image reach to its original position without any jerk.

This is full code:

const canvas = new fabric.Canvas("canvas",{
    width:700,
    height:450,
    backgroundColor:"grey"
    // selection:false
});


var zoom=0;
canvas.on('mouse:wheel', function(opt) {
    var delta = opt.e.deltaY;
    // console.log(delta)
    zoom = canvas.getZoom();
    // console.log(zoom)
    zoom *= 0.999 ** delta;
    if (zoom > 40) zoom = 40;
    if (zoom < 1) zoom = 1;
    canvas.zoomToPoint({ x: opt.e.offsetX, y: opt.e.offsetY }, zoom);
    opt.e.preventDefault();
    opt.e.stopPropagation();

    //image back to its origanal position
    var vpt = this.viewportTransform;
    if (zoom === 1) {
      vpt[4] = 0;
      vpt[5] = 0;
    }   
});

canvas.renderAll();

//get image from user
var upload_image="";

const image = document.querySelector("#image"); 

image.addEventListener("change",()=>{
    const reader = new FileReader();
    // console.log(reader)
    reader.addEventListener("load",()=>{
        upload_image=reader.result;
    })
    reader.readAsDataURL(image.files[0]);
});

//set image on the canvas 
const setImage =() =>{
  fabric.Image.fromURL(upload_image,(img)=>{
    canvas.backgroundImage=img;
    canvas.viewportCenterObject(img);
    canvas.renderAll();
});
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!