Draw rectangles, circles, lines, triangles, and text on your canvas. These are the building blocks of everything you'll make!
Draws a rectangle. You give it the top-left corner position, width, and height.
| Parameter | Type | Description |
|---|---|---|
x | number | X position of the top-left corner |
y | number | Y position of the top-left corner |
w | number | Width |
h | number | Height |
function setup() {
size(400, 300);
}
function draw() {
background("white");
fill("dodgerblue");
rect(50, 50, 150, 100);
fill("tomato");
rect(220, 80, 80, 80);
}Draws a circle. You give it the center position and the radius (half the diameter).
| Parameter | Type | Description |
|---|---|---|
x | number | X position of the center |
y | number | Y position of the center |
r | number | Radius (distance from center to edge) |
function setup() {
size(400, 300);
}
function draw() {
background("white");
fill("mediumseagreen");
circle(200, 150, 80);
fill("gold");
circle(120, 100, 30);
circle(280, 100, 30);
}Draws a straight line between two points.
| Parameter | Type | Description |
|---|---|---|
x1 | number | X of the start point |
y1 | number | Y of the start point |
x2 | number | X of the end point |
y2 | number | Y of the end point |
function setup() {
size(400, 300);
}
function draw() {
background("white");
stroke("black");
strokeWeight(3);
line(50, 250, 350, 50);
stroke("tomato");
strokeWeight(2);
line(50, 50, 350, 250);
}Draws a triangle using three corner points.
| Parameter | Type | Description |
|---|---|---|
x1 | number | X of the first corner |
y1 | number | Y of the first corner |
x2 | number | X of the second corner |
y2 | number | Y of the second corner |
x3 | number | X of the third corner |
y3 | number | Y of the third corner |
function setup() {
size(400, 300);
}
function draw() {
background("white");
fill("mediumseagreen");
triangle(200, 50, 100, 250, 300, 250);
}Draws text on the canvas at the given position.
| Parameter | Type | Description |
|---|---|---|
str | string | The text to display |
x | number | X position |
y | number | Y position (top of the text) |
function setup() {
size(400, 200);
}
function draw() {
background("white");
fill("black");
textSize(32);
text("Hello!", 140, 40);
fill("dodgerblue");
textSize(18);
text("You can draw text too.", 100, 100);
}Draws an oval (ellipse) centered at a point. You give it the center, width, and height.
| Parameter | Type | Description |
|---|---|---|
x | number | X position of the center |
y | number | Y position of the center |
w | number | Width of the oval |
h | number | Height of the oval |
function setup() {
size(400, 300);
}
function draw() {
background("white");
fill("dodgerblue");
ellipse(200, 150, 200, 100);
fill("tomato");
ellipse(200, 150, 80, 140);
}Draws part of a circle (an arc). Angles are in degrees β 0 is right, 90 is down, 180 is left, 270 is up.
| Parameter | Type | Description |
|---|---|---|
x | number | X position of the center |
y | number | Y position of the center |
r | number | Radius of the arc |
startAngle | number | Starting angle in degrees |
stopAngle | number | Ending angle in degrees |
function setup() {
size(400, 300);
}
function draw() {
background("white");
fill("gold");
arc(200, 150, 80, 30, 330);
noFill();
stroke("tomato");
strokeWeight(4);
arc(200, 150, 100, 0, 180);
}