<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Page Title</title>
<script src="hello.js"></script>
<script>
...javascript code here...
</script>
</head>
</html>
Get an element in the document (selector
is like CSS selector)
const someElement document.querySelector("selector");
first element in document matching selector is returned
Create an element (some-tag
is desired tag of element)
let myElement = document.createElement("some-tag");
Add text to element
myElement.textContent = "some text";
Add element as child of another
someElement.appendChild(myElement);
If someElement
is an element, we can…
set an id
someElement.id = "some-id";
add a class
someElement.classList.add("some-class");
add a style
someElement.style.backgroundColor = "rgb(200,200,200)";
Cellular Automata
A cellular automaton consists of
0
or 1
In a single step, each cell updates its state based on
Space-time diagram shows evolution over time
Update rule: update to 1
if either neighbor was 1
, update to 0
otherwise
Updated state depends on 3 states:
There are $8 = 2^3$ possibilities that must be considered
A rule determines update state for each possibility
Associate each value with a bit in binary representation
Input:
0
to 255
)0
s and 1
s)Output:
To draw space-time diagram:
Apply Rule 90!
Visualize cellular automata to make a cool site!
cellular-automata.js
applyRule(config, rule)
config
a 0
-1
arrayrule
a number from 0
to 255
Node.js
We’ll use Node.js to test your assignment submission!
In Assignment 03, you added interactions to your grid with the :hover
pseudo-class:
.tile:hover {
border-color: white !important;
z-index: 100;
}
CSS can do more interesting transformations:
scale(x-scale, y-scale)
rotate(amount)
(deg)translate(x-amount, y-amount)
(px)skew(x-skew, y-skew)
(deg)For example:
.tile:hover {
transform: [transformations];
}
[Color grid demo]
…I’d like to see some motion…
CSS can do animations too!
.tile:hover {
animation-name: some-animation;
animation-duration: 1s;
animation-iteration-count: 1;
}
@keyframes some-animation {
from {
/* initial state */}
to {
/* final state */}
}
Check it out!
To have more robust interactions, we need JavaScript
Add an event listener to an element
let box = document.querySelector('#some-box');
box.addEventListener('event-name', someMethod(e));
"click"
element is clicked"mouseover"
, "mouseout"
…there are a lot!
Events also have properties:
e.target
the element that event happened toe.clientX
, e.clientY
relative coordinates of where mouse cursor was when the event occuredLet’s make our grid more interactive!