![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | MikaelT |
How can i make something like this in Godot?
Link: http://phrogz.net/tmp/canvas_zoom_to_cursor.html
Pan: Drag with mouse
Zoom: Use mouse wheel or click (and shift-click). Zoom will zoom at to mouse position.
I have spend more than 7 hours to make something similar in Godot. I’m stuck. Please help. Thanks.
/Mikael
What code do you have so far?
Magso | 2019-08-01 18:22
In my 3rd try, I started a new project, so any existing code did not “blur” my hunt for a solution.
Here is the code:
extends Node2D
var zoom = 1
var p1 = 0
func _ready():
set_process_input(true)
set_process(true)
func _process(delta):
$Label.text = "Zoom: "+str(zoom)+" Mouse: "+str(get_global_mouse_position()) + " Local: " + str(get_local_mouse_position())+ \
"\nPos: "+str(position) + \
"\nSpr:"+str($Sprite9.position) + \
"\nP1: "+str(p1)
func _input(event):
if Input.is_action_just_pressed("cam_zoom_in"):
AdjustZoom(0.1)
if Input.is_action_just_pressed("cam_zoom_out"):
AdjustZoom(-0.1)
if Input.is_action_just_pressed("test"):
zoom = 1
AdjustZoom(0)
func AdjustZoom(amount):
zoom += amount
$Sprite9.position = get_local_mouse_position()
var local = get_local_mouse_position()
if (amount != 0):
p1 = (1-zoom)*get_local_mouse_position()
print("->zoom: "+str(zoom)+ " ("+str(amount)+")")
position = p1
scale.x = zoom
scale.y = zoom
static func IsEqual(a, b, epsilon = 0.00001):
return abs(a - b) <= epsilon
MikaelT | 2019-08-01 18:47