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.
import zim from "https://zimjs.org/cdn/00/zim";
// see https://zimjs.com
// and https://zimjs.com/learn
// and https://zimjs.com/docs
const frame = new Frame(FIT, 1024, 768, darker, dark);
frame.on("ready", () => {
const stage = frame.stage;
const stageW = frame.width;
const stageH = frame.height;
// put your code here
// Create board and pieces using Tile
// could use loops, etc. but perhaps this is conceptually easier
const a = pink;
const b = purple;
const c = white;
const d = black;
const s = 80;
const colors = series(a,b,a,b,a,b,a,b).flip();
const board = new Tile(new Rectangle(s,s,colors),8,8).center();
const colorsA = series(d,c,d,c,d,c,d,c).flip();
const piecesA = new Tile(new Circle(s/2,colorsA),8,3).loc(board);
const colorsB = series(d,c,d,c,d,c,d,c).flip();
const piecesB = new Tile(new Circle(s/2,colorsB),8,3).pos(0,0,LEFT,BOTTOM,board);
// loop through the pieces and adjust them
// while we are at it, add each piece to a pool to pick the next move from
// if a piece is picked and it has no move it will be removed from the pool
// and another piece is picked
// once the turn is over all the existing pieces get added to the pool again
// store the pieces opposite side as other property
piecesA.pool = [];
piecesA.loop(piece=>{
if (piece.color!=c) piece.removeFrom();
else {
piece.sca(.8);
piece.other = piecesB;
piecesA.pool.push(piece);
}
}, true); // loop backwards when removing from container
piecesB.pool = [];
piecesB.loop(piece=>{
if (piece.color!=d) piece.removeFrom();
else {
piece.sca(.8);
piece.other = piecesA;
piecesB.pool.push(piece);
}
}, true); // loop backwards when removing from container
// these are the directions that we can try to move each piece
const directions = [{x:s,y:s},{x:-s,y:s},{x:s,y:-s},{x:-s,y:-s}];
const emitter = new Emitter({startPaused:true});
let pieces;
let count = 0; // use this to figure the side we are moving
move();
function move() {
pieces = count++%2==0?piecesB:piecesA;
// ZIM loops have no continue nor break
// but rather use return for continue
// and return a value to break
// the return value goes is returned by the loop
// NOTE: a return value of true is given to the loop
// if it finishes looping
// so here, we will eventually return a "done" to status
// if the piece has taken another or moved to a square
let status = false;
while(status!="done" && pieces.pool.length>0) {
// grab a piece
let piece = pluck(pieces.pool, true); // remove from pool
piece.start = {x:piece.x, y:piece.y}; // record where we start
shuffle(directions); // just go any which way!
status = loop(directions, d=>{ // get a movement
// relocate the piece to start then do the movement
piece.loc(piece.start).mov(d.x,d.y)
if (!piece.hitTestBounds(board)) return; // continue the loop
let attack = piece.other.loop(other=>{
if (piece.hitTestBounds(other)) {
emitter.loc(other).spurt(10);
other.alp(0).removeFrom();
return "success";
}
});
if (attack == "success") return "done"; // breaks the loop
let empty = piece.parent.loop(sibling=>{ // loop our own pieces
if (sibling == piece) return;
if (piece.hitTestBounds(sibling)) return false;
});
if (empty) return "done"; // otherwise will try another direction
});
if (status != "done") piece.loc(piece.start);
}
// add all pieces back to pool - next time might be different arrangement
pieces.pool = [];
pieces.loop(piece=>{pieces.pool.push(piece);});
stage.update();
if (piecesA.numChildren==0 || piecesB.numChildren==0) {
endGame();
return;
}
timeout(.3, move);
}
function endGame() {
frame.color = green;
stage.update();
timeout(2,()=>{
new Pane(stageW+40,200,"💙 CREATIVE CODING LESSONS on CODEPEN",yellow).show(()=>{
zgo("https://codepen.io/collection/JGNqmz?sort_by=item_created_at", "_blank")
});
});
}
frame.madeWith().pos(30,30,RIGHT,BOTTOM)
// 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=Pane
// https://zimjs.com/docs.html?item=hitTestBounds
// https://zimjs.com/docs.html?item=move
// https://zimjs.com/docs.html?item=loop
// https://zimjs.com/docs.html?item=pos
// https://zimjs.com/docs.html?item=loc
// https://zimjs.com/docs.html?item=mov
// https://zimjs.com/docs.html?item=alp
// https://zimjs.com/docs.html?item=sca
// https://zimjs.com/docs.html?item=removeFrom
// https://zimjs.com/docs.html?item=center
// https://zimjs.com/docs.html?item=Tile
// https://zimjs.com/docs.html?item=shuffle
// https://zimjs.com/docs.html?item=timeout
// https://zimjs.com/docs.html?item=series
createGreet(50,50);
});
Also see: Tab Triggers