Skip to content

Commit 53378fc

Browse files
Ported "Input" examples from Processing (#12)
* Ported "Input" examples from Processing Ported all examples under the Input section of the Processing examples website. * fix: error declaring rectWidth outside setup() * Making sure ported examples here are identical to website's * Matched input examples merged in website * Matched the revised input examples in the L5 website, with proper crediting/licensing. * Cleaned up unrelated L5.lua and main.lua changes for another issue * tweak: delete white space * clean up to match main * sync fork & main * tweak: fix whitespace
1 parent 55c2a3c commit 53378fc

13 files changed

Lines changed: 611 additions & 2 deletions

examples/clock.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--[[
2+
Clock.
3+
4+
The current time can be read with the second(), minute(),
5+
and hour() functions. In this example, sin() and cos() values
6+
are used to set the position of the hands
7+
8+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
9+
]] --
10+
require("L5")
11+
12+
local cx, cy
13+
local secondsRadius
14+
local minutesRadius
15+
local hoursRadius
16+
local clockDiameter
17+
18+
function setup()
19+
size(640, 360)
20+
windowTitle("Clock")
21+
describe("Display the current time")
22+
23+
stroke(255)
24+
25+
local radius = min(width, height) / 2
26+
secondsRadius = radius * 0.72
27+
minutesRadius = radius * 0.60
28+
hoursRadius = radius * 0.50
29+
clockDiameter = radius * 1.8
30+
31+
cx = width / 2
32+
cy = height / 2
33+
end
34+
35+
function draw()
36+
background(0)
37+
38+
-- Draw the clock background
39+
fill(80)
40+
noStroke()
41+
ellipse(cx, cy, clockDiameter, clockDiameter)
42+
43+
-- Angles for sin() and cos() start at 3 o'clock;
44+
-- subtract HALF_PI to make them start at the top
45+
local s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI
46+
local m = map(minute() + map(second(), 0, 60, 0, 1), 0, 60, 0, TWO_PI) - HALF_PI
47+
local h = map(hour() + map(minute(), 0, 60, 0, 1), 0, 24, 0, TWO_PI * 2) - HALF_PI
48+
49+
-- Draw the hands of the clock
50+
stroke(255)
51+
strokeWeight(1)
52+
line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius)
53+
strokeWeight(2)
54+
line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius)
55+
strokeWeight(4)
56+
line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius)
57+
58+
-- Draw the minute ticks
59+
strokeWeight(2)
60+
for a = 0, 354, 6 do
61+
local angle = radians(a)
62+
local x = cx + cos(angle) * secondsRadius
63+
local y = cy + sin(angle) * secondsRadius
64+
point(x, y)
65+
end
66+
end

examples/constrain.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--[[
2+
Constrain.
3+
4+
Move the mouse across the screen to move the circle.
5+
The program constrains the circle to its box.
6+
7+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
8+
]] --
9+
require("L5")
10+
11+
local mx = 0
12+
local my = 0
13+
local easing = 0.05
14+
local radius = 24
15+
local edge = 100
16+
local inner = edge + radius
17+
18+
function setup()
19+
size(640, 360)
20+
windowTitle("Constrain")
21+
describe(" Move the mouse across the screen to move the circle. The program constrains the circle to its box.")
22+
23+
noStroke()
24+
ellipseMode(RADIUS)
25+
rectMode(CORNERS)
26+
end
27+
28+
function draw()
29+
background(51)
30+
31+
-- Change the position of the drawn ellipse to the position of the mouse with easing
32+
if (abs(mouseX - mx) > 0.1) then
33+
mx = mx + (mouseX - mx) * easing
34+
end
35+
36+
if (abs(mouseY - my) > 0.1) then
37+
my = my + (mouseY - my) * easing
38+
end
39+
40+
-- Constrain the position of the ellipse to the inner rectangle
41+
mx = constrain(mx, inner, width - inner)
42+
my = constrain(my, inner, height - inner)
43+
fill(76)
44+
rect(edge, edge, width - edge, height - edge)
45+
fill(255)
46+
ellipse(mx, my, radius, radius)
47+
end

examples/easing.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[
2+
Easing.
3+
4+
Move the mouse across the screen and the symbol will follow.
5+
Between drawing each frame of the animation, the program
6+
calculates the difference between the position of the
7+
symbol and the curson. If the difference is larger than
8+
1 pixel, the symbol moves part of the distance (0.05) from its
9+
current position toward the cursor.
10+
11+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
12+
]] --
13+
require("L5")
14+
15+
local x = 0
16+
local y = 0
17+
local easing = 0.05
18+
19+
function setup()
20+
size(640, 360)
21+
windowTitle("Easing")
22+
describe(" Move the mouse across the screen and the symbol will follow.")
23+
24+
noStroke()
25+
end
26+
27+
function draw()
28+
background(51)
29+
30+
-- Change the position of the drawn ellipse to the position of the mouse with easing
31+
32+
targetX = mouseX
33+
dx = targetX - x
34+
x = x + dx * easing
35+
36+
targetY = mouseY
37+
dy = targetY - y
38+
y = y + dy * easing
39+
40+
ellipse(x, y, 66)
41+
end

examples/keyboard-functions.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--[[
2+
Keyboard Functions
3+
4+
Click on the window to give it focus and press the letter keys to type colors.
5+
The keyboard function keyPressed() is called whenever a key is pressed.
6+
keyPressed() is another keyboard function that is called when a key is released.
7+
8+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
9+
10+
Original 'Color Typewriter' concept by John Maeda.
11+
]] --
12+
require("L5")
13+
14+
local maxHeight = 40
15+
local minHeight = 20
16+
local letterHeight = maxHeight
17+
local letterWidth = 20
18+
19+
local x = -letterWidth
20+
local y = 0
21+
22+
local newletter = false
23+
24+
local numChars = 26
25+
local colors = {}
26+
27+
function setup()
28+
size(640, 360)
29+
windowTitle("Keyboard Functions")
30+
describe("Press letter keys to create forms in time and space")
31+
32+
noStroke()
33+
colorMode(HSB, numChars)
34+
background(numChars / 2)
35+
36+
-- Set a hue value for each key
37+
for i = 0, numChars - 1 do
38+
colors[i] = color(i, numChars, numChars)
39+
end
40+
end
41+
42+
function draw()
43+
if newletter == true then
44+
-- Draw the "letter"
45+
local y_pos
46+
if letterHeight == maxHeight then
47+
y_pos = y
48+
rect(x, y_pos, letterWidth, letterHeight)
49+
else
50+
y_pos = y + minHeight
51+
rect(x, y_pos, letterWidth, letterHeight)
52+
fill(numChars / 2)
53+
rect(x, y_pos - minHeight, letterWidth, letterHeight)
54+
end
55+
newletter = false
56+
end
57+
end
58+
59+
function textinput(text)
60+
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
61+
62+
if string.match(key, "^[A-Z]$") then
63+
keyIndex = string.find(alphabet, key) - 1
64+
letterHeight = maxHeight
65+
fill(colors[keyIndex])
66+
elseif string.match(key, "^[a-z]$") then
67+
keyIndex = string.find(alphabet, string.upper(key)) - 1
68+
letterHeight = minHeight
69+
fill(colors[keyIndex])
70+
else
71+
fill(0)
72+
letterHeight = 10
73+
end
74+
75+
newletter = true
76+
77+
-- Update the "letter" position
78+
x = x + letterWidth
79+
80+
-- Wrap horizontally
81+
if x > width - letterWidth then
82+
x = 0
83+
y = y + maxHeight
84+
end
85+
86+
-- Wrap vertically
87+
if y > height - letterHeight then
88+
y = 0
89+
end
90+
end

examples/keyboard.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--[[
2+
Keyboard.
3+
4+
Click on the image to give it focus and press the letter keys
5+
to create forms in time and space. Each key has a unique identifying
6+
number. These numbers can be used to position shapes in space.
7+
8+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
9+
]] --
10+
require("L5")
11+
12+
function setup()
13+
size(640, 360)
14+
windowTitle("Keyboard")
15+
describe("Press letter keys to create forms in time and space")
16+
noStroke()
17+
background(0)
18+
rectWidth = width / 4
19+
end
20+
21+
function keyPressed()
22+
local keyIndex = -1
23+
local alphabet = "abcdefghijklmnopqrstuvwxyz"
24+
local upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
25+
26+
-- Check if it's a letter and get its index
27+
local upperPos = string.find(upperAlphabet, key)
28+
local lowerPos = string.find(alphabet, key)
29+
30+
if upperPos then
31+
keyIndex = upperPos - 1
32+
elseif lowerPos then
33+
keyIndex = lowerPos - 1
34+
end
35+
36+
if keyIndex == -1 then
37+
-- If it's not a letter key, clear the screen
38+
background(0)
39+
else
40+
-- It's a letter key, fill a rectangle
41+
fill(millis() % 255)
42+
local x = map(keyIndex, 0, 25, 0, width - rectWidth)
43+
rect(x, 0, rectWidth, height)
44+
end
45+
end

examples/milliseconds.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--[[
2+
Milliseconds.
3+
4+
A millisecond is 1/1000 of a second.
5+
L5 keeps track of the number of miliseconds a program has run.
6+
By modifying this number with the modulo(%) operator,
7+
different patterns in time are created.
8+
9+
Adapted from Processing examples. Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
10+
]] --
11+
require("L5")
12+
13+
function setup()
14+
size(640, 360)
15+
windowTitle("Milliseconds")
16+
describe("How L5 keeps track of the number of milliseconds a program has run")
17+
18+
noStroke()
19+
scale = width / 20
20+
end
21+
22+
function draw()
23+
for i = 0, scale do
24+
colorMode(RGB, (i + 1) * scale * 10)
25+
fill(millis() % ((i + 1) * scale * 10))
26+
rect(i * scale, 0, scale, height)
27+
end
28+
end

0 commit comments

Comments
 (0)