Created
February 8, 2020 05:09
-
-
Save rleaf/62b8683630efd7fe6271173ca02ec4d9 to your computer and use it in GitHub Desktop.
Background Noise - Canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<main role="main" class="main-content"> | |
<canvas id="noise" class="noise"></canvas> | |
<h1 class="main-title"> | |
Background Noise | |
</h1> | |
<p> | |
<a href="https://codepen.io/IbeVanmeenen/pen/awjyPV" class="link" target="_blank"> | |
Alternative: Background image and CSS Animation | |
</a> | |
</p> | |
</main> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const noise = () => { | |
let canvas, ctx; | |
let wWidth, wHeight; | |
let noiseData = []; | |
let frame = 0; | |
let loopTimeout; | |
// Create Noise | |
const createNoise = () => { | |
const idata = ctx.createImageData(wWidth, wHeight); | |
const buffer32 = new Uint32Array(idata.data.buffer); | |
const len = buffer32.length; | |
for (let i = 0; i < len; i++) { | |
if (Math.random() < 0.5) { | |
buffer32[i] = 0xff000000; | |
} | |
} | |
noiseData.push(idata); | |
}; | |
// Play Noise | |
const paintNoise = () => { | |
if (frame === 9) { | |
frame = 0; | |
} else { | |
frame++; | |
} | |
ctx.putImageData(noiseData[frame], 0, 0); | |
}; | |
// Loop | |
const loop = () => { | |
paintNoise(frame); | |
loopTimeout = window.setTimeout(() => { | |
window.requestAnimationFrame(loop); | |
}, (1000 / 25)); | |
}; | |
// Setup | |
const setup = () => { | |
wWidth = window.innerWidth; | |
wHeight = window.innerHeight; | |
canvas.width = wWidth; | |
canvas.height = wHeight; | |
for (let i = 0; i < 10; i++) { | |
createNoise(); | |
} | |
loop(); | |
}; | |
// Reset | |
let resizeThrottle; | |
const reset = () => { | |
window.addEventListener('resize', () => { | |
window.clearTimeout(resizeThrottle); | |
resizeThrottle = window.setTimeout(() => { | |
window.clearTimeout(loopTimeout); | |
setup(); | |
}, 200); | |
}, false); | |
}; | |
// Init | |
const init = (() => { | |
canvas = document.getElementById('noise'); | |
ctx = canvas.getContext('2d'); | |
setup(); | |
})(); | |
}; | |
noise(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.main-content { | |
position: relative; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
flex-flow: column; | |
height: 100vh; | |
background: #673ab7; | |
color: #fff; | |
text-align: center; | |
} | |
.noise { | |
z-index: 100; | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
pointer-events: none; | |
opacity: .05; | |
} | |
.main-title { | |
padding: .3em 1em .25em; | |
font-weight: 400; | |
font-size: 3vw; | |
line-height: 1; | |
text-transform: uppercase; | |
letter-spacing: .15em; | |
color: #4c2298; | |
background: #794fc5; | |
} | |
.link { | |
display: inline-block; | |
padding: 1em 1em; | |
color: #4c2298; | |
line-height: 1; | |
background: #794fc5; | |
text-decoration: none; | |
transition: color .3s ease, background .3s ease; | |
&:hover, &:focus { | |
background: #4c2298; | |
color: #794fc5; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With resize empty the matrix