From 2074fa67fae8812baa0d62889bb332540c30778e Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Thu, 11 Oct 2018 02:42:12 -0700 Subject: Improve comments & assign node --- projects/1-colour-shift/main.js | 9 +++++++-- 1 file 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 } -- cgit v1.2.3