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.
const frame = new Frame("fit", 1024, 768, yellow, grey);
frame.on("ready", ()=>{ // ES6 Arrow Function - similar to function(){}
zog("ready from ZIM Frame"); // logs in console (F12 - choose console)
// often need below - so consider it part of the template
const stage = frame.stage;
const stageW = frame.width;
const stageH = frame.height;
// REFERENCES for ZIM at https://zimjs.com
// see https://zimjs.com/intro.html for an intro example
// see https://zimjs.com/learn.html for video and code tutorials
// see https://zimjs.com/docs.html for documentation
// see https://codepen.io/topic/zim/ for ZIM on CodePen
// *** NOTE: ZIM Cat defaults to time in seconds
// All previous versions, examples, videos, etc. have time in milliseconds
// This can be set back with TIME = "milliseconds" but we suggest you give it a try!
// There will be a warning in the conslole if your animation is not moving ;-)
// CODE HERE
new Label("R E P E T I T I O N", 60, "impact", yellow.darken(.3)).pos(0,50, CENTER);
// we will get a new color in this order
const colors = series(blue,pink,orange,yellow,red,light,green);
// making two tiles that are almost the same so use STYLE
STYLE = {
cols:7,
rows:4,
colSize:130,
rowSize:130,
align:CENTER,
valign:CENTER
}
// make the backing clickable
// just a little easier for the user than the shape when the shape changes
const back = new Tile(new Rectangle(125, 125, purple, grey)).center().cur();
const tile = new Tile(new Circle(50, green)).center().noMouse();
// turn off the STYLE - or use Style.clear()
STYLE = {};
// this will hold our data for each change that we want
// in the format of [tile index going backwards and the forwards] then shape index and color
// we store one of these each time we click on a tile
// [ [[4,6],1,color], [] ]
const tasks = [];
// set up an auto activate so it animates to start - good for CodePen people to see some motion
// choose a random tile number in the middle to start
const r = rand(9, tile.items.length-9);
let auto = interval(1, ()=>{
activate(r);
}, 3, true); // three times immediately
// also call activate when press down on a tile
// do not want to continue auto activating when users clicks so clear the auto interval
back.on("mousedown", e=>{
activate(e.target.tileNum);
if (auto) {
auto.clear();
auto = null;
}
});
function activate(tileIndex) {
// pick the next color from the series
// get the tile at the index
// and swap for a new random shape
let color = colors();
let current = tile.items[tileIndex];
let shapeIndex = rand(shapes.length-1);
swapShape(tileIndex, shapeIndex, color);
stage.update();
// add the task to the tasks
tasks.push( [[tileIndex-1, tileIndex+1], shapeIndex, color] );
}
// use an interval to handle all updates
// we will use a Slider to change the speed of the interval
// so store it in a variable and call a named function
// this allows us to clear the interval and call it again with a new speed
// we could use ZIM VEE to dynamically change the speed of the interval
// but that always waits for the current interval to finish
// before moving to the new interval speed - so can see sluggish
// So... we will clear the interval and remake it in the slider event function later
let inter = interval(.2, change);
function change() {
// loop through each task
// each time reducing or increasing the back and fore values
// and setting the appropriate shapes
loop(tasks, (task, i)=>{
let back = task[0][0]--;
let fore = task[0][1]++;
if (back >= 0) {
swapShape(back, task[1], task[2]);
}
if (fore < tile.items.length) {
swapShape(fore, task[1], task[2]);
}
// remove the task when all done
if (back < 0 && fore >= tile.items.length) tasks.splice(i,1);
}, true); // loop backwards when potentially removing
stage.update();
}
// the shapes to randomly pick from
// centerReg the Rectangle so they place on others which are already centerReg
const shapes = [
new Circle(50),
new Rectangle(100,100).centerReg({add:false}),
new Triangle(100,100,100),
new Poly(60,[5,6,7,8],{min:.4, max:.6}),
new Poly(60,[5,6,7,8],{min:.4, max:.6})
];
function swapShape(tileIndex, shapeIndex, color) {
// clone the shape - using true to make sure Poly is exact per task
let shape = shapes[shapeIndex].clone(true);
shape.color = color;
// record the shape we are replacing
let replace = tile.items[tileIndex];
// loc will place at the same location
// need to add it to the tile
shape.loc(replace, null, tile);
// remove the old shape
replace.removeFrom().dispose();
// set the tile item
tile.items[tileIndex] = shape;
// and set the tileNum on the shape (used in activate)
shape.tileNum = tileIndex;
}
// create the slider
STYLE = {
backgroundColor:purple,
rollBackgroundColor:pink,
borderColor:dark
}
new Slider({
min:.5,
max:0,
step:.05,
useTicks:true,
semiTicks:false,
button:"aztec",
currentValue:.2
}).pos(70, 40, CENTER, BOTTOM).change(e=>{
// clear the old interval
inter.clear();
// remake the interval with the slider speed
inter = interval(e.target.currentValue, change);
});
stage.update(); // this is needed to show any changes
// DOCS FOR ITEMS USED
// https://zimjs.com/docs.html?item=Frame
// https://zimjs.com/docs.html?item=Circle
// https://zimjs.com/docs.html?item=Rectangle
// https://zimjs.com/docs.html?item=Triangle
// https://zimjs.com/docs.html?item=Poly
// https://zimjs.com/docs.html?item=Label
// https://zimjs.com/docs.html?item=Slider
// https://zimjs.com/docs.html?item=change
// https://zimjs.com/docs.html?item=noMouse
// https://zimjs.com/docs.html?item=loop
// https://zimjs.com/docs.html?item=cur
// https://zimjs.com/docs.html?item=pos
// https://zimjs.com/docs.html?item=loc
// https://zimjs.com/docs.html?item=removeFrom
// https://zimjs.com/docs.html?item=centerReg
// https://zimjs.com/docs.html?item=center
// https://zimjs.com/docs.html?item=Tile
// https://zimjs.com/docs.html?item=rand
// https://zimjs.com/docs.html?item=interval
// https://zimjs.com/docs.html?item=series
// https://zimjs.com/docs.html?item=darken
// https://zimjs.com/docs.html?item=zog
// https://zimjs.com/docs.html?item=STYLE
STYLE = {};
// FOOTER
// call remote script to make ZIM icon - you will not need this
createIcon();
createGreet();
}); // end of ready
Also see: Tab Triggers