-
Notifications
You must be signed in to change notification settings - Fork 6
Ported "Input" examples from Processing #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f802ad4
Ported "Input" examples from Processing
hmyam6090-lab 0f0f0a4
fix: error declaring rectWidth outside setup()
hmyam6090-lab b29a3fb
Making sure ported examples here are identical to website's
hmyam6090-lab f1fa2d3
Matched input examples merged in website
hmyam6090-lab 5e5c96e
tweak: delete white space
hmyam6090-lab 485c78c
clean up to match main
hmyam6090-lab f78e19e
Merge branch 'L5lua:main' into main
hmyam6090-lab d9175c4
sync fork & main
hmyam6090-lab d3fc032
tweak: fix whitespace
hmyam6090-lab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| --[[ | ||
| Clock. | ||
|
|
||
| The current time can be read with the second(), minute(), | ||
| and hour() functions. In this example, sin() and cos() values | ||
| are used to set the position of the hands | ||
|
|
||
| Adapted from Processing Examples website. | ||
| https://processing.org/examples/clock.html | ||
| ]] -- | ||
| require("L5") | ||
|
|
||
| local cx, cy | ||
| local secondsRadius | ||
| local minutesRadius | ||
| local hoursRadius | ||
| local clockDiameter | ||
|
|
||
| function setup() | ||
| size(640, 360) | ||
| windowTitle("Clock") | ||
| describe("Display the current time") | ||
|
|
||
| stroke(255) | ||
|
|
||
| local radius = min(width, height) / 2 | ||
| secondsRadius = radius * 0.72 | ||
| minutesRadius = radius * 0.60 | ||
| hoursRadius = radius * 0.50 | ||
| clockDiameter = radius * 1.8 | ||
|
|
||
| cx = width / 2 | ||
| cy = height / 2 | ||
| end | ||
|
|
||
| function draw() | ||
| background(0) | ||
|
|
||
| -- Draw the clock background | ||
| fill(80) | ||
| noStroke() | ||
| ellipse(cx, cy, clockDiameter, clockDiameter) | ||
|
|
||
| -- Angles for sin() and cos() start at 3 o'clock; | ||
| -- subtract HALF_PI to make them start at the top | ||
| local s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI | ||
| local m = map(minute() + map(second(), 0, 60, 0, 1), 0, 60, 0, TWO_PI) - HALF_PI | ||
| local h = map(hour() + map(minute(), 0, 60, 0, 1), 0, 24, 0, TWO_PI * 2) - HALF_PI | ||
|
|
||
| -- Draw the hands of the clock | ||
| stroke(255) | ||
| strokeWeight(1) | ||
| line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius) | ||
| strokeWeight(2) | ||
| line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius) | ||
| strokeWeight(4) | ||
| line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius) | ||
|
|
||
| -- Draw the minute ticks | ||
| strokeWeight(2) | ||
| for a = 0, 354, 6 do | ||
| local angle = radians(a) | ||
| local x = cx + cos(angle) * secondsRadius | ||
| local y = cy + sin(angle) * secondsRadius | ||
| point(x, y) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| --[[ | ||
| Constrain. | ||
|
|
||
| Move the mouse across the screen to move the circle. | ||
| The program constrains the circle to its box. | ||
|
|
||
| Adapted from Processing Examples website. | ||
| https://processing.org/examples/constrain.html | ||
| ]] -- | ||
| require("L5") | ||
|
|
||
| local mx = 0 | ||
| local my = 0 | ||
| local easing = 0.05 | ||
| local radius = 24 | ||
| local edge = 100 | ||
| local inner = edge + radius | ||
|
|
||
| function setup() | ||
| size(640, 360) | ||
| windowTitle("Constrain") | ||
| describe(" Move the mouse across the screen to move the circle. The program constrains the circle to its box.") | ||
|
|
||
| noStroke() | ||
| ellipseMode(RADIUS) | ||
| rectMode(CORNERS) | ||
| end | ||
|
|
||
| function draw() | ||
| background(51) | ||
|
|
||
| -- Change the position of the drawn ellipse to the position of the mouse with easing | ||
| if (abs(mouseX - mx) > 0.1) then | ||
| mx = mx + (mouseX - mx) * easing | ||
| end | ||
|
|
||
| if (abs(mouseY - my) > 0.1) then | ||
| my = my + (mouseY - my) * easing | ||
| end | ||
|
|
||
| -- Constrain the position of the ellipse to the inner rectangle | ||
| mx = constrain(mx, inner, width - inner) | ||
| my = constrain(my, inner, height - inner) | ||
| fill(76) | ||
| rect(edge, edge, width - edge, height - edge) | ||
| fill(255) | ||
| ellipse(mx, my, radius, radius) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --[[ | ||
| Easing. | ||
|
|
||
| Move the mouse across the screen and the symbol will follow. | ||
| Between drawing each frame of the animation, the program | ||
| calculates the difference between the position of the | ||
| symbol and the curson. If the difference is larger than | ||
| 1 pixel, the symbol moves part of the distance (0.05) from its | ||
| current position toward the cursor. | ||
|
|
||
| Adapted from Processing Examples website. | ||
| https://processing.org/examples/easing.html | ||
| ]] -- | ||
| require("L5") | ||
|
|
||
| local x = 0 | ||
| local y = 0 | ||
| local easing = 0.05 | ||
|
|
||
| function setup() | ||
| size(640, 360) | ||
| windowTitle("Easing") | ||
| describe(" Move the mouse across the screen and the symbol will follow.") | ||
|
|
||
| noStroke() | ||
| end | ||
|
|
||
| function draw() | ||
| background(51) | ||
|
|
||
| -- Change the position of the drawn ellipse to the position of the mouse with easing | ||
|
|
||
| targetX = mouseX | ||
| dx = targetX - x | ||
| x = x + dx * easing | ||
|
|
||
| targetY = mouseY | ||
| dy = targetY - y | ||
| y = y + dy * easing | ||
|
|
||
| ellipse(x, y, 66) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --[[ | ||
| Keyboard Functions | ||
|
|
||
| Click on the window to give it focus and press the letter keys to type colors. | ||
| The keyboard function keyPressed() is called whenever a key is pressed. | ||
| keyPressed() is another keyboard function that is called when a key is released. | ||
|
|
||
| Adapted from Processing Examples website. | ||
| https://processing.org/examples/keyboardfunctions.html | ||
| by Martin Gomez | ||
|
|
||
| Original 'Color Typewriter' concept by John Maeda. | ||
| ]] -- | ||
| require("L5") | ||
|
|
||
| local maxHeight = 40 | ||
| local minHeight = 20 | ||
| local letterHeight = maxHeight | ||
| local letterWidth = 20 | ||
|
|
||
| local x = -letterWidth | ||
| local y = 0 | ||
|
|
||
| local newletter = false | ||
|
|
||
| local numChars = 26 | ||
| local colors = {} | ||
|
|
||
| function setup() | ||
| size(640, 360) | ||
| windowTitle("Keyboard Functions") | ||
| describe("Press letter keys to create forms in time and space") | ||
|
|
||
| noStroke() | ||
| colorMode(HSB, numChars) | ||
| background(numChars / 2) | ||
|
|
||
| -- Set a hue value for each key | ||
| for i = 0, numChars - 1 do | ||
| colors[i] = color(i, numChars, numChars) | ||
| end | ||
| end | ||
|
|
||
| function draw() | ||
| if newletter == true then | ||
| -- Draw the "letter" | ||
| local y_pos | ||
| if letterHeight == maxHeight then | ||
| y_pos = y | ||
| rect(x, y_pos, letterWidth, letterHeight) | ||
| else | ||
| y_pos = y + minHeight | ||
| rect(x, y_pos, letterWidth, letterHeight) | ||
| fill(numChars / 2) | ||
| rect(x, y_pos - minHeight, letterWidth, letterHeight) | ||
| end | ||
| newletter = false | ||
| end | ||
| end | ||
|
|
||
| function textinput(text) | ||
| local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
|
||
| if string.match(key, "^[A-Z]$") then | ||
| keyIndex = string.find(alphabet, key) - 1 | ||
| letterHeight = maxHeight | ||
| fill(colors[keyIndex]) | ||
| elseif string.match(key, "^[a-z]$") then | ||
| keyIndex = string.find(alphabet, string.upper(key)) - 1 | ||
| letterHeight = minHeight | ||
| fill(colors[keyIndex]) | ||
| else | ||
| fill(0) | ||
| letterHeight = 10 | ||
| end | ||
|
|
||
| newletter = true | ||
|
|
||
| -- Update the "letter" position | ||
| x = x + letterWidth | ||
|
|
||
| -- Wrap horizontally | ||
| if x > width - letterWidth then | ||
| x = 0 | ||
| y = y + maxHeight | ||
| end | ||
|
|
||
| -- Wrap vertically | ||
| if y > height - letterHeight then | ||
| y = 0 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| --[[ | ||
| Keyboard. | ||
|
|
||
| Click on the image to give it focus and press the letter keys | ||
| to create forms in time and space. Each key has a unique identifying | ||
| number. These numbers can be used to position shapes in space. | ||
|
|
||
| Adapted from Processing Examples website. | ||
| https://processing.org/examples/keyboard.html | ||
| ]] -- | ||
| require("L5") | ||
|
|
||
| function setup() | ||
| size(640, 360) | ||
| windowTitle("Keyboard") | ||
| describe("Press letter keys to create forms in time and space") | ||
|
|
||
| noStroke() | ||
| background(0) | ||
|
|
||
| rectWidth = width / 4 | ||
| end | ||
|
|
||
| function draw() | ||
| -- Keep draw() here to continue looping while waiting for keys | ||
| end | ||
|
|
||
| function keyPressed() | ||
| local keyIndex = -1 | ||
| -- Convert the built-in key variable from a string to its byte value | ||
| local keyByte = string.byte(key) | ||
|
|
||
| if (keyByte >= string.byte('A') and keyByte <= string.byte('Z')) then | ||
| keyIndex = keyByte - string.byte('A') | ||
| elseif (keyByte >= string.byte('a') and keyByte <= string.byte('z')) then | ||
| keyIndex = keyByte - string.byte('a') | ||
| end | ||
|
|
||
| if (keyIndex == -1) then | ||
| -- If it's not a letter key, clear the screen | ||
| background(0) | ||
| else | ||
| -- If it's a letter key, fill a rectangle | ||
| fill(millis() % 255) | ||
| x = map(keyIndex, 0, 25, 0, width - rectWidth) | ||
| rect(x, 0, rectWidth, height) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.