I have weird issue, it was working before, the issue is on CodePen. I have full size Canvas:
function width() {
// why -1 ?
// without this there is horizontal scrollbar
// I have no idea what is causing this
return window.innerWidth - 1;
}
// ---------------------------------------------------------------
function height() {
return window.innerHeight;
}
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = width();
canvas.height = height();
I'm not sure what is causing it but as you see I've added -1
to the width otherwise there is horizontal scrollbar. I think it was working before, it happen on GNU/Linux Chrome.
The canvas have display: block
, you can see the code at this Matrix Rain Demo, it works fine in debug mode.
Is this something with CodePen, anyone have a clue why this 1px.
This works fine in StackSnippets:
function width() {
return window.innerWidth;
}
// ---------------------------------------------------------------
function height() {
return window.innerHeight;
}
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = width();
canvas.height = height();
canvas {
display: block;
}
body {
margin: 0;
}
<canvas></canvas>
EDIT:
The problem appear only on certain with of the window. To see you need to resize slowly. For my (I have FullHD laptop) it appear for example at: 1594 but work at 1595. If I use -1 it the scrollbar never appear without it it flicker it shows up in some widths.
To be precise about the browser and OS I have Fedora 32 with Google Chrome stable (just updated to 84.0.4147.105 (Official version) (64-bit)
)
EDIT2:
My demo works in Stack Snippet no need to fix my demo:
var katagana = gen_unicode(0x30A1, 0x30F6);
var hiragana = gen_unicode(0x3041, 0x3096);
// ---------------------------------------------------------------
class Matrix {
constructor(canvas, { font_size = 14, width, height } = {}) {
this._canvas = canvas;
this._ctx = canvas.getContext('2d');
this._font_size = font_size;
this._drops = [];
this._columns = Math.floor(width / font_size);
this._chars = katagana.concat(hiragana);
this.resize(width, height);
}
random_char() {
return rnd(this._chars);
}
render_char(char, x, y) {
this._ctx.fillText(char, x, y);
}
start() {
let frames = 0;
this._run = true;
const self = this;
(function loop() {
if (frames++ % 2 === 0) {
self.render(); // slower render
}
if (self._run) {
requestAnimationFrame(loop);
}
})()
}
stop() {
this._run = false;
}
reset() {
for (let x = 0; x < this._columns; x++) {
this._drops[x] = 255;
}
}
resize(width, height) {
this._width = width;
this._height = height;
this._canvas.height = height;
this._canvas.width = width;
this.reset();
}
clear() {
this._ctx.fillStyle = 'rgba(0, 0,0,0.05)';
this._ctx.fillRect(0, 0, this._width, this._height);
this._ctx.fillStyle = '#0F0';
this._ctx.font = this._font_size + "px monospace";
}
render() {
this.clear();
for (let col = 0; col < this._drops.length; col++) {
const char = this.random_char();
const x = col * this._font_size;
const y = this._drops[col] * this._font_size;
this.render_char(char, x, y);
if (y > this._height && Math.random() > .975) {
this._drops[col] = 0;
}
this._drops[col]++;
}
}
}
// ---------------------------------------------------------------
// :: Init code
// ---------------------------------------------------------------
var canvas = document.getElementsByTagName('canvas')[0];
const matrix = new Matrix(canvas, {
font_size: 14,
width: width(),
height: height()
});
matrix.start();
window.addEventListener('resize', e => {
matrix.resize(width(), height());
});
// ---------------------------------------------------------------
// :: Utils
// ---------------------------------------------------------------
function gen_unicode(start, end) {
var chars = [];
for (var i = start; i <= end; ++i) {
chars.push(String.fromCharCode(i));
}
return chars;
}
// ---------------------------------------------------------------
function rnd(array) {
return array[Math.floor(Math.random() * array.length)]
}
// ---------------------------------------------------------------
function width() {
// why -1 ?
// without this there is horizontal scrollbar
// I have no idea what is causing this
return window.innerWidth;
}
// ---------------------------------------------------------------
function height() {
return window.innerHeight;
}
body {
background: black;
margin: 0;
min-width: 100vw;
min-height: 100vh;
}
canvas {
display: block;
margin: 0;
}
<canvas></canvas>
EDIT: the 1px was not the fix it happen with or without it.