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

164
Views
How can I calculate image overflow in the canvas, so I can limit the image editor to always be filled no matter the scale/position of image?

I have a canvas in a react app which I can zoom in image and move it right/left/up/down. I want to be able to calculate image overflow size so that I can limit the left/right/up/down actions when there's no more room too go.

Here's the working app: https://codesandbox.io/s/reverent-mestorf-9gd1t?file=/src/App.js

As you can see when you zoom out the image will no longer be filled inside canvas. I want to be able to disable zoom out button when image is going to be smaller than canvas.

I also want to be able to disable move right/left/up/down buttons when there's no more room in the image to move.

current behavior just shows empty spots inside canvas.

Here's the canvas code inside my useEffect, whenever src, x, y and scale changes we'll render the canvas.

    useEffect(() => {
        if (src) {
            const canvas = canvasRef.current
            const canvasContext = canvas?.getContext('2d')
            const imageObj = new Image()
            imageObj.onload = () => {
                if (canvas) {
                    const hRatio = canvas.width / imageObj.width
                    const vRatio = canvas.height / imageObj.height
                    // Calculate ratio so the image is filled inside canvas
                    const ratio = Math.max(hRatio, vRatio)
                    canvasContext?.clearRect(0, 0, canvas.width, canvas.height)
                    canvasContext?.drawImage(
                        imageObj,
                        x,
                        y,
                        imageObj.width,
                        imageObj.height,
                        0,
                        0,
                        // for zooming scale is between 0 and 2
                        imageObj.width * ratio * scale,
                        imageObj.height * ratio * scale
                    )
                }
            }
            imageObj.src = src
        }
    }, [src, x, y, scale])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After a lot of playing around with canvas I found the solution.

my old draw image:

                    canvasContext?.drawImage(
                        imageObj,
                        x,
                        y,
                        imageObj.width,
                        imageObj.height,
                        0,
                        0,
                        // for zooming scale is between 0 and 2
                        imageObj.width * ratio * scale,
                        imageObj.height * ratio * scale
                    )

my new draw image:

                    canvasContext?.drawImage(
                        imageObj,
                        x,
                        y,
                        // scale is between 0 and 2 for zooming
                        imageObj.width * ratio * scale,
                        imageObj.height * ratio * scale
                    )

I needed to apply the ratio and zoom to the source image size instead of canvas's.

So now to simply calculating overflow I can do this:

overflowY = 3000(canvas height) - (imageObj.height * ratio * scale)
overflowX = 4500(canvas width) - (imageObj.width * ratio * scale)
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!