CatGuy
CatGuy

Reputation: 31

Consistent Point Placement on Zoomable Tkinter Canvas

I'm working on a Python project where you have a canvas with an image that you can zoom in/out on various points, I made it so I can place dots on this canvas. Initially at zoom_scale 1 (no zoom change) the dots are placed correctly but when I zoom in it zooms out of place and I can't figure out what function to make to calculate their position on the 512x512 canvas and make them not move.

Reference:
[[][]

Canvas stays the same 512x512 and this is the form I need my coordinates in.
First iteration this works (before the image is zoomed (cropped and resized to fit the canvas)) and I get coordinates x:73 y:700 which I can easily convert to 512x512 format by dividing them by 1.5 (image_size/canvas_size). But when I zoom and image is of size 698x698 (zoomed by a factor of 1.1) the red point starts drifting away, it should be consistent so the point stays on there however I move the canvas, and if its out of the canvas I just don't render it.

This is the current code:

  def get_zoomed_image_xy_canvas_position
    (x, y, img_size, canvas_size, cropped_image_bbox, zoom_scale)
    :
        size_factor = img_size/canvas_size/zoom_scale
    
        x /= size_factor
        y /= size_factor
    
        return Vector3(x, 0, y)

Gif reference: enter image description here

Upvotes: 1

Views: 118

Answers (1)

CatGuy
CatGuy

Reputation: 31

Drew this way scaled down masterpiece: my masterpiece

And kind of figured it out, it was super simple:

def get_zoomed_image_xy_canvas_position(x, y, img_size, canvas_size, crop_bbox, zoom):
    x -= crop_bbox[0]
    y -= crop_bbox[1]

    canvas_scale = (img_size/zoom)/canvas_size
    canvas_x = x / canvas_scale
    canvas_y = y / canvas_scale

    return Vector3(canvas_x, 0, canvas_y)

Upvotes: 1

Related Questions