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

307
Views
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 answers
Answer question

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 Report

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 Report
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!