Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

310
Vistas
Zoom in on the pointer not on the origin of the uploaded image in fabric js Also to set the maximum zoom out to 1

I need to zoom in to the pointer location and the maximum zoom out location would be set to 1. the image is uplaoded to the canvas from the local system. Below is my js code snippet:

var canvas = new fabric.Canvas('canvas');

document.getElementById("uploader").onchange = function(e) {
  var reader = new FileReader();
  reader.onload = function(e) {
    var image = new Image();
    image.src = e.target.result;
    image.onload = function() {
      var img = new fabric.Image(image);
      img.set({
        left: 100,
        top: 60
      });
      img.scaleToWidth(1125);
      canvas.add(img).setActiveObject(img).renderAll();
    }
  }
  reader.readAsDataURL(e.target.files[0]);
}
canvas.on('mouse:wheel', function(opt) {
    var delta = opt.e.deltaY;
    var zoom = canvas.getZoom();
    zoom *= 0.999 ** delta;
    if (zoom > 20) zoom = 20;
    if (zoom < 0.01) zoom = 0.01;
    //canvas.setZoom(zoom);
    const center = canvas.getCenter();
    const centerPoint = new fabric.Point(center.left , center.right);
    canvas.setZoom(zoom);
    opt.e.preventDefault();
    opt.e.stopPropagation();
  })

HTML file:

<canvas id="canvas" width="1500" height="844" style="border:1px solid black"></canvas>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

To set max zoom out change this if (zoom < 0.01) zoom = 0.01; to if (zoom < 1) zoom = 1;

about 4 years ago · Juan Pablo Isaza Denunciar

0

you need to use this code

canvas.zoomToPoint({ x: opt.e.offsetX, y: opt.e.offsetY }, zoom);

instead of

const center = canvas.getCenter();
const centerPoint = new fabric.Point(center.left , center.right);
canvas.setZoom(zoom);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda