Week 6: Generative Language Art
Interactive Website (complete with CSS design) (Due Thursday Feb 19)
Hangman Game (complete with CSS design) (Due Feb 26 )
JavaScript Codecademy #2 (DUE March 5 )
Module Notes
Important:
- Submit the urls of ALL your projects to Slack and Canvas and always verify the address is a working URL!
- In the script, at the top, include the natural language logic steps you worked out with JS Mentor. Use JavaScript terms (as best you can). Make a list format with broken lines and numbers for easier reading. And include a url(s) to the ChatGPT/JS Mentor chat(s) that shows your work with AI.
- Add your project to your GitHub repository.
- For the next projects in this class, add your own comments in your script, along wth ChatGPT comments explaining what is happening at each step. Study these comments to understand the logic in the syntax.
Review:
- Statements
- Variables
- Data Types
- Operators (basic and "shortcut" operators)
- Comparison Operators
- Arrays
- Functions
- For Loops, While Loops
- Conditional Statements
- Scope
- Arrays Methods
- Arrays Iterators
- Objects, Methods, and Properties
Generative Literature:
What is Generative Language Art?
Generative language art is a type of electronic literature (e-lit) where a computer script or algorithm dynamically generates poems, stories, or other text-based works. These works often incorporate randomness or evolving digital processes to create unique and engaging outputs.
JavaScript and Poetic Text:
Examples of generative literature:- Taroko Gorge by Nick Montfort
- "Refactored" Taroko Script (with comments)
- Taper: An online literary magazine for small computational pieces
Student E-Lit
- holly-slocum-bedbugs
- Noise - Tommy Ortale
- A Poem of Love & Unity Across Languages
- Elastic Text
- Tech Support
Code Guidelines for Taper:
- All code (HTML, CSS, JavaScript) must fit within 2KB (2048 bytes).
- Use a Minifier to compress code or Unminify for readability.
More Taper Projects
- https://taper.badquar.to/2/if_jupiter_had_turned_into_a_star.html
- https://taper.badquar.to/4/links.html
- https://taper.badquar.to/6/chance_infections.html
- https://taper.badquar.to/7/night_voyagers.html
- https://taper.badquar.to/7/moons_of_jupiter_7.html
- https://taper.badquar.to/7/verdigris_7.html
- https://taper.badquar.to/11/returning_thoughts.html
- https://taper.badquar.to/11/hollywood_hitmaker.html
- https://taper.badquar.to/11/infinite_scroll.html
- https://taper.badquar.to/11/this_bird_has_flown.html
- https://taper.badquar.to/11/hail.html
- https://taper.badquar.to/10/sense_of_existence.html
- https://taper.badquar.to/9/hell_is_overthinking.html
- https://taper.badquar.to/9/life_plan.html
- https://taper.badquar.to/9/nein_finality.html
- https://taper.badquar.to/8/wreckage_of_the_infinite.html
- https://taper.badquar.to/7/flaneur_treadmill.html
Manipulating Text:
Key JavaScript methods for text manipulation:- charAt(): Get a character at a specific position in a string
- slice(): Extract a part of a string
- toUpperCase() / toLowerCase(): Change case
- replace() / replaceAll(): Replace text
- split(): Convert a string into an array
Array Methods:
- pop(): Remove the last element
- push(): Add a new element
- shift(): Remove the first element
- splice(): Add/remove elements
- slice(): Create a subarray
- sort() / reverse(): Sort or reverse elements
Regex (Regular Expressions):
Examples of common tasks:// Break a line after a period
Find: \.
Replace with: \.\n
// Add a quote at the start of lines
Find: ^
Replace with: "
// Add a quote and comma at the end of lines
Find: $
Replace with: ",
// Remove all line breaks
Find: \n\s+
Replace with: (nothing)
Try it with Sample Text.
JavaScript Animation
// get the box element
let box = document.getElementById("box");
// starting position
let position = 0;
// run the function every 50 milliseconds
setInterval(moveBox, 50);
// function to move the box
function moveBox() {
position += 2; // move 2px each tick
box.style.top = position + "px";
}
JavaScript / CSS Animation Reference
https://javascript.info/css-animations
/* CSS: define the box and the animation class */
#box {
position: absolute;
width: 50px;
height: 50px;
background: red;
top: 0;
left: 100px;
transition: transform 0.5s;
}
/* this class triggers the movement */
.moveDown {
transform: translateY(200px);
}
// JavaScript: add or remove the class
let box = document.getElementById("box");
// add the class (moves the box down)
function moveDown() {
box.classList.add("moveDown");
}
// remove the class (moves the box back up)
function moveUp() {
box.classList.remove("moveDown");
}
<!-- Example HTML buttons to trigger animation -->
<button onclick="moveDown()">Move Down</button>
<button onclick="moveUp()">Move Up</button>
<div id="box"></div>
- Use the JavaScript version if you need to dynamically change the speed, stop the animation based on user actions, or generally have fine-grained control in code.
- Use the CSS version if you only need a basic (but smooth) animation, like sliding in/or or fade in/out. It’s less code to write and is typically optimized by the browser.
Shuffle an Array (No Repeats)
The simplest reliable way to shuffle an array so every item appears exactly once (no repeats) is the Fisher–Yates shuffle.
Shuffle the array in place
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
let words = ["apple", "banana", "cherry", "date"];
shuffleArray(words);
console.log(words);
Draw items one-by-one (still no repeats)
After shuffling, use pop() to pull a new item each time until the array is empty.
shuffleArray(words);
let nextWord = words.pop(); // removes and returns one item
console.log(nextWord);
Thursday Workshop
Work independently on the Interactve Website or the Hangman projects. I will come around to help.
Project 3: Generative Language Art (15%) DUE March 12
Create an electronic literature piece using JavaScript to manipulate text. No other media, just text designed on the page with HTML, CSS and Javascript. Include comments listing your logic steps and ChatGPT url(s).
Development Steps:
- Idea Generation
- Conceptual Breakdown
- Consult with JS mentor to see if your idea is possible
- Work out Logic Steps with JS Mentor
- JavaScript Implementation with JS Mentor
- Testing and Refinement
- Styling with CSS
- Include the Statement
- Final Submission
Suggested Ideas:
- Interactive Poetry
- Generative Poetry
- Recombinant Narrative
- Text-Based Puzzle