Width and Height

Width and Height in Processing #

Processing defines window ‘width’ and ‘height’ variables set in the size() function.

Width and Height in Godot #

Godot Coordinates Output

To replicate the same example in Godot, set the window size to 640 by 360 pixels:

Godot > menu Project > Project Settings… > tab General > Display > Window > Width: 640, Height: 360
Godot Screen Size

Set the background to gray color 127,127,127.

Godot > menu Project > Project Settings… > tab General > Rendering > Environment > Define Clear Color
Godot Screen Color

Create Node2D called Main with the following script attached:

extends Node2D

# Converted from https://processing.org/examples/widthheight.html

# Set the window size to 640 by 360 pixels in
# Godot > menu Project > Project Settings... > tab General > Display > Window > Width: 640, Height: 360
# Set the background to gray color 127,127,127,0 in
# Godot > menu Project > Project Settings... > tab General > Rendering > Environment > Define Clear Color

# Register the size_changed event
func _ready():
	get_tree().get_root().connect("size_changed", self, "on_size_changed")

# Event called on window size change
func on_size_changed():
	print("Resizing: ", get_viewport_rect().size)

# Custom draw - called only once and cached.
func _draw():
	var size = get_viewport_rect().size
	var width = size.x
	var height = size.y
	
	# Alternative way to set background color
	# draw_rect(Rect2(0, 0, width-1, height-1), Color.lightgray, true)
	
	for i in range(0, height, 20):
		draw_rect(Rect2(0, i, width, 10), color256(129, 206,15), true)
		draw_rect(Rect2(i, 0, 10, height), Color.white, true)

# Convert rgb values 0..255 to 0..1 range
func color256(r,g,b):
	return Color(r/255.0, g/255.0, b/255.0)

You can download the whole zipped Godot project here: 02-WidthAndHeight.zip.