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

203
Views
Is there a more effective way to transform coordinates?

I have a picture on canvas that I want to do a trapezoidal distortion on. The code itself is ready, but it is slow, very slow. 400x400 images in ~10-20 seconds on an i7 desktop machine. However, I want to run this on a mobile device with maximum camera resolution, so I'm looking for a more efficient way. Currently, my code works like this:

  1. request the coordinates of the distortion rectangle from the user. I need to get the quadrature coordinates from the user
  2. I divide the sides a and c into 400 parts.
  3. I iterate over the previous division, so I take the point on the 1/400th part of the side "a" and the point on the 1/400th part of side "c". Then the section between the points on the 2/400 part. And so on.
  4. I divide the section I have just produced by 400 and iterate over that as well.
  5. I take the line number produced in point 2 as "X" and the points in point 4 as "Y". The coordinates of the given points are Q and W.
  6. I take the pixel on the original canvas at coordinates "Q" and "W" and draw it on the target canvas at the location identified by coordinates "X" and "Y".

enter image description here

Is there a more efficient way to do this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As your essentially operating on the pixel level things have to be pretty slow. The fastest way would be to utilize WebGL and outsource the calculations to a fragment shader. That might be a bit of an overkill in this case though. If all you want to do is perspectively transform an image, there's a ready-to-use tiny library - though not as acurate as on the pixel level - which might suite your needs called perspective.js

All you have to do is feed it an image and your desired four corner points.

Here's an example:

let canvas = document.getElementById("canvas");
let image = new Image();
image.onload = function() {
  let ctx = canvas.getContext("2d");
  let p = new Perspective(ctx, image);

  p.draw([
    [65,43],
    [266, 16],
    [304,251],
    [26,151]
  ]);
}
image.src = "https://picsum.photos/id/1079/200/300";
canvas {
  border: gray solid 1px;
}
<script src="https://cdn.rawgit.com/wanadev/perspective.js/master/dist/perspective.min.js"></script>
<canvas id="canvas" width="400" height="300"></canvas>

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!