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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<div id="wrap">
<h1>Canvas animated clipping mask</h1>
<canvas id="output" width="600" height="600"></canvas>
</div>
<div id="sources">
<img id="base" crossorigin="anonymous" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/101507/album1.jpg" />
<img id="xtra" crossorigin="anonymous" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/101507/album2.jpg" />
</div>
html {
height: 100%;
color: white;
}
body {
background: teal linear-gradient(transparent, #ff0099);
text-align: center;
}
canvas {
border: 3px solid yellow;
}
#sources {
display: none;
}
console.clear();
/** This is dead simple, just heavily-commented! **/
// Some constants to tweak.
var NUM_CIRCLES = 60,
MIN_SIZE = 50,
MAX_SIZE = 100;
// Returns a random int between two numbers.
function getRndInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Cache refs to our canvas, context and images.
var c = document.getElementById('output'),
ctx = c.getContext('2d'),
sources = document.getElementById('sources'),
imgBase = document.getElementById('base'),
imgXtra = document.getElementById('xtra');
// Timestamp var used in the update-render loop.
var t;
/**
* An object 'class' to describe a circle which animates
* its size (radius) and which can randomize its position.
**/
var Circle = {
x: 0,
y: 0,
size: 0,
_needsRandomized: false,
/**
* Set a random position and max size
**/
randomize: function() {
this.x = getRndInt(50, c.width - 50);
this.y = getRndInt(50, c.height - 50);
this.maxSize = getRndInt(MIN_SIZE, MAX_SIZE);
},
/**
* Animates the size up and down via a sine calculation against a passed-in
* timestamp factor. (See the main program update() method).
* Accepts an offset so different instances will animate out-of-sync
* (if ofs was 0 for all instances, they would synchronize).
* When the circle is fully-shrunk, it randomizes its position and max size.
**/
update: function(t, ofs) {
this.size = Math.abs(Math.round(Math.sin(t + ofs) * this.maxSize));
if (this.size < 2) {
if (this._needsRandomized) {
this.randomize();
this._needsRandomized = false;
}
} else {
this._needsRandomized = true;
}
},
/**
* Draws a circle to the context at the current position and size.
* NOTE: this doesn't open or close a path, or apply a fill or stroke.
* It assumes a path has already been opened in the context.
* (See main program render() method)
**/
draw: function() {
ctx.moveTo(this.x, this.y);
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
}
};
/**
*
* MAIN PROGRAM
*
**/
// Create multiple Circle instances in an array.
var circles = [];
for (var i = 0; i < NUM_CIRCLES; i++) {
var circle = Object.create(Circle);
circle.randomize();
circles.push(circle);
}
/**
* Tell each mask to update itself. Pass in the current time
* and an offset integer so the animations do not synchronize.
**/
function update() {
t = 0.001 * Date.now();
circles.forEach(function(circle, idx) {
circle.update(t, idx);
});
}
/**
* Main render method.
* NOTE: we could have the Circle class drawing its own clip path and
* image, but for performance reasons we do the main rendering here.
* So we have one clip path created from multiple circles, instead of
* multiple clip paths (and image renders) per frame.
**/
function render() {
// Draw the 'Sexualizer' background first.
// Since this will cover everything from the last frame,
// there's no need for an explicit 'clear the canvas' step.
ctx.drawImage(imgBase, 0, 0);
// Save the state (before any clip regions exist),
// and begin a new path.
ctx.save();
ctx.beginPath();
// Draw each Circle instance, at its current position
// and size, into the open path.
// (Note that nothing gets visibly 'drawn' here - we're
// just defining the shape of the path.)
circles.forEach(function(circle) {
circle.draw();
});
// Close the path and flag it as a clipping region
// for any subsequent drawing.
ctx.closePath();
ctx.clip();
// Draw the 'I Am The Night' image, which will be clipped
// by our path, so it is drawn 'into' the circles.
ctx.drawImage(imgXtra, 0, 0);
// Restore the pre-clip state, so that no further
// clipping will occur. Otherwise, the Sexualizer bg
// would get clipped when we draw it next frame.
ctx.restore();
}
// Main update-render loop.
function loop() {
requestAnimationFrame(loop);
update();
render();
}
// Kick it off when our images are fully loaded.
imagesLoaded(sources, function() {
console.log('Humans are such easy prey...');
loop();
});
Also see: Tab Triggers