Runs on every computer in the world that has a webbrowser.
Even if the internet is out!
π Hello, world!
What?
New language, new βHello, world!β
But this time, it is interactive!
We will say βhelloβ when someone clicks.
How?
index.html
<!DOCTYPE html><html><head><script type="text/javascript" src="script.js"></script></head><!-- The `()` means "take an action." --><body onclick="hello()"> Text.</body></html>
script.js
/* A function is a named action. */functionhello() {/* An alert is a text pop-up */alert("Hello, world!")} /* Enclose the action in {} */
It will mostly look like this (or animate the eyes of your ghost for a challenge!):
π‘ Idea
We will move the pupil
The pupil was position within the eye on the ghost using the left and top styles.
For example, we can move the 10vmin left and top.
This moves the βpupilβ off the βeyeβ as the βeyeβ is only β6vminβ in radius.
π Geometry
The eye is 6x6
The pupil is 3x3
So to move the pupil all the way across the eye, move it by 3 from top and left, each.
πΊ Display
We can follow the mouse
If the mouse is leftmost, the pupil should be at left:0vmin
If the mouse is rightmost, the pupil should be at left:3vmin
Helpfully:
JavaScript will tell us where the mouse is, and
JavaScript will let us easily update the style of an element.
Step 0
Letβs start making eye.js
Weβll start with the βHello, world!β example.
script.js
functionhello() {alert("Hello, world!")}
Step 1 π
We set the function to be performed on mouse clicks.
We do so using an event listener
A special JavaScript way to do an action whenever something happens.
script.js
functionhello() {alert("Hello, world!")}document.addEventListener('click', hello);/* "document" refers the website, "click" to what you do, "hello" to what JavaScript does. */
Step 2 πΊ
Letβs see where the click occured.
A mouseclick is an event, so letβs βgiveβ that event to hello by placing it within the ()
Letβs look at just the x coordinate within the webpage
Itβs called pageX
functionhello(event) {alert(event.pageX)}
Text
Alert π¨
You may notice clicking toward the left gives smaller numbers and to the right gives bigger numbers.
If you didnβt try it below!
We can use these numbers to move the pupil!
Text
Step 4 πΆββοΈ
Rather than an alert, we can change the location of some HTML element.
Give it a name.
Ask JavaScript to find it.
Use JavaScript to change its left value to the clicked value.
functionhello(event) {/* Find the element by id */const element =document.getElementById("name");/* Change the "left" value of the element's style */ element.style["left"] =event.pageX+"px";/* x is given in "px" */alert(event.pageX+"px");/* So we can see it */}
functionhello(event) {/* Find the element by id */const element =document.getElementById("pupil");/* Find the new "left" value*/constnew=3*event.pageX/window.innerWidth;/* Update */ element.style["left"] =new+"vmin";}document.addEventListener('click',hello(event));
Step 7 β
Also do vertical movement.
eye.js
functionhello(event) {/* Find the element by id */const element =document.getElementById("pupil");/* Find the new "x" value*/const x =3*event.pageX/window.innerWidth;/* Find the new "y" value*/const y =3*event.pageY/window.innerHeight;/* Update */ element.style["left"] = x +"vmin"; element.style["top"] = y +"vmin";}document.addEventListener('click',hello(event));
π Visually
It will mostly look like this (or animate the eyes of your ghost for a challenge!):
Altogether
This is a bit confusing.
Try this out altogether like so.
This uses mousemove rather than click (itβs more fun).