P5 library - Styling
Background color and/or reset the canvas
...
from p5 import *
def setup():
size(350, 350)
reset_canvas()
def reset_canvas():
background(240)
text("Click to reset the canvas", 10, 20)
def mouse_moved():
push_style()
color_mode(HSB)
fill(frame_count % 360, 100, 100, 0.3)
circle(mouse_x, mouse_y, 50)
pop_style()
def mouse_pressed():
reset_canvas()
Fill color
...
from p5 import *
def setup():
size(350, 350)
background(240)
# Default fill is white.
rect(0, height * 0.475, width, height * 0.05)
fill("red")
circle(width * 1/4, height / 2, width * 0.35)
fill("#00FF0030") # green with transparency
circle(width * 1/2, height / 2, width * 0.35)
no_fill()
circle(width * 3/4, height / 2, width * 0.35)
Stroke weight
...
from p5 import *
def setup():
size(350, 350)
background(240)
fill("#FF0000") # red
# Default stroke weight is 1.
circle(width * 1/4, height * 1/4, width * 0.35)
stroke_weight(5)
circle(width * 3/4, height * 1/4, width * 0.35)
stroke_weight(25)
circle(width * 1/4, height * 3/4, width * 0.35)
no_stroke()
circle(width * 3/4, height * 3/4, width * 0.35)
Stroke color
...
from p5 import *
def setup():
size(350, 350)
background(240)
fill("#FF0000") # red
stroke_weight(10)
# Default stroke is black.
circle(width * 1/4, height * 1/4, width * 0.35)
stroke("blue")
circle(width * 3/4, height * 1/4, width * 0.35)
stroke("#0000FF60") # blue with transparency
circle(width * 1/4, height * 3/4, width * 0.35)
no_stroke()
circle(width * 3/4, height * 3/4, width * 0.35)
Stroke cap
...
from p5 import *
def setup():
size(350, 350)
background(240)
stroke(0)
stroke_weight(20)
# Default stroke cap is ROUND.
line(width * 0.15, height * 0.2, width * 0.85, height * 0.2)
stroke_cap(SQUARE)
line(width * 0.15, height * 0.4, width * 0.85, height * 0.4)
stroke_cap(PROJECT)
line(width * 0.15, height * 0.6, width * 0.85, height * 0.6)
stroke_cap(ROUND)
line(width * 0.15, height * 0.8, width * 0.85, height * 0.8)
stroke(200)
stroke_weight(1)
line(width * 0.15, 0, width * 0.15, height)
line(width * 0.85, 0, width * 0.85, height)
Stroke join
...
from p5 import *
def setup():
size(350, 350)
background(240)
rect_mode(CENTER)
stroke_weight(20)
SIDE = width * 0.3
# Default stroke join is MITER.
rect(width * 1/4, height * 1/4, SIDE, SIDE)
stroke_join(ROUND)
rect(width * 3/4, height * 1/4, SIDE, SIDE)
stroke_join(BEVEL)
rect(width * 1/4, height * 3/4, SIDE, SIDE)
stroke_join(MITER)
rect(width * 3/4, height * 3/4, SIDE, SIDE)
Save and restore the styling context
...
from p5 import *
def setup():
size(350, 350)
LEFT = width * 1/5
CENTER = width * 1/2
RIGHT = width * 4/5
TOP = height * 1/3
BOTTOM = height * 2/3
DIAM = width * 0.2
background(240)
fill("red")
circle(LEFT, TOP, DIAM) # RED
push_style() # save styling context, fill is still red
fill("green")
circle(CENTER, TOP, DIAM) # GREEN
push_style() # save styling context, fill is still green
fill("blue")
circle(RIGHT, TOP, DIAM) # BLUE
fill("yellow")
circle(LEFT, BOTTOM, DIAM) # YELLOW
pop_style() # restore styling context, fill is green again
circle(CENTER, BOTTOM, DIAM) # GREEN
pop_style() # restore styling context, fill is red again
circle(RIGHT, BOTTOM, DIAM) # RED
run()