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>
<canvas id="testCanvas" width="550" height="500"></canvas>
</div>
<a href="https://createjs.com/" target="_blank"><img id="logo" src="https://createjs.com/mediakit/createjs-badge-reverse.png"></a>
<div id="badges">
<div class="badge follow white"></div>
<div class="badge click white"></div>
</div>
var canvas;
var stage;
var imgSeq = new Image(); // The image for the sparkle animation
var sprite; // The animated sparkle template to clone
var fpsLabel;
var fader; // Semi fades the Background to give a tracing effect
var spkls; // Container for all the sparkles
function init() {
// create a new stage and point it at our canvas:
canvas = document.getElementById("testCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
stage = new createjs.StageGL(canvas, {preserveBuffer:true, antialias:true});
stage.autoClear = false;
stage.setClearColor(0x000000);
// attach mouse handlers directly to the source canvas.
// better than calling from canvas tag for cross browser compatibility:
stage.addEventListener("stagemousemove", moveCanvas);
stage.addEventListener("stagemousedown", clickCanvas);
var img = new Image();
img.crossOrigin="Anonymous";
img.onload = imageLoaded;
img.src = "https://lab.gskinner.com/codepen/_assets/art/spritesheet_sparkle.png";
}
function handleResize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
fader.graphics.clear().beginFill("rgba(0,0,0, 0.3)").drawRect(0,0, canvas.width, canvas.height).endFill();
fader.cache(0,0, canvas.width,canvas.height, 1/32);
stage.updateViewport(canvas.width, canvas.height);
stage.update();
}
function imageLoaded() {
// define simple sprite sheet data specifying the image(s) to use, the size of the frames,
// and the registration point of the frame
// it will auto-calculate the number of frames from the image dimensions and loop them
var data = {
images: [this], //'this' is a reference to the loaded image. Image loaded crossOrigin will throw security errors. For more information on Cross Origin Issues: https://www.createjs.com/docs/easeljs/classes/SpriteSheet.html
frames: {
width: 21,
height: 23,
regX: 10,
regY: 11
}
};
// set up an animation instance, which we will clone
sprite = new createjs.Sprite(new createjs.SpriteSheet(data));
// setup slow fadeout
fader = new createjs.Shape();
stage.addChild(fader);
var gfx = fader.graphics;
gfx.beginFill("rgba(0,0,0, 0.3)").drawRect(0,0, 1024, 704).endFill();
fader.cache(0,0, 1024,1024, 1/32); // Power of two textures smooth better, but we don't need full resolution either
spkls = new createjs.Container();
stage.addChild(spkls);
// add a text object to output the current FPS:
/*fpsLabel = new createjs.Text("------ @ -- fps", "bold 14px Arial", "#FFF");
stage.addChild(fpsLabel);
fpsLabel.x = 10;
fpsLabel.y = 20;
fpsLabel.cache(0, 0, 128, 16);*/
// start the tick and point it at the window so we can do some work before updating the stage:
createjs.Ticker.timingMode = createjs.Ticker.RAF;
createjs.Ticker.addEventListener("tick", tick);
handleResize();
window.addEventListener("resize", handleResize);
}
function tick(event) {
// loop through all of the active sparkles on stage:
var l = spkls.numChildren;
var m = event.delta / 16;
for (var i = 0; i < l; i++) {
var sparkle = spkls.getChildAt(i);
if(--sparkle.life <= 0) {
spkls.removeChild(sparkle);
i--; l--;
continue;
}
// apply gravity and friction
sparkle.vY += 0.1 * m;
// update position, scale, and alpha:
sparkle.x += sparkle.vX * m;
sparkle.y += sparkle.vY * m;
sparkle.alpha = sparkle.alphaStart * (sparkle.life / sparkle.lifeMax);
// remove sparkles that are off screen or not invisible
if (sparkle.y > canvas.height) {
sparkle.vY *= -(Math.random() * 0.1 + 0.8);
sparkle.vX += Math.cos( Math.random() * Math.PI * 2) * 3;
} else if (sparkle.y < 0) {
sparkle.vY *= 0.9;
}
if (sparkle.x > canvas.width || sparkle.x < 0) {
sparkle.vX *= -1;
}
}
/*fpsLabel.text = l + " @ " + Math.round(createjs.Ticker.getMeasuredFPS()) + " fps";
fpsLabel.updateCache();*/
// draw the updates to stage
stage.update(event);
}
// sparkle explosion
function clickCanvas(evt) {
addSparkles(Math.random() * 500 + 200 | 0, stage.mouseX, stage.mouseY, 1);
}
// sparkle trail
function moveCanvas(evt) {
addSparkles((Math.random() * 6 + 3) | 0, stage.mouseX, stage.mouseY, 0.1);
}
function addSparkles(count, x, y, speed) {
// create the specified number of sparkles
for (var i = 0; i < count; i++) {
// clone the original sparkle, so we don't need to set shared properties:
var sparkle = sprite.clone();
// set display properties:
sparkle.x = x;
sparkle.y = y;
sparkle.rotation = Math.random()*360;
sparkle.alpha = sparkle.alphaStart = Math.random() * 0.7 + 0.6;
sparkle.scale = Math.random() + 0.3;
sparkle.life = sparkle.lifeMax = Math.random() * 100 + 50;
// set up velocities:
var a = Math.PI * 2 * Math.random();
var v = (Math.random() - 0.5) * 30 * speed;
sparkle.vX = Math.cos(a) * v;
sparkle.vY = Math.sin(a) * v;
// start the animation on a random frame:
sparkle.gotoAndPlay(Math.random() * sparkle.spriteSheet.getNumFrames() | 0);
// add to the display list:
spkls.addChild(sparkle);
}
}
init();
Also see: Tab Triggers