A Pen by Charles Ojukwu on CodePen.
Created
June 23, 2018 15:16
-
-
Save e1blue/fdaeff23770c9dc9f719502a8e41d0bf to your computer and use it in GitHub Desktop.
Wavy Header
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
| <div class="header"> | |
| <h1>Waves</h1> | |
| <p>Waves generated with javascript and <canvas>.</p> | |
| </div> | |
| <div class="canvas-wrap"> | |
| <canvas id="canvas"></canvas> | |
| </div> | |
| <div class="content"> | |
| <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate rerum nemo, autem. Maiores quibusdam asperiores iste laboriosam deleniti quo ratione voluptatem expedita temporibus facere velit cum nesciunt, doloremque ducimus consequuntur numquam, accusamus porro a quam. Perspiciatis quo voluptates dolores error temporibus optio, placeat minus deserunt accusamus, doloribus repellat sed ut!</p> | |
| <p>Beatae ullam repellendus tempore, dicta excepturi necessitatibus magnam praesentium illum voluptates nihil, fugiat est, corporis. Reiciendis veritatis facere reprehenderit nemo quod temporibus ea assumenda nulla iure, mollitia beatae debitis consequuntur a blanditiis quasi suscipit eos ab repellendus deleniti fuga accusamus officia maxime perferendis modi! Fugiat libero incidunt sunt nostrum harum?</p> | |
| <p>Quis sit debitis nulla natus neque earum odit nihil sequi doloremque eveniet vel vitae doloribus numquam expedita quaerat ex inventore, optio a ipsum ducimus maiores quisquam recusandae? Officia dolore id quisquam perferendis iste officiis sint fugiat temporibus sequi provident commodi deserunt repellendus enim rerum in iusto nulla, rem facere nemo!</p> | |
| <p>Pariatur placeat cumque quibusdam incidunt cupiditate quae alias autem quo consectetur cum labore aliquid laudantium, explicabo suscipit nihil possimus est et ipsum animi veritatis! Consequatur ipsa ipsam, facilis vel odit inventore nemo minus ab nisi aut voluptas modi possimus. Voluptatum est alias eaque, odio in dolorum, laboriosam quos officia rem.</p> | |
| </div> |
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
| (function(){ | |
| "use strict"; | |
| var cvs,ctx; | |
| //var nodes = 5; | |
| var waves = []; | |
| var waveHeight = 30; | |
| var colours = ["#f00","#0f0","#00f"] | |
| function init() { | |
| cvs = document.getElementById("canvas"); | |
| ctx = cvs.getContext("2d"); | |
| resizeCanvas(cvs); | |
| for (var i = 0; i < 3; i++) { | |
| var temp = new wave(colours[i],1,5); | |
| } | |
| setInterval(update,16); | |
| } | |
| function randomColour() { | |
| // body... | |
| var h = Math.round(Math.random()*360); | |
| return "hsl("+h+",100%,50%)"; | |
| } | |
| function update(array) { | |
| // body... | |
| //ctx.clearRect(0, 0, cvs.width, cvs.height); | |
| 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]); | |
| //drawLine(waves[i].nodes); | |
| //drawNodes(waves[i].nodes); | |
| } | |
| ctx.globalCompositeOperation = "hue"; | |
| ctx.fillStyle = fill; | |
| //ctx.fillRect(0,0,cvs.width,cvs.height); | |
| } | |
| function wave(colour,lambda,nodes) { | |
| // body... | |
| 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.speed*plusOrMinus | |
| this.nodes.push(temp); | |
| console.log(temp); | |
| } | |
| console.log(this.nodes); | |
| waves.push(this); | |
| } | |
| function bounce(node) { | |
| node[1] = waveHeight/2*Math.sin(node[2]/20)+cvs.height/2; | |
| node[2] = node[2] + node[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]),diff(obj.nodes[i][1],obj.nodes[i+1][1]) | |
| ); | |
| }else{ | |
| ctx.lineTo(obj.nodes[i][0],obj.nodes[i][1]); | |
| ctx.lineTo(cvs.width,cvs.height); | |
| } | |
| } | |
| ctx.closePath(); | |
| ctx.fill(); | |
| } | |
| function drawNodes (array) { | |
| ctx.strokeStyle = "#888"; | |
| for (var i = 0; i < array.length; i++) { | |
| ctx.beginPath(); | |
| ctx.arc(array[i][0],array[i][1],4,0,2*Math.PI); | |
| ctx.closePath(); | |
| ctx.stroke(); | |
| } | |
| } | |
| function drawLine (array) { | |
| ctx.strokeStyle = "#888"; | |
| for (var i = 0; i < array.length; i++) { | |
| if (array[i+1]) { | |
| ctx.lineTo(array[i+1][0],array[i+1][1]); | |
| } | |
| } | |
| ctx.stroke(); | |
| } | |
| function resizeCanvas(canvas,width,height) { | |
| if (width && height) { | |
| canvas.width = width; | |
| canvas.height = height; | |
| } else { | |
| if (window.innerHeight > 1920) { | |
| canvas.width = window.innerWidth; | |
| } | |
| else { | |
| canvas.width = 1920; | |
| } | |
| canvas.height = waveHeight; | |
| } | |
| } | |
| document.addEventListener("DOMContentLoaded",init,false); | |
| })(); |
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
| *,*:before,*:after { | |
| box-sizing: border-box; | |
| } | |
| html { | |
| font-family: "Lato", sans-serif; | |
| color: #333; | |
| font-size: 20px; | |
| line-height: 1.5; | |
| font-weight: 300; | |
| } | |
| body { | |
| margin: 0; | |
| padding: 0; | |
| background: #fff; | |
| } | |
| .header { | |
| background-color: #215; | |
| background-image: linear-gradient(to bottom, rgba(#715, .5), transparent); | |
| mmin-height: 200px; | |
| padding: 2rem; | |
| color: #fff; | |
| text-align: center; | |
| } | |
| .content { | |
| min-height: 200px; | |
| padding: 2rem; | |
| margin: 0 auto; | |
| max-width: 900px; | |
| } | |
| .canvas-wrap { | |
| max-width: 100%; | |
| overflow: hidden; | |
| position: relative; | |
| } | |
| canvas { | |
| display: block; | |
| } |
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
| <link href="https://fonts.googleapis.com/css?family=Lato:300,400,600" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment