Hi guys, all I wanna do its simple: While app window size changed, resize the Node2D at same time. My nodes are:

I wanna achieve is, when view_ratio < design_ratio

And when view_ratio > design_ratio

My code was:

extends Node

var designSize:Vector2 = Vector2.ONE
var viewportSize:Vector2 = Vector2.ONE

func _ready():
	designSize = Vector2(ProjectSettings.get_setting("display/window/size/viewport_width"), ProjectSettings.get_setting("display/window/size/viewport_height"))
	
	self.get_viewport().size_changed.connect(onResized)
	self.get_viewport().size_changed.emit()

func onResized():
	viewportSize = self.get_viewport().size

func _process(delta):
	var design_ratio:float = designSize.x / designSize.y
	var view_ratio:float = viewportSize.x / viewportSize.y
	var scale = Vector2.ONE

	if view_ratio < design_ratio:
		scale = Vector2.ONE * designSize.x / viewportSize.x
	elif view_ratio > design_ratio:
		scale = Vector2.ONE * designSize.y / viewportSize.y
	
	$Node2D.scale = scale
	$Node2D.position.x = (designSize.x * scale.x - viewportSize.x) * .5

And result is totally mess, what Im go wrong? Plz help, Thx guys

You can do this with Control nodes, that automatically scale and you have a few options like keep aspect, stretch, fill, etc.

If you want to do this manually, I can give you some code, but please let me know what you actually want.

Do you want the image centered, but with black bars? Do you want it to fill the window, but cropping the sides of the images? Do you want it aligned in a certain way (like scaling from the corner or the center), etc.

    cybereality Thank you so much, and yes, I want to do this manually.

    First of all, I want Sprite2D ( which is the child node of Node2D ) remain the "designed aspect ratio" when the app window is resizing, for example, I made the Sprite2D in IDE, the window size in project setting is 1280x720, Sprite2D is fit right in, so the "designed aspect ratio" is 16:9

    And, when the app window remain 16:9 aspect ratio, same as designed aspect ratio, the Sprite2D fill the window.

    When the app window resized to a narrow aspect ratio, ( window aspect ratio < designed aspect ratio ), lets say 3:9, the Sprite2D fill the window and centered, cropping the left & right sides, the height of Sprite2D same as window height.

    And last, when the app window resized to a wider aspect ratio, ( window aspect ratio > designed aspect ratio ), lets say 21:6, the Sprite2D centered with black bars on left & right sides, no cropping, also the height of Sprite2D same as window height.

    narrow aspect ratio

    wider aspect ratio

    I tried to set "Stretch Aspect" from expend to keep_width to achieve that. The wider aspect ratio works fine, but in narrow aspect ratio, I can't get Sprite2D centered and fill the window at same time. So I thought maybe leave the "Stretch Aspect" to expend, and try to make it manually.

    Node2D & Sprite2D scaling from the top left corner