'How do I overlay an image over a canvas
I have a canvas that shows matrix binary code raining down. I would like to add an image over the canvas in question
This is what I have
<div class="rain">
<canvas id="Matrix"></canvas>
<div class ="imgclass">
<img class="imgclass" src="assets/image.jpg"/>
</div>
</div>
JavaScript for the canvas
const canvas = document.getElementById('Matrix');
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const nums = '0123456789';
const alphabet = latin + nums;
const fontSize = 16;
const columns = canvas.width/fontSize;
const rainDrops = [];
for( let x = 0; x < columns; x++ ) {
rainDrops[x] = 1;
}
const draw = () => {
context.fillStyle = 'rgba(0, 0, 0, 0.05)';
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = '#0F0';
context.font = fontSize + 'px monospace';
for(let i = 0; i < rainDrops.length; i++)
{
const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length));
context.fillText(text, i*fontSize, rainDrops[i]*fontSize);
if(rainDrops[i]*fontSize > canvas.height && Math.random() > 0.975){
rainDrops[i] = 0;
}
rainDrops[i]++;
}
};
setInterval(draw, 30);
and the CSS:
.rain {
background: black;
height: 40%;
overflow: hidden;
}
canvas {
background-image: url("assets/img.jpg");
}
Right now the image in question appears below the canvas as opposed to on the canvas (ie. in the middle).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
