Header Waves
by Faisal Khan Janjua
HTML
<div class="header">
<h1>Animated waves...</h1>
</div>
<div class="canvas-wrap">
<canvas id="canvas"></canvas>
</div>
<div class="content">
<h1>Animated waves...</h1>
</div>
<script id="rendered-js">
(function () {
"use strict";
var cvs, ctx;
var nodes = 6;
var waves = [];
var waveHeight = 60;
var colours = ["#f80000", "#00f800", "#0000f8"];
// Initiator function
function init() {
cvs = document.getElementById("canvas");
ctx = cvs.getContext("2d");
resizeCanvas(cvs);
for (var i = 0; i < 3; i++) {
waves.push(new wave(colours[i], 1, nodes));
}
update();
}
function update() {
var fill = window.getComputedStyle(document.querySelector(".header"), null).getPropertyValue("background-color");
ctx.fillStyle = fill;
ctx.globalCompositeOperation = "source-over";
ctx.fillRect(0, 0, cvs.width, cvs.height);
ctx.globalCompositeOperation = "screen";
for (var i = 0; i < waves.length; i++) {
for (var j = 0; j < waves[i].nodes.length; j++) {
bounce(waves[i].nodes[j]);
}
drawWave(waves[i]);
}
ctx.fillStyle = fill;
requestAnimationFrame(update);
}
function wave(colour, lambda, nodes) {
this.colour = colour;
this.lambda = lambda;
this.nodes = [];
var tick = 1;
for (var i = 0; i <= nodes + 2; i++) {
var temp = [(i - 1) * cvs.width / nodes, 0, Math.random() * 200, .3];
this.nodes.push(temp);
console.log(temp);
}
console.log(this.nodes);
}
function bounce(nodeArr) {
nodeArr[1] = waveHeight / 2 * Math.sin(nodeArr[2] / 20) + cvs.height / 2;
nodeArr[2] = nodeArr[2] + nodeArr[3];
}
function drawWave(obj) {
var diff = function (a, b) {
return (b - a) / 2 + a;
};
ctx.fillStyle = obj.colour;
ctx.beginPath();
ctx.moveTo(0, cvs.height);
ctx.lineTo(obj.nodes[0][0], obj.nodes[0][1]);
for (var i = 0; i < obj.nodes.length; i++) {
if (obj.nodes[i + 1]) {
ctx.quadraticCurveTo(
obj.nodes[i][0], obj.nodes[i][1],
diff(obj.nodes[i][0], obj.nodes[i + 1][0]),...
SCSS
$primary: #22184c;
*,*::before,*::after {
box-sizing: border-box;
}
html {
font-family: "Work Sans", sans-serif;
color: #333;
font-size: 18px;
line-height: 1.5;
font-weight: 300;
}
body {
margin: 0;
padding: 0;
background: #f8f8f8;
}
.header,
.content {
min-height: 100px;
height: 50vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.header {
background-color: $primary;
background-image: linear-gradient(to bottom, rgba(#715, 0.5), transparent);
color: #ddf;
}
.content {
color: $primary;
}
h1 {
font-size: 3rem;
letter-spacing: 0.1ch;
font-weight: 300;
margin: 0;
opacity: 0.5;
}
.canvas-wrap {
max-width: 100%;
overflow: hidden;
position: absolute;
transform: translateY(-50%);
}
canvas {
display: block;
}