cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="header">
  <h1>Waves</h1>
  <p>Waves generated with javascript and &lt;canvas&gt;.</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>
            
          
!
            
              *,*: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: #fcfae3;
}

.header {
  background-color: #22184c;
  // 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;
}
            
          
!
            
              (function(){
	"use strict";
	
	var cvs,ctx;
	//var nodes = 5;
	var waves = [];
	var waveHeight = 30;
	var colours = ["#fc0000","#00fa00","#0000e3"]; // #fcfae3
	
  // Initiator function
	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);
		}

    update();
	}

	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]);
		}
		ctx.globalCompositeOperation = "hue";
		ctx.fillStyle = fill;
    
    requestAnimationFrame(update);
		//ctx.fillRect(0,0,cvs.width,cvs.height);
	}

	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);
		waves.push(this);
	}

	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]),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.innerWidth > 1920) {
				canvas.width = window.innerWidth;
			}
			else {
				canvas.width = 1920;
			}
      
			canvas.height = waveHeight;
		}
	}
  
	document.addEventListener("DOMContentLoaded",init,false);
})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console