Redraw in Processing #
Processing moves a horizontal line based on a mouse click.
Example of Click and Draw in Processing
float y;
// The statements in the setup() function
// execute once when the program begins
void setup() {
size(640, 360); // Size should be the first statement
stroke(255); // Set line drawing color to white
noLoop();
y = height * 0.5;
}
// The statements in draw() are executed until the
// program is stopped. Each statement is executed in
// sequence and after the last line is read, the first
// line is executed again.
void draw() {
background(0); // Set the background to black
y = y - 4;
if (y < 0) { y = height; }
line(0, y, width, y);
}
void mousePressed() {
redraw();
}
Redraw in Godot #
To replicate the 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
Set the background to black color.
Godot > menu Project > Project Settings… > tab General > Rendering > Environment > Define Clear Color
Create Node2D called Main and attach a script:
extends Node2D
# Converted from https://processing.org/examples/redraw.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 black color in
# Godot > menu Project > Project Settings... > tab General >
# Rendering > Environment > Define Clear Color
var y = 0.0
var width = -1
var height = -1
# Called when the node enters the scene tree for the first time.
func _ready():
var size = get_viewport_rect().size
width = size.x
height = size.y
y = height * 0.5
set_process_input(true)
update()
# Invoked by mouse button click input event. The update() method
# notifies _draw() to update this node (Main in our case).
func _input(event):
if event is InputEventMouseButton:
update()
# elif event is InputEventMouseMotion:
# update()
# Called automatically only once after enter scene tree.
# If re-drawing is required, call update() on this node.
func _draw():
draw_rect(Rect2(0, 0, width, height), Color.black)
y = y - 4;
if y < 0: y = height;
draw_line(Vector2(0, y), Vector2(width, y), Color.white)
You can download the whole zipped Godot project here: 04-ClickAndDraw.zip.