Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda