Skip to content

Instantly share code, notes, and snippets.

@johnbeech
Last active August 20, 2020 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbeech/8e5559c83bc9d9018c5c1c9cab368803 to your computer and use it in GitHub Desktop.
Save johnbeech/8e5559c83bc9d9018c5c1c9cab368803 to your computer and use it in GitHub Desktop.
Turn a page into matrix scrolling code.
var canvas = document.createElement('canvas')
document.body.appendChild(canvas)
canvas.style.position = 'absolute';
canvas.style.width = '100vw';
canvas.style.height = '100vh';
canvas.style.top = 0;
canvas.style.left = 0;
canvas.style.zindex = 100;
var ctx = canvas.getContext('2d');
var columns = [];
var chars = [];
canvas.height = window.screen.height;
canvas.width = window.screen.width;
ctx.translate(canvas.width, 0);
ctx.scale(-1, 1);
for (i = 0; i < 256; columns[i] = 1, chars[i++] = '゠'); // aka 12448
ctx.shadowBlur = 2;
function step() {
ctx.fillStyle = 'rgba(0,0,0,0.05)';
ctx.shadowColor = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
columns.map(function (value, index) {
// overwrites the previous light-green char with a green one
ctx.fillStyle = ctx.shadowColor = '#000';
ctx.fillRect(index * 10, value - 10, 10, 10);
ctx.fillStyle = ctx.shadowColor = '#0F0';
ctx.fillText(chars[index], index * 10, value - 10);
columns[index] = value > 758 + Math.random() * 1e4 ? 0 : value + 10;
chars[index] = String.fromCharCode(12448 + Math.random() * 96);
ctx.fillStyle = ctx.shadowColor = '#AFA';
ctx.fillText(chars[index], index * 10, value);
});
}
//1000/33 = ~30 times a second
setInterval(step, 33);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment