summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2018-10-11 02:42:12 -0700
committerNick Van Doorn <vandoorn.nick@gmail.com>2018-10-11 02:42:12 -0700
commit2074fa67fae8812baa0d62889bb332540c30778e (patch)
tree397fb9ec19931018d63d51c8dbb580aee24fd000
parentb277c10164b99a018de04c21af1b8c9ccea5779e (diff)
Improve comments & assign node
-rw-r--r--projects/1-colour-shift/main.js9
1 files changed, 7 insertions, 2 deletions
diff --git a/projects/1-colour-shift/main.js b/projects/1-colour-shift/main.js
index 53a5d4f..8fc7609 100644
--- a/projects/1-colour-shift/main.js
+++ b/projects/1-colour-shift/main.js
@@ -9,17 +9,22 @@ function computeRgb(x, y, width, height) {
// This will be an array of nodes since we are
// using class name (array may have length 0 or 1)
let attentionNodes
+// Make sure we wait until the page is ready.
+// Querying the DOM before the page is ready
+// will result in queries returning null/empty array
window.onload = function() {
- document.getElementsByClassName('attention')
+ attentionNodes = document.getElementsByClassName('attention')
}
document.onmousemove = function(event) {
- // iterate over each node and update its style
+ // calculate the color once (will be used
+ // for all nodes)
let color = computeRgb(
event.pageX,
event.pageY,
window.innerWidth,
window.innerHeight
)
+ // iterate over each node and update its style
for (let node of attentionNodes) {
node.style.color = color
}