Skip to content

Commit 72400d6

Browse files
committed
add and update examples
1 parent c861d35 commit 72400d6

19 files changed

Lines changed: 315 additions & 17 deletions

docs/assets/examples/10print.gif

165 KB
Loading
200 KB
Loading
29.8 KB
Loading

docs/examples/10print.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 10PRINT variations
2+
3+
10PRINT is the name of a [one-liner](https://en.wikipedia.org/wiki/One-liner_program) program originally written in the BASIC language for Commodore computers and widely shared among early 1980s home computer users.
4+
5+
The original line of code was typed into the terminal and immediately executed:
6+
7+
```BASIC
8+
10 PRINT CHR$(205.5+RND(1)); : GOTO 10
9+
```
10+
11+
This single line, which begins with the line number 10, says to [print](/reference/print) out the ASCII character ([char](/reference/char)) of the enclosed number that follows. That number is 205.5 plus a [random](/reference/random) number between 0 and 1, which, results in a decimal ([float](/reference/float)) between 205.5 and 206.5. When converted to an ASCII character, the fractional ([fract](/reference/fract)) decimal value is automatically removed, with an implicit [floor](/reference/floor), and the equivalent character `'\'` or `'/'` is printed out. Finally, the program says to goto line number 10 (the beginning of the line), a loop, to begin again, in an infinite loop. This results in a twisty maze of passages created with just these two ASCII characters that spill out over the terminal screen in tumbling lines until the computer operator / programmer interrupts it.
12+
13+
Much more can be said about 10print, and in fact, an incredible and widely-lauded 324-page collaboratively-written book also titled [10 PRINT CHR$(205.5+RND(1)); GOTO 10](https://10print.org/) was published in 2012 by MIT Press. It can be purchased, and is available as a creative-commons licensed book and freely downloadable PDF.
14+
15+
And here, following in tradition, we attempt yet another port of the original 10print instruction into somewhat-equivalent functionality with L5. This is but one of many (infinite?) potential implementations, and adds a varying block size each frame. The slow (1 frame per second here) rate is needed as otherwise the redrawing rate would be too quick for our eyes to handle.
16+
17+
A main structure in this implementation are nested loops. This classic programming pattern is used everywhere from grid-based and patterned artworks to top down and sidescrolling tiled 2D videogames. The inner loop of incrementing x values advances the blocks across the page all the way to the right, where at that point the y value advances one block down the page, and begins looping through the x values again, drawing to the right, finally stopping drawing one complete iteration of the maze upon reaching the bottom of the window before drawing anew top to bottom again on the successive frame.
18+
19+
Ways you can build on the 10print code:
20+
21+
* Add color to the maze
22+
* Alter the randomness percentage to be more likely to draw one kind of line or the other
23+
* Change the block output to other line forms or shapes to make other kinds of mazes
24+
* Figure out an implementation that iteratively adds one line per frame rather than drawing the whole maze grid at once
25+
* Apply more ideas you read about from [10Print](https://10print.org/) and ideas of your own
26+
27+
![a maze of changing corridor size](/assets/examples/10print.gif)
28+
29+
```lua
30+
require ("L5")
31+
32+
function setup()
33+
size(600, 800)
34+
block = height / 40
35+
36+
frameRate(1)
37+
38+
describe("A maze of twisty little passages in each frame")
39+
end
40+
41+
function draw()
42+
background(255)
43+
block = floor(random(10,140))
44+
for y = 1, height, block do
45+
for x = 1, width, block do
46+
if random() < 0.5 then
47+
line(x, y, x + block, y + block)
48+
else
49+
line(x + block, y, x, y + block)
50+
end
51+
end
52+
end
53+
end
54+
```
55+
56+
## Related References
57+
58+
* [for](/reference/for)
59+
* [floor()](/reference/floor)
60+
* [frameRate()](/reference/frameRate)
61+
* [if](/reference/if)
62+
* [line()](/reference/line.md)
63+
* [draw()](/reference/draw)
64+
* [random()](/reference/random)
65+
66+
## Related Examples
67+
68+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
69+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
70+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
71+
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.

docs/examples/animation-and-variables-animation-with-events.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ end
6666

6767
## Related Examples
6868

69-
* [Drawing Lines](animation-and-variables-drawing-lines.md)
70-
* [Conditions](animation-and-variables-conditions.md)
69+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
70+
* [Drawing Lines](animation-and-variables-drawing-lines.md) - A drawing program demonstrating HSB colorMode
71+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
72+
73+
---
7174

7275
Animation with Events: Revised by [Darren Kessner](https://github.com/dkessner). Edited and maintained by [p5.js Contributors](https://github.com/processing/p5.js?tab=readme-ov-file#contributors) and [Processing Foundation](https://processingfoundation.org/people). Adapted to L5 2025. Licensed under [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/).

docs/examples/animation-and-variables-conditions.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ If and else statements allow a [block of code](https://www.lua.org/pil/4.2.html)
44

55
Comparison operators help to form conditions by comparing two values. In this example, the hue of the circle resets to zero when the hue is at least 360 because of the if statement on line 69. You can read more about [relational operators](https://www.lua.org/manual/5.2/manual.html#3.4.3) in the Programming in Lua book.
66

7-
Logical operators allow conditions to be combined. [and](https://www.lua.org/manual/5.2/manual.html#3.4.4) checks that both conditions are true. The circle in this example has a black fill when it is toward the horizontal center of the canvas, and it has a white fill when it is not. This is because of the if statement on line 45, which checks that the circle’s x position is at least 100 and also no more than 300. [or](https://www.lua.org/manual/5.2/manual.html#3.4.4) checks that at least one of the conditions is true. The circle reverses horizontal speed when it reaches the left or right edge of the canvas because of the if statement on line 75.
7+
Logical operators allow conditions to be combined. [and](https://www.lua.org/manual/5.2/manual.html#3.4.4) checks that both conditions are true. The circle in this example has a black fill when it is toward the horizontal center of the canvas, and it has a white fill when it is not. This is because of the if statement on line 52, which checks that the circle’s x position is at least 100 and also no more than 300. [or](https://www.lua.org/manual/5.2/manual.html#3.4.4) checks that at least one of the conditions is true. The circle reverses horizontal speed when it reaches the left or right edge of the canvas because of the if statement beginning on line 82.
88

99
![A square screen with a white stripe center and a black circle overlapping a colored circle that changes position and background color when the mouse is held down.](/assets/examples/conditions.gif)
1010

@@ -111,7 +111,10 @@ end
111111

112112
## Related Examples
113113

114-
* [Drawing Lines](animation-and-variables-drawing-lines.md)
115-
* [Animaton with Events](animation-and-variables-animation-with-events.md)
114+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
115+
* [Drawing Lines](animation-and-variables-drawing-lines.md) - A drawing program demonstrating HSB colorMode
116+
* [Animaton with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
117+
118+
---
116119

117120
Conditions: Inspired by [the Conditional Shapes Example by Prof. WM Harris](https://archive.p5js.org/examples/control-conditional-shapes.html). Revised by [Caleb Foss](https://github.com/calebfoss). From 2024 onwards, edited and maintained by [p5.js Contributors](https://github.com/processing/p5.js?tab=readme-ov-file#contributors) and [Processing Foundation](https://processingfoundation.org/people). Adapted to L5 2025. Licensed under [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/).

docs/examples/animation-and-variables-drawing-lines.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ end
4242

4343
## Related Examples
4444

45-
* [Doodle Draw](doodle-draw.md)
46-
* [Animation with Events](animation-and-variables-animation-with-events.md)
47-
* [Conditions](animation-and-variables-conditions.md)
45+
* [Doodle Draw](doodle-draw.md) - A basic drawing program tracking mouse movements
46+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
47+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
48+
49+
---
4850

4951
Drawing Lines: Revised by [Darren Kessner](https://github.com/dkessner). Edited and maintained by [p5.js Contributors](https://github.com/processing/p5.js?tab=readme-ov-file#contributors) and [Processing Foundation](https://processingfoundation.org/people). Adapted to L5 2025. Licensed under CC BY-NC-SA 4.0.
5052

docs/examples/basic-pong.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function setup()
5959
}
6060

6161
noCursor()
62+
63+
description("A game of Pong between a human and computer AI player.")
6264
end
6365

6466
function draw()
@@ -141,9 +143,17 @@ end
141143

142144
## Related References
143145

144-
* [table](/table.md) - changing color modes
146+
* [if](/reference/if)
147+
* [height](/reference/height)
148+
* [noCursor](/reference/noCursor)
149+
* [random()](/reference/random)
150+
* [table](/reference/table)
151+
* [text()](/reference/text)
152+
* [width](/reference/width)
145153

146154
## Related Examples
147155

148-
* [Drawing software](/examples/drawing-software)
156+
* [10Print](10print.md) - An implementation of the classic maze-drawing algorithm
157+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
158+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
149159

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# createGraphics Offscreen
2+
3+
Sometimes you may find a need for creating an offscreen canvas and drawing graphics to this hidden screen. For that we have the [createGraphics()](/reference/createGraphics) function.
4+
5+
In this example, a type of drawing program, dragging with the mouse across the window will draw tiny photos wherever our mouse is located. If this was the entirety of the program, we'd have no need for createGraphics. A minimal implementation of this follows:
6+
7+
```lua
8+
require("L5")
9+
10+
function setup()
11+
-- initalize a global array of images
12+
img = {}
13+
14+
-- load all of our images
15+
for i=1,9 do
16+
img[i] = loadImage("assets/"..i..".png")
17+
end
18+
19+
describe("Draws random images at mouse position when dragged in the window.")
20+
end
21+
22+
function mouseDragged()
23+
image(random(img),mouseX, mouseY, 50, 50)
24+
end
25+
```
26+
27+
This program is fine, but what happens if we want a new color background each time we press down to draw a line or lay down photos? If we added `background()` to the `mouseDragged()` function than we would cover all of our previous drawing. To prevent this problem, we use an offscreen canvas.
28+
29+
In our `setup()` we load in photos to an array `img`. And we initialize our `offscreenCanvas` as a new graphics buffer for drawing with `createGraphics()`.
30+
31+
As draw runs continously, it grabs the offscreen canvas. We'll come back to that. In the mouseDragged event, as long as we are dragging the mouse we start drawing to the offscreen buffer, leaving a randomly-selected image at the equivalent of the mouse X and Y positions, but in the offscreen canvas. As we press down on the mouse, we draw a random color on the entire visible fullscreen. Then in draw, we take the offscreen graphics canvas and draw it as an image, starting at the top left 0, 0 position, effectively applying this offscreen doodle of images with transparent background on top of the color background. This allows us to maintain the design of drawing photos on a colored canvas without overwriting the entire window's contents.
32+
33+
To extend the program, try changing the images, creating buttons to select images to draw with, add a secret scratch-off image finder or turn it into a coloring book game. You could even have multiple offscreen canvases and apply them to the window based on rules or randomness.
34+
35+
![Drawing with photos on a colored background that changes on mouse press](/assets/examples/createGraphics-offscreen.gif "Drawing with photos on a colored background that changes on mouse press")
36+
37+
```lua
38+
require("L5")
39+
40+
function setup()
41+
fullscreen()
42+
windowTitle("Quick Pic Draw")
43+
img = {}
44+
for i = 1, 9 do
45+
img[i] = loadImage("assets/"..i..".png")
46+
end
47+
48+
background(0)
49+
describe("Draw with pictures.")
50+
51+
-- Create offscreen canvas
52+
offscreenCanvas = createGraphics(800, 600)
53+
end
54+
55+
function draw()
56+
image(offscreenCanvas:getCanvas(), 0, 0)
57+
end
58+
59+
function mouseDragged()
60+
offscreenCanvas:beginDraw()
61+
62+
image(random(img),mouseX,mouseY,50,50)
63+
64+
offscreenCanvas:endDraw()
65+
end
66+
67+
function mousePressed()
68+
background(random(255),random(255),random(255))
69+
end
70+
```
71+
72+
## Related References
73+
74+
* [createGraphics()](/reference/createGraphics)
75+
* [for](/reference/for)
76+
* [fullscreen()](/reference/fullscreen)
77+
* [image()](/reference/image)
78+
* [mouseDragged()](/reference/mouseDragged)
79+
* [mousePressed()](/reference/mousePressed)
80+
81+
## Related Examples
82+
83+
* [10print variations](10print.md) - An implementation of the classic maze-drawing algorithm
84+
* [Doodle draw](doodle-draw.md) - Basic implementation of a doodle drawing program
85+
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.
86+
* [Daily Rituals](daily-rituals.md) - One a day daily ritual generator

docs/examples/daily-rituals.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Daily Rituals
2+
3+
This program is inspired by game designer Michael Brough's [VESPER.5](https://mightyvision.blogspot.com/2012/08/vesper5.html) (also see [this interview](https://www.gamedeveloper.com/design/road-to-the-igf-michael-brough-s-i-vesper-5-i-) with Michael), as well as the [instruction works](https://en.wikipedia.org/wiki/Instructions_for_Paintings) of Yoko Ono, the long-running exhibition series [Do It](https://curatorsintl.org/exhibitions/8593-do-it-1997) and the small exhibition and objects [#Fortune](http://stfj.net/index2.php?year=2013&project=art/2013/Fortune) by artist and game designer Zach Gage.
4+
5+
Daily rituals uses L5's [loadStrings()](/reference/loadStrings) function to load a text file `things.txt` with 31 meditative artistic tasks, one for each possible day of a month.
6+
7+
The [text file can be downloaded](https://gist.github.com/lee2sman/43709f948a2c4be1132774e082a5ee0a) for use with the L5 program, or you can write your own. Be sure to place it in the same folder of your Daily Rituals project. It should have at least 31 lines of text. The font file [Frijole-Regular](https://fonts.google.com/specimen/Frijole) can be downloaded and placed in the same folder.
8+
9+
The program labels the window with the current day, loads the font and sets up text styling. Then it loads the lines of text from the text file into an array with as many elements as total days in a month. Finally, it draws some random lines for texture, grabs the current day of the month, and displays the day's ritual. Try logging your daily practice.
10+
11+
After trying out the program, consider changing the rituals, font, altering the time period, even building a way to save your own response to the ritual prompts, turning it into a game, or making your own totally new derivation on the idea.
12+
13+
Enjoy it.
14+
15+
![the text 'Close Your Eyes and Think of a Past Rainstorm - Sound, Taste. Name it.'](/assets/examples/daily-rituals.webp "The text: Close Your Eyes and Think of a Past Rainstorm - Sound, Taste. Name it.")
16+
17+
```lua
18+
require("L5")
19+
20+
function setup()
21+
local date=(year().." "..month().." "..day())
22+
23+
windowTitle("Daily ritual "..date)
24+
25+
f = loadFont("Frijole-Regular.ttf")
26+
27+
textFont(f)
28+
textSize(48)
29+
30+
things = loadStrings("things.txt")
31+
32+
background("blue")
33+
34+
stroke("grey")
35+
36+
-- adds some texture
37+
for i = 1, floor(random(12) + 1) do
38+
strokeWeight(random(1, 12))
39+
line(random(width), random(height), random(width), random(height))
40+
end
41+
42+
fill("yellow")
43+
44+
-- activity by day of the month
45+
local today = things[day()]
46+
47+
text(today, 25, 25, width - 50)
48+
end
49+
```
50+
51+
## Related References
52+
53+
* [day()](/reference/day)
54+
* [floor()](/reference/floor)
55+
* [loadFont()](/reference/loadFont)
56+
* [loadStrings()](/reference/loadStrings)
57+
* [local](/reference/local)
58+
* [random()](/reference/random)
59+
* [year()](/reference/year)
60+
61+
## Related Examples
62+
63+
* [10print variations](10print.md) - An implementation of the classic maze-drawing algorithm
64+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
65+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
66+
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.

0 commit comments

Comments
 (0)