HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<canvas id= "c"> </canvas>
<button id="reset">new colours</button>
<div id="info">
<div id="top">
<a id="close" href="">close</a>
</div>
<p>
<br>
- Alter particles/groups with the GROUPS variable.<br><br>
- Customize colours with GROUP_COLOURS.<br><br>
- Use the mouse to move the fluid.<br><br>
- Click anywhere to make water.<br><br>
- Use one group for a more water-like effect.<br><br>
</p>
</div>
*{
margin: 0;
padding: 0;
overflow:hidden;
}
body {
background:#333;
}
#reset {
position: absolute;
left:50%;
top:10px;
margin-left:-400px;
background: #444;
border: 1px solid #555;
color: #888;
padding: 6px 10px;
cursor: pointer;
opacity: 0.8;
}
#c {
width:800px;
height:376px;
margin:0 auto;
display:block;
}
#info {
position:absolute;
left:-1px;
top:-1px;
width:auto;
max-width:380px;
height:auto;
background:#f2f2f2;
border-bottom-right-radius:10px;
}
#top {
background:#fff;
width:100%;
height:auto;
position:relative;
border-bottom:1px solid #eee;
}
p {
font-family:Arial, sans-serif;
color:#666;
text-align:justify;
font-size: 16px;
margin:10px;
}
a {
font-family:sans-serif;
color:#444;
text-decoration:none;
font-size: 20px;
}
#site {
float:left;
margin: 10px;
color: #38a;
}
#close {
float:right;
margin: 10px;
}
/*
Copyright (c) 2013 dissimulate at codepen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
document.getElementById('close').onmousedown = function(e) {
e.preventDefault();
document.getElementById('info').style.display = 'none';
return false;
};
/* Settings */
var MOUSE_INFLUENCE = 5,
GRAVITY_X = 0,
GRAVITY_Y = 0,
MOUSE_REPEL = false,
GROUPS = [50,50,50],
GROUP_COLOURS = ['rgba(97,160,232'];
window.requestAnimFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
var fluid = function() {
var ctx, width, height, num_x, num_y, particles, grid, meta_ctx, threshold = 220, play = false, spacing = 45, radius = 30, limit = radius * 0.66, textures, num_particles;
var mouse = {
down: false,
x: 0,
y: 0
};
var process_image = function() {
var imageData = meta_ctx.getImageData(0, 0, width, height),
pix = imageData.data;
for (var i = 0, n = pix.length; i < n; i += 4) {
(pix[i + 3] < threshold) && (pix[i + 3] /= 6);
}
ctx.putImageData(imageData, 0, 0);
};
var run = function () {
//var time = new Date().getTime();
meta_ctx.clearRect(0, 0, width, height);
for (var i = 0, l = num_x * num_y; i < l; i++) grid[i].length = 0;
var i = num_particles;
while(i--) particles[i].first_process();
i = num_particles;
while(i--) particles[i].second_process();
process_image();
if(mouse.down) {
ctx.canvas.style.cursor = 'none';
ctx.fillStyle = 'rgba(97, 160, 232, 0.05)';
ctx.beginPath();
ctx.arc(
mouse.x,
mouse.y,
radius * MOUSE_INFLUENCE,
0,
Math.PI * 2
);
ctx.closePath();
ctx.fill();
ctx.fillStyle = 'rgba(97, 160, 232, 0.05)';
ctx.beginPath();
ctx.arc(
mouse.x,
mouse.y,
(radius * MOUSE_INFLUENCE)/3,
0,
Math.PI * 2
);
ctx.closePath();
ctx.fill();
} else ctx.canvas.style.cursor = 'default';
//console.log(new Date().getTime() - time);
if(play)
requestAnimFrame(run);
};
var Particle = function (type, x, y) {
this.type = type;
this.x = x;
this.y = y;
this.px = x;
this.py = y;
this.vx = 0;
this.vy = 0;
};
Particle.prototype.first_process = function () {
var g = grid[Math.round(this.y / spacing) * num_x + Math.round(this.x / spacing)];
if (g) g.close[g.length++] = this;
this.vx = this.x - this.px;
this.vy = this.y - this.py;
if (mouse.down) {
var dist_x = this.x - mouse.x;
var dist_y = this.y - mouse.y;
var dist = Math.sqrt(dist_x * dist_x + dist_y * dist_y);
if (dist < radius * MOUSE_INFLUENCE) {
var cos = dist_x / dist;
var sin = dist_y / dist;
this.vx += (MOUSE_REPEL) ? cos : -cos;
this.vy += (MOUSE_REPEL) ? sin : -sin;
}
}
this.vx += GRAVITY_X;
this.vy += GRAVITY_Y;
this.px = this.x;
this.py = this.y;
this.x += this.vx;
this.y += this.vy;
};
Particle.prototype.second_process = function () {
var force = 0,
force_b = 0,
cell_x = Math.round(this.x / spacing),
cell_y = Math.round(this.y / spacing),
close = [];
for (var x_off = -1; x_off < 2; x_off++) {
for (var y_off = -1; y_off < 2; y_off++) {
var cell = grid[(cell_y + y_off) * num_x + (cell_x + x_off)];
if (cell && cell.length) {
for (var a = 0, l = cell.length; a < l; a++) {
var particle = cell.close[a];
if (particle != this) {
var dfx = particle.x - this.x;
var dfy = particle.y - this.y;
var distance = Math.sqrt(dfx * dfx + dfy * dfy);
if (distance < spacing) {
var m = 1 - (distance / spacing);
force += Math.pow(m, 2);
force_b += Math.pow(m, 3) / 2;
particle.m = m;
particle.dfx = (dfx / distance) * m;
particle.dfy = (dfy / distance) * m;
close.push(particle);
}
}
}
}
}
}
force = (force - 3) * 0.5;
for (var i = 0, l = close.length; i < l; i++) {
var neighbor = close[i];
var press = force + force_b * neighbor.m;
if (this.type != neighbor.type) press *= 0.35;
var dx = neighbor.dfx * press * 0.5;
var dy = neighbor.dfy * press * 0.5;
neighbor.x += dx;
neighbor.y += dy;
this.x -= dx;
this.y -= dy;
}
if (this.x < limit) this.x = limit;
else if (this.x > width - limit) this.x = width - limit;
if (this.y < limit) this.y = limit;
else if (this.y > height - limit) this.y = height - limit;
this.draw();
};
Particle.prototype.draw = function () {
var size = radius * 2;
meta_ctx.drawImage(
textures[this.type],
this.x - radius,
this.y - radius,
size,
size);
};
return {
init: function(canvas, w, h) {
particles = [];
grid = [];
close = [];
textures = [];
var canvas = document.getElementById(canvas);
ctx = canvas.getContext('2d');
canvas.height = h || window.innerHeight;
canvas.width = w || window.innerWidth;
width = canvas.width;
height = canvas.height;
var meta_canvas = document.createElement("canvas");
meta_canvas.width = width;
meta_canvas.height = height;
meta_ctx = meta_canvas.getContext("2d");
for(var i = 0; i < GROUPS.length; i++) {
var colour;
if(GROUP_COLOURS[i]) {
colour = GROUP_COLOURS[i];
} else {
colour =
'hsla(' + Math.round(Math.random() * 360) + ', 80%, 60%';
}
textures[i] = document.createElement("canvas");
textures[i].width = radius * 2;
textures[i].height = radius * 2;
var nctx = textures[i].getContext("2d");
var grad = nctx.createRadialGradient(
radius,
radius,
1,
radius,
radius,
radius
);
grad.addColorStop(0, colour + ',1)');
grad.addColorStop(1, colour + ',0)');
nctx.fillStyle = grad;
nctx.beginPath();
nctx.arc(radius, radius, radius, 0, Math.PI * 2, true);
nctx.closePath();
nctx.fill();
}
canvas.onmousedown = function(e) {
mouse.down = true;
return false;
};
canvas.onmouseup = function(e) {
mouse.down = false;
return false;
};
canvas.onmousemove = function(e) {
var rect = canvas.getBoundingClientRect();
mouse.x = e.clientX - rect.left;
mouse.y = e.clientY - rect.top;
return false;
};
num_x = Math.round(width / spacing) + 1;
num_y = Math.round(height / spacing) + 1;
for (var i = 0; i < num_x * num_y; i++) {
grid[i] = {
length: 0,
close: []
}
}
for (var i = 0; i < GROUPS.length; i++ ) {
for (var k = 0; k < GROUPS[i]; k++ ) {
particles.push(
new Particle(
i,
radius + Math.random() * (width - radius * 2),
radius + Math.random() * (height - radius * 2)
)
);
}
}
num_particles = particles.length
play = true;
run();
},
stop: function() {
play = false;
}
};
}();
fluid.init('c', 800, 376);
document.getElementById('reset').onmousedown = function() {
fluid.stop();
setTimeout(function(){fluid.init('c', 800, 366)}, 100);
}
Also see: Tab Triggers