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.
<div class="selector">
<label>
<input type="radio" name="mode" value="water" checked />WATER
</label>
<label>
<input type="radio" name="mode" value="fire" />FIRE
</label>
<label>
<input type="radio" name="mode" value="earth" />EARTH
</label>
<label>
<input type="radio" name="mode" value="air" />AIR
</label>
<label>
<input type="checkbox" id="visual_mode" />VISUAL?
</label>
<input type="hidden" id="visual_uri" value="https://customstickershop.us/wp-content/uploads/2015/08/lucifer-sigil.jpg" />
<input type="button" id="btn_set_visual_uri" value="Set URI" />
</div>
<canvas id="canvas" class="orb"></canvas>
body {
background-image: url(http://subtlepatterns2015.subtlepatterns.netdna-cdn.com/patterns/light_wool.png);
}
.water {
background-color: rgba(58, 209, 254, .2);
}
.air {
background-color: rgba(224, 224, 224, .2);
}
.fire {
background-color: rgba(171, 0, 11, .2);
}
.earth {
background-color: rgba(159, 214, 34, .2);
}
.orb {
-webkit-box-shadow: 0px 50px 5px rgba(0, 0, 0, .5);
/* Saf3-4 */
-moz-box-shadow: 0px 50px 5px rgba(0, 0, 0, .5);
/* FF3.5 - 3.6 */
box-shadow: 0px 50px 5px rgba(0, 0, 0, .5);
/* Opera 10.5, IE9, FF4+, Chrome 10+ */
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
// Create an array to store our particles
var particles = [];
// The amount of particles to render
var particleCount = 30;
// The maximum velocity in each direction
var maxVelocity = 2;
// The target frames per second (how often do we want to update / redraw the scene)
var targetFPS = 33;
// Set the dimensions of the canvas as variables so they can be used.
var canvasWidth = Math.min(window.innerHeight, window.innerWidth) * 0.75;
var canvasHeight = canvasWidth;
// Create an image object (only need one instance)
var imageObj = new Image();
// Once the image has been downloaded then set the image on all of the particles
imageObj.onload = function() {
particles.forEach(function(particle) {
particle.setImage(imageObj);
});
};
// Once the callback is arranged then set the source of the image
imageObj.src = "http://www.blog.jonnycornwell.com/wp-content/uploads/2012/07/Smoke10.png";
// A function to create a particle object.
function Particle(context) {
// Set the initial x and y positions
this.x = 0;
this.y = 0;
// Set the initial velocity
this.xVelocity = 0;
this.yVelocity = 0;
// Set the radius
this.radius = 5;
// Store the context which will be used to draw the particle
this.context = context;
// The function to draw the particle on the canvas.
this.draw = function() {
// If an image is set draw it
if (this.image) {
this.context.drawImage(this.image, this.x - 128, this.y - 128);
// If the image is being rendered do not draw the circle so break out of the draw function
return;
}
};
// Update the particle.
this.update = function() {
// Update the position of the particle with the addition of the velocity.
this.x += this.xVelocity;
this.y += this.yVelocity;
// Check if has crossed the right edge
if (this.x >= canvasWidth) {
this.xVelocity = -this.xVelocity;
this.x = canvasWidth;
}
// Check if has crossed the left edge
else if (this.x <= 0) {
this.xVelocity = -this.xVelocity;
this.x = 0;
}
// Check if has crossed the bottom edge
if (this.y >= canvasHeight) {
this.yVelocity = -this.yVelocity;
this.y = canvasHeight;
}
// Check if has crossed the top edge
else if (this.y <= 0) {
this.yVelocity = -this.yVelocity;
this.y = 0;
}
};
// A function to set the position of the particle.
this.setPosition = function(x, y) {
this.x = x;
this.y = y;
};
// Function to set the velocity.
this.setVelocity = function(x, y) {
this.xVelocity = x;
this.yVelocity = y;
};
this.setImage = function(image) {
this.image = image;
}
}
// A function to generate a random number between 2 values
function generateRandom(min, max) {
return Math.random() * (max - min) + min;
}
// The canvas context if it is defined.
var context;
// Initialise the scene and set the context if possible
function init() {
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
// Set the context variable so it can be re-used
context = canvas.getContext('2d');
// Create the particles and set their initial positions and velocities
for (var i = 0; i < particleCount; ++i) {
var particle = new Particle(context);
// Set the position to be inside the canvas bounds
particle.setPosition(generateRandom(0, canvasWidth), generateRandom(0, canvasHeight));
// Set the initial velocity to be either random and either negative or positive
particle.setVelocity(generateRandom(-maxVelocity, maxVelocity), generateRandom(-maxVelocity, maxVelocity));
particles.push(particle);
}
} else {
alert("Please use a modern browser");
}
}
// The function to draw the scene
function draw() {
// Clear the drawing surface and fill it with a black background
//context.fillStyle = "rgba(0, 0, 0, 0.5)";
//context.fillRect(0, 0, 400, 400);
// Go through all of the particles and draw them.
particles.forEach(function(particle) {
particle.draw();
});
}
// Update the scene
function update() {
canvasWidth = Math.min(window.innerHeight, window.innerWidth) * 0.75;
canvasHeight = canvasWidth;
var margin_left = (window.innerWidth - canvasWidth) * 0.5;
var margin_top = (window.innerHeight - canvasWidth) * 0.5;
$('#canvas').attr({
width: Math.min(canvasWidth, canvasHeight),
height: Math.min(canvasWidth, canvasHeight),
'class': 'orb ' + $('input[name=mode]:checked').val()
}).css({
'margin-left': margin_left,
'margin-top': margin_top
});
particles.forEach(function(particle) {
particle.update();
});
}
// Initialize the scene
init();
// If the context is set then we can draw the scene (if not then the browser does not support canvas)
if (context) {
setInterval(function() {
// Update the scene befoe drawing
update();
// Draw the scene
draw();
}, 1000 / targetFPS);
}
$(document).ready(function() {
$('#btn_set_visual_uri').click(function() {
var uri = $('#visual_uri').val();
uri = prompt("Paste the URL to some image you wish to use...", uri);
if (uri)
$('#visual_uri').val(uri);
});
$('#visual_mode').change(function() {
if ($(this).prop('checked')) {
var uri = $('#visual_uri').val();
$('#canvas').css({
"background": "url(" + uri + ")",
"-moz-background-size": "100% 100%",
/* Firefox 3.6 */
"background-size": "100% 100%",
"background-repeat": "no-repeat"
});
} else {
$('#canvas').css({
"background": "",
"-moz-background-size": "",
"background-size": "",
"background-repeat": ""
});
}
})
});
Also see: Tab Triggers