<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html lang='en' class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/Robpayot/pen/KNOeeE" />
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css'>
<style class="cp-pen-styles">undefined</style></head><body>
<canvas class="wave-xp"></canvas>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.1/dat.gui.min.js'></script>
<script >// Create context
var canvas = document.querySelector('canvas');
var ctx = document.querySelector('canvas').getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Set waves opacities
var wavesOpacities = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1];
// Set parameters
var params = {
AMPLITUDE_WAVES: canvas.height,
AMPLITUDE_MIDDLE: canvas.height / 3,
AMPLITUDE_SIDES: canvas.height / 2,
OFFSET_SPEED: 120,
SPEED: 3,
OFFSET_WAVES: 35,
NUMBER_WAVES: 3,
COLOR: '#032bac',
NUMBER_CURVES: 2,
OFFSET_CURVE: true,
RESET: false
};
var speedInc = 0;
// Set gradient colors
// Convert Hex to RGB for gradient
var hexToRgb = function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
};
var rgb = hexToRgb(params.COLOR);
var gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 0)');
gradient.addColorStop(1, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 1)');
// Render
var render = function render() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// For each wave
for (var j = params.NUMBER_WAVES - 1; j >= 0; j--) {if (window.CP.shouldStopExecution(2)){break;}if (window.CP.shouldStopExecution(2)){break;}
// offset between waves
var offset = speedInc + j * Math.PI * params.OFFSET_WAVES;
// Color and increase gradually opacity
if (j === 0) {
ctx.fillStyle = gradient;
} else {
ctx.fillStyle = params.COLOR;
}
ctx.globalAlpha = wavesOpacities[j];
// Oscillations
// Define heights cubicBezier amplitudes
// Speed amplitude variation between 0 and AMPLITUDE_SIDES ( half height window)
// Set height amplitude of borders points (left and right of the window) -> no offset here
var leftRange = (Math.sin(offset / params.OFFSET_SPEED + 2) + 1) / 2 * params.AMPLITUDE_SIDES + (canvas.height - params.AMPLITUDE_SIDES) / 2;
var rightRange = (Math.sin(offset / params.OFFSET_SPEED + 2) + 1) / 2 * params.AMPLITUDE_SIDES + (canvas.height - params.AMPLITUDE_SIDES) / 2;
// Speed amplitude variation between 0 and AMPLITUDE_WAVES ( height window)
// Set height amplitude of the first and second points of a curve
var leftCurveRange = (Math.sin(offset / params.OFFSET_SPEED + 2) + 1) / 2 * params.AMPLITUDE_WAVES + (canvas.height - params.AMPLITUDE_WAVES) / 2;
var rightCurveRange = (Math.sin(offset / params.OFFSET_SPEED + 1) + 1) / 2 * params.AMPLITUDE_WAVES + (canvas.height - params.AMPLITUDE_WAVES) / 2;
// Speed amplitude variation between 0 and AMPLITUDE_MIDDLE
// Set height amplitude of the last point of a curve
var endCurveRange = (Math.sin(offset / params.OFFSET_SPEED + 2) + 1) / 2 * params.AMPLITUDE_MIDDLE + (canvas.height - params.AMPLITUDE_MIDDLE) / 2;
// Reverse amplitude of the first and second points of a curve (only needed with 3 curves or more)
var reverseLeftCurveRange = endCurveRange - rightCurveRange + endCurveRange;
var reverseRightCurveRange = endCurveRange - leftCurveRange + endCurveRange;
// Neutralise curves first and second point amplitude
if (params.OFFSET_CURVE === false) {
leftCurveRange = rightCurveRange;
reverseRightCurveRange = reverseLeftCurveRange;
}
// Draw and fill path
ctx.beginPath();
// Draw first point from Left
ctx.moveTo(0, leftRange);
// Draw bezier curves based on amplitude
// Draw each points of the first curve
// bezierCurveTo() see https://www.w3schools.com/TAGs/canvas_beziercurveto.asp
ctx.bezierCurveTo(canvas.width / (params.NUMBER_CURVES * 3), leftCurveRange, canvas.width / (params.NUMBER_CURVES * 3 / 2), rightCurveRange, canvas.width / params.NUMBER_CURVES, endCurveRange);
// Draw each points of other curves if needed
for (var i = 1; i < params.NUMBER_CURVES; i++) {if (window.CP.shouldStopExecution(1)){break;}if (window.CP.shouldStopExecution(1)){break;}
// Reverse waves amplitude 1 / 2 times
var finalRightCurveRange = i % 2 !== 0 ? rightCurveRange : reverseRightCurveRange;
var finalLeftCurveRange = i % 2 !== 0 ? leftCurveRange : reverseLeftCurveRange;
// Set points curve
var secondPtX = canvas.width * (i / params.NUMBER_CURVES) + canvas.width / (params.NUMBER_CURVES * 3);
var secondPtY = endCurveRange - finalRightCurveRange + endCurveRange;
var thirdPtX = canvas.width * (i / params.NUMBER_CURVES) + canvas.width * (2 / (params.NUMBER_CURVES * 3));
var thirdPtY = endCurveRange - finalLeftCurveRange + endCurveRange;
var lastPtX = canvas.width * ((i + 1) / params.NUMBER_CURVES);
var lastPtY = i === params.NUMBER_CURVES - 1 ? rightRange : endCurveRange;
ctx.bezierCurveTo(secondPtX, secondPtY, thirdPtX, thirdPtY, lastPtX, lastPtY);
}
window.CP.exitedLoop(1);
window.CP.exitedLoop(1);
// Draw last lines
ctx.lineTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
ctx.lineTo(0, rightRange);
ctx.closePath();
ctx.fill();
}
window.CP.exitedLoop(2);
window.CP.exitedLoop(2);
// Speed
speedInc += params.SPEED;
};
// RAF
TweenMax.ticker.addEventListener('tick', render);
// GUI
var gui = new dat.GUI();
gui.add(params, 'AMPLITUDE_WAVES', 0, canvas.height);
gui.add(params, 'AMPLITUDE_MIDDLE', 0, canvas.height * 0.6);
gui.add(params, 'AMPLITUDE_SIDES', 0, canvas.height * 0.5);
var changeColor = gui.addColor(params, 'COLOR');
gui.add(params, 'NUMBER_WAVES', 0, 8).step(1);
gui.add(params, 'NUMBER_CURVES', 1, 8).step(1);
gui.add(params, 'SPEED', 0, 10);
gui.add(params, 'OFFSET_WAVES', 0, 155);
gui.add(params, 'OFFSET_CURVE');
var reset = gui.add(params, 'RESET');
changeColor.onChange(function (colorValue) {
rgb = hexToRgb(colorValue);
gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 0)');
gradient.addColorStop(1, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 1)');
});
reset.onChange(function (resetValue) {
params.AMPLITUDE_WAVES = canvas.height;
params.AMPLITUDE_MIDDLE = canvas.height / 3;
params.AMPLITUDE_SIDES = canvas.height / 2;
params.OFFSET_SPEED = 120;
params.SPEED = 3;
params.OFFSET_WAVES = 35;
params.NUMBER_WAVES = 3;
params.COLOR = '#032bac';
params.NUMBER_CURVES = 2;
params.OFFSET_CURVE = true;
params.RESET = false;
rgb = hexToRgb(params.COLOR);
gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 0)');
gradient.addColorStop(1, 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', 1)');
});
//# sourceURL=pen.js
</script>
</body></html>