We will create a webpage that displays a haiku where each line is chosen at random from a set of possibilities. If the user clicks on a line it will fade out and fade back in as a new randomly selected line.
activities folder and name it haiku-generator (plain-javascript-project.zip)index.html to something appropriate like Haiku GeneratorWe can roughly divide this project up into four steps:
Our first task is to actually create a haiku. If you’re being pedantic, a haiku is a poem with three lines where the first and third lines have five syllables and the second line has seven syllables. An example from Matsuo Basho (he wrote it in Japanese, though):
In the twilight rain
these brilliant-hued hibiscus -
A lovely sunset.
To randomly generate one of these, we’ll need to have a set of five and seven syllable lines to choose from randomly.
First let’s create the set of lines to choose from.
fiveSyllableLines and assign an array with at least five five-syllable strings in it.sevenSyllableLines and assign an array with at least five seven-syllable strings in it.If you can’t think of anything, you can use these hot, hot strings:
Five syllables:
O, to be a tree,
The cat does not know,
We are all forests,
You have done your best,
They are all gone now
Seven syllables:
Say the things left unsaid,
Never believe the wind's lies,
The autumn stretches its legs,
Nothing can satisfy you,
They will not come back again
Now we’re ready to create the haiku, we should have each line in its own variable and randomly choose lines from our arrays. To help with this, let’s first write a random() function that can return a random element from any array passed as an argument. Then we can use the function to choose and assign random lines to our haiku, then print it out in the console.
random() functionrandom() that takes one parameter called array (the array to choose a random element from), in itindex and assign a random index into the array provided (the formula for this is Math.floor(Math.random() * array.length))index in the arrayBack at the top of the program, after the arrays…
line1 and assign a random element from the fiveSyllableLines arrayline2 and sevenSyllableLinesline3 and fiveSyllableLinesWe can now print out our random haiku in the console! After declaring our line variables…
console.log() to print the three variables containing the linesNow when we run the program we should see a random haiku show up in the JavaScript Console! Job done! But only if we have a user who loves looking at webpages through the JavaScript Console… which is almost nobody!
We want to display our haiku on the actual webpage rather than in the JavaScript console so we’ll need to add some HTML structure to insert our lines into and then use JavaScript to insert the lines of the poem onto the page.
First let’s create a basic structure in the HTML to insert our haiku into
<section> tag to contain the overall haiku, give it an id like haiku<p> tags, with ids of line-1, line-2, and line-3 to store the three lines of our haikuBack in our script, let’s replace the console.log() of the haiku with code to actually insert the lines onto the page itself
console.log() statementsdocument.getElementById() to store the three <p> tags in three variables (called something like line1P, line2P, line3P or similar) according to their ids.innerText property of the elements to set each one with its appropriate line of the haikuNow when the program runs you should see the haiku show up on the webpage itself, but it will look very… default.
It doesn’t look super inspiring, so this is an opportunity to add some CSS for our haiku section that will make things look a little nicer. So in the style.css…
id and add some styling. Consider at least font-family and font-sizebackground-color, maybe add a width, some margins etc. as you pleaseNow hopefully the page looks a little more alluring to the eye! No big deal!
We want to channel Raymond Queneau and be able to change individual lines of the haiku. We’ll do that by letting the user click on a line and have it swap the line for another randomly chosen line from the appropriate array.
setNewLine() that takes an element as a parameter (the line to change)if statement that checks if the element matches the variable containing the element for line 1, 2, or 3.innerText to a random line from the appropriate array (line 1 and 3 get five syllable lines, line 2 gets a seven syllable line)lineClicked() at the bottom of your program, it should:event (like any callback for addEventListener())setNewLine() and pass through the event.target element (the element that was clicked)click event listener to the variable containing the first line element (e.g. line1P)lineClicked as the event handler function you provide to the listenerNow when you look at the page you should be able to click on individual lines and see them instantly change to a new random line each time. Have fun composing amazing new haiku!
In order to be cool it would be nice if a clicked line would fade out, change while invisible, and then fade back in. That’s just cool, right? To do so, we’ll need animate the opacity of the line in JavaScript.
We want to edit lineClicked() (the event handler for a click on a line) to fade out the line over time. We’ll use requestAnimationFrame() to do this with the browser’s framerate.
Define a function fadeOut() that accepts two parameters
element (which will receive the element to fade out)opacity (which will receive the current opacity of that element)It should:
opacity number by a small amount (perhaps 0.01)opacity style property of element to the new opacity value so that it actually changesif statement to check if opacity is greater than 0requestAnimationFrame() with an anonymous function as an argument which calls fadeOut() with element and opacity as arguments, to keep fading
fadeIn() function!else statementIn lineClicked():
setNewLine() with a call to fadeOut, providing the event.target and 1 as argumentsIf we run this version of the program we should see each line fade out when we click on it. And then they’re just gone. The amazing disappearing haiku! Poignant! Sand mandala! Sad mandala!
When the a line has faded out we want to switch the text of the line, and then fade it back in. This is a pretty similar process to the above…
Define a function fadeIn() (yes, a lot of this will be the same as fadeOut()) that accepts two parameters:
element (which will receive the element to fade out)opacity (which will receive the current opacity of that element)It should:
opacity by a small amount (perhaps 0.01)opacity style property of element to the new opacity valueif statement to check if opacity is less than 1requestAnimationFrame() with an anonymous function as an argument which calls fadeIn() with element and opacity as arguments (to keep the fade going)
fadeIn() function!In fadeOut():
if statement that runs if the opacity has reached 0 add:setNewLine() with the element as an argument to switch the line (the same line we deleted earlier)fadeIn(), providing element and 0 as arguments (same idea as above, except fading in from 0)Now if we run the program, lines should fade out when clicked, then fade back in with a new line! It’s alive!
As always, there are probably improvements that can be made. Consider
.innerText of the elementsWe can imagine all kinds of improvements and changes!
<span> tags around the words you can change, could be fun with a rhyme scheme)mouseenter and mouseleave events on the lines and trigger visual (or other) changes for those events too<span> to do this)