2026-03-28 19:10:45 +01:00
|
|
|
---
|
|
|
|
|
title: Matrix
|
|
|
|
|
tags:
|
|
|
|
|
- demoExample
|
|
|
|
|
---
|
|
|
|
|
{% layout 'hippie/simple.liquid' %}
|
|
|
|
|
|
|
|
|
|
{% block style %}
|
|
|
|
|
<style>
|
|
|
|
|
html, body {
|
2026-03-28 19:44:07 +01:00
|
|
|
height: 100vh;
|
2026-03-28 19:10:45 +01:00
|
|
|
box-sizing: border-box;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body {
|
2026-03-28 19:44:07 +01:00
|
|
|
margin: 0;
|
2026-03-28 19:10:45 +01:00
|
|
|
background-color: grey;
|
2026-03-28 19:44:07 +01:00
|
|
|
font-family: 'Courier New', Courier, monospace;
|
2026-03-28 19:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
#canvas {
|
|
|
|
|
display: block;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
2026-03-28 19:10:45 +01:00
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
{% block body %}
|
2026-03-28 19:44:07 +01:00
|
|
|
<canvas id="canvas"></canvas>
|
2026-03-28 19:10:45 +01:00
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
{% block script %}
|
|
|
|
|
<script>
|
|
|
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2026-03-28 19:44:07 +01:00
|
|
|
const canvas = document.getElementById("canvas");
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
2026-03-28 19:53:49 +01:00
|
|
|
// const observer = new ResizeObserver(() => {
|
|
|
|
|
// canvas.width = canvas.clientWidth;
|
|
|
|
|
// canvas.height = canvas.clientHeight;
|
|
|
|
|
// console.log("resize");
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
canvas.width = canvas.clientWidth;
|
|
|
|
|
canvas.height = canvas.clientHeight;
|
|
|
|
|
|
|
|
|
|
// observer.observe(canvas);
|
2026-03-28 19:44:07 +01:00
|
|
|
|
|
|
|
|
ctx.font = "24px 'Courier New', Courier, monospace";
|
2026-03-28 19:53:49 +01:00
|
|
|
ctx.textBaseline = "top";
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
let char = characters.charAt(Math.floor(Math.random() * characters.length));
|
|
|
|
|
let charMeasure = ctx.measureText(char);
|
2026-03-28 19:49:13 +01:00
|
|
|
let charW = Math.ceil(Math.max(charMeasure.actualBoundingBoxLeft + charMeasure.actualBoundingBoxRight, charMeasure.width));
|
|
|
|
|
let charH = charMeasure.fontBoundingBoxDescent + charMeasure.fontBoundingBoxAscent;
|
2026-03-28 19:44:07 +01:00
|
|
|
let glyph = {
|
|
|
|
|
text: char,
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
2026-03-28 19:53:49 +01:00
|
|
|
w: charH,
|
|
|
|
|
h: charH
|
2026-03-28 19:10:45 +01:00
|
|
|
}
|
2026-03-28 19:44:07 +01:00
|
|
|
let lane = [];
|
2026-03-28 19:49:13 +01:00
|
|
|
let max = Math.floor(canvas.clientHeight / glyph.h);
|
|
|
|
|
|
|
|
|
|
let newId = undefined;
|
|
|
|
|
let holdId = undefined;
|
|
|
|
|
let cleanId = undefined;
|
|
|
|
|
let newInterval = 300;
|
|
|
|
|
let holdInterval = 1000;
|
|
|
|
|
let cleanInterval = 400;
|
|
|
|
|
let newIndex = 0;
|
|
|
|
|
let cleanIndex = randomIntFrom(1, max);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
console.log("init", newInterval);
|
|
|
|
|
console.log(glyph);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
newId = setInterval(newTrail, newInterval);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
function newTrail() {
|
|
|
|
|
clearInterval(holdId);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
glyph.text = characters.charAt(Math.floor(Math.random() * characters.length));
|
2026-03-28 19:49:13 +01:00
|
|
|
console.log(newIndex, glyph.text);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
lane.push([newIndex, glyph.text, glyph.y]);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
ctx.fillStyle = "white";
|
|
|
|
|
ctx.fillRect(glyph.x, glyph.y, glyph.w, glyph.h);
|
2026-03-28 19:53:49 +01:00
|
|
|
ctx.fillStyle = "black";
|
|
|
|
|
ctx.fillText(glyph.text, Math.floor((charH - charW) / 2), glyph.y);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
if (newIndex > 0) {
|
2026-03-28 19:44:07 +01:00
|
|
|
let prevY = glyph.y - glyph.h;
|
2026-03-28 19:53:49 +01:00
|
|
|
let alpha = randomBetween(.2, 1);
|
|
|
|
|
ctx.clearRect(glyph.x, prevY, glyph.w, glyph.h);
|
|
|
|
|
ctx.fillStyle = "rgba(0, 0, 0, " + alpha + ")";
|
2026-03-28 19:44:07 +01:00
|
|
|
ctx.fillRect(glyph.x, prevY, glyph.w, glyph.h);
|
2026-03-28 19:53:49 +01:00
|
|
|
ctx.fillStyle = "rgba(255, 255, 255, " + alpha + ")";
|
|
|
|
|
// TODO: can not use array lane if it is changed by cleanTrail()
|
|
|
|
|
ctx.fillText(lane[newIndex - 1][1], Math.floor((charH - charW) / 2), prevY);
|
2026-03-28 19:44:07 +01:00
|
|
|
}
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
if (newIndex === cleanIndex) {
|
|
|
|
|
console.log("clean", cleanInterval, cleanIndex);
|
|
|
|
|
|
|
|
|
|
cleanId = setInterval(cleanTrail, cleanInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newIndex++;
|
2026-03-28 19:44:07 +01:00
|
|
|
glyph.y += glyph.h;
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
if (newIndex > max) {
|
|
|
|
|
// console.log("hold", holdInterval);
|
|
|
|
|
console.log("end");
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
clearInterval(newId);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
newIndex = 0;
|
2026-03-28 19:44:07 +01:00
|
|
|
glyph.y = 0;
|
|
|
|
|
newInterval = randomIntFrom(100, 1000, 2);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
// holdId = setTimeout(() => {
|
|
|
|
|
// console.log("clean", cleanInterval);
|
2026-03-28 19:10:45 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
// holdInterval = randomIntFrom(1000, 10000, 3);
|
|
|
|
|
// cleanId = setInterval(cleanTrail, cleanInterval);
|
|
|
|
|
// }, holdInterval);
|
2026-03-28 19:10:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
function cleanTrail() {
|
|
|
|
|
let pos = lane.shift();
|
|
|
|
|
console.log(pos);
|
2026-03-28 19:21:59 +01:00
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
ctx.clearRect(0, pos[2], glyph.w, glyph.h);
|
|
|
|
|
|
|
|
|
|
if (!lane.length) {
|
2026-03-28 19:21:59 +01:00
|
|
|
console.log("hold", holdInterval);
|
|
|
|
|
|
2026-03-28 19:44:07 +01:00
|
|
|
clearInterval(cleanId);
|
|
|
|
|
|
2026-03-28 19:21:59 +01:00
|
|
|
holdId = setTimeout(() => {
|
2026-03-28 19:44:07 +01:00
|
|
|
console.clear();
|
|
|
|
|
console.log("new", newInterval);
|
2026-03-28 19:21:59 +01:00
|
|
|
|
2026-03-28 19:49:13 +01:00
|
|
|
cleanIndex = randomIntFrom(1, max);
|
2026-03-28 19:44:07 +01:00
|
|
|
cleanInterval = randomIntFrom(1000, 10000, 3);
|
2026-03-28 19:49:13 +01:00
|
|
|
holdInterval = randomIntFrom(1000, 10000, 3);
|
2026-03-28 19:44:07 +01:00
|
|
|
newId = setInterval(newTrail, newInterval);
|
2026-03-28 19:21:59 +01:00
|
|
|
}, holdInterval);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-28 19:10:45 +01:00
|
|
|
|
|
|
|
|
function randomBetween(min, max) {
|
|
|
|
|
return (Math.random() * (max - min) + min).toFixed(2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 19:21:59 +01:00
|
|
|
function randomIntFrom(min, max, pos = 0) {
|
|
|
|
|
pos = Math.pow(10, pos);
|
2026-03-28 19:10:45 +01:00
|
|
|
min = Math.ceil(min);
|
|
|
|
|
max = Math.floor(max);
|
|
|
|
|
|
2026-03-28 19:21:59 +01:00
|
|
|
return Math.floor((Math.random() * (max - min + 1) + min) / pos) * pos;
|
2026-03-28 19:10:45 +01:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
{% endblock %}
|