Public
Edited
Jan 11, 2024
Insert cell
<script>
mainDiv = document.getElementById("mainDiv");

// create new dom text elements
function divWithText(text) {
let div = document.createElement("div");
let textNode = document.createTextNode(text);
div.appendChild(textNode);
return div;
}

function numberText(v) {
let node = divWithText(String(Math.floor(v)));
// here we add a new update function to the node.
// It takes the previous v and adds 'amount' to it.
node.update = function (amount) {
v += amount;
node.textContent = String(Math.floor(v));
let x = v % 800;
let y = v % 50;
node.setAttribute("style", "position:absolute; " +
"left: " + x + "px; " +
"top: " + y % 50 + "px; " +
"color: hsl(" + x % 255 + ", 100%, 50%)");
};
node.update(0);
return node;
}

// updating the nodes
function tick() {
for (let node of nodes) {
node.update(1);
}
}

// running the animation
function tickForever() {
tick();
// a recursive call to this function executed by the browser on repaint
window.requestAnimationFrame(tickForever);
}

// initializing the nodes
nodes = [];
for (let i = 0; i < 400; i += 30) {
let node = numberText(i);
mainDiv.appendChild(node);
nodes.push(node);
if (i === 0)
console.log(node);
}

tickForever();

// if we did this in a while(true) loop it would run forever and eventually
// crash your browser, never showing you a result
// while (true) {
// tick();
// }

</script>

Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more