css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h2>Hover over the rectangle</h2>
<div id="canvasCont">
  <canvas id="Canvas2"></canvas> 
  <canvas id="Canvas1"></canvas>
</div>

<div id="link">
  
  <button id="clear">Clear</button>

  * Based on:
  <a href="https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm" target="blank">2D water</a>
</div>

            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono');

* {
    margin: 0 auto;
}

body {
  font-family: 'Share Tech Mono', monospace;
  background: #000;
  color: #ffffff;
  text-align: center;
}

#link {
  margin-top: 15px;
  width: 964px;
  text-align: left;
}

#clear {
  float: right;
  margin-left: 10px;
}

#canvasCont {
  position: relative;
  width: 960px;
  height: 320px;
  background-size: 200%;
  background-position: right 20px;
  border: 2px #ffffff solid;
  overflow: hidden;
  cursor: pointer;
}

#Canvas1 {
  positon: absolute;
  top: 0px;
  left:0;
  background: rgba(0,0,100,0.8);
  display: block;
  width: 960px;
  height: 320px;
}

#Canvas2 {
  position: absolute;
  top: 0px;
  left:0;
  background: rgba(255,255,255,0.3);
  display: block;
  width: 960px;
  height: 320px;
}
            
          
!
            
              var dropSize = 2;

var cwidth = 480;
var cheight = 160;

var cont = document.getElementById('canvasCont');
var c1 = document.getElementById("Canvas1");
var c2 = document.getElementById("Canvas2");

c1.width = cwidth;
c1.height = cheight;
c2.width = cwidth;
c2.height = cheight;

var ctx1 = c1.getContext("2d");
var ctx2 = c2.getContext("2d");

var imgData1 = ctx1.getImageData(0, 0, cwidth, cheight);
var Data1 = imgData1.data;
var imgData2 = ctx2.getImageData(0, 0, cwidth, cheight);
var Data2 = imgData2.data;

var buffer1 = [];
var buffer2 = [];
var damp = 0.995;

document.getElementById('clear').onclick = function() { clear(); }
clear();
function clear() {
  
 for (x = 0 ; x < cwidth ; x++) {

    buffer1[x] = [];
    buffer2[x] = [];
  
    for (y = 0 ; y < cheight ; y++) {
     
      buffer1[x][y] = 0;
	    buffer2[x][y] = 0;
      
 	    Data1[0 + (4 * (x + (y * cwidth)))] = ((Math.floor((x+y) / 2) == ((x+y) / 2)) ? Math.floor(8 + Math.random() * 8) : 0);
 	    Data1[1 + (4 * (x + (y * cwidth)))] = ((Math.floor((x+y) / 2) == ((x+y) / 2)) ? Math.floor(16 + Math.random() * 16) : 0);
 	    Data1[2 + (4 * (x + (y * cwidth)))] = ((Math.floor((x+y) / 2) == ((x+y) / 2)) ? Math.floor(32 + Math.random() * 32) : 0);
 	    Data1[3 + (4 * (x + (y * cwidth)))] = 255;
  
      Data2[0 + (4 * (x + (y * cwidth)))] = Data1[0 + (4 * (x + (y * cwidth)))]
      Data2[1 + (4 * (x + (y * cwidth)))] = Data1[1 + (4 * (x + (y * cwidth)))]
      Data2[2 + (4 * (x + (y * cwidth)))] = Data1[2 + (4 * (x + (y * cwidth)))]
      Data2[3 + (4 * (x + (y * cwidth)))] = Data1[3 + (4 * (x + (y * cwidth)))]
    }
  } 
  ctx1.putImageData(imgData1, 0, 0);
  ctx2.putImageData(imgData2, 0, 0);
  cont.style.backgroundImage = "url('" + c1.toDataURL() + "')";
}

var lastRun = 0;
Run();
function Run() {
  
	for (x = 1 ; x < cwidth-1 ; x++) {
	  for (y = 1 ; y < cheight-1 ; y++) {
      
     	buffer2[x][y] = (((
        buffer1[x+1][y] +
        buffer1[x-1][y] +
      	buffer1[x][y+1] +
       	buffer1[x][y-1]
      ) / 2) - buffer2[x][y]) * damp;
      
      Data1[3 + (4 * (x + (y * cwidth)))] = Math.round(255 - (buffer2[x][y]));    
      Data2[3 + (4 * (x + (y * cwidth)))] = Math.round(255 - Math.max(0, (buffer2[x][y] - buffer1[x][y])));    
    } 	
  }
  
  var tampbuffer = buffer1;
	buffer1 = buffer2;
	buffer2 = tampbuffer;
  
  ctx1.putImageData(imgData1, 0, 0);
  ctx2.putImageData(imgData2, 0, 0);
	
  requestAnimationFrame(Run);
}

document.getElementById('canvasCont').addEventListener('click', drop)
document.getElementById('canvasCont').addEventListener('mousemove', drop)

function drop(e) {

  var rect = e.target.getBoundingClientRect();
  var clientX = Math.round((e.clientX - rect.left) / 2);
  var clientY = Math.round((e.clientY - rect.top) / 2);

  for (i = 0-dropSize ; i <= dropSize ; i++) {
	  for (j = 0-dropSize ; j <= dropSize ; j++) {
    
    	var nX = Math.min((cwidth - 1), Math.max(1, (clientX + i)))
    	var nY = Math.min((cheight - 1), Math.max(1, (clientY + j)))

			buffer1[nX][nY] = 255;    
    } 	
  }
}


            
          
!
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