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, silver, dark, "js.png", "https://assets.codepen.io/2104200/");
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: since ZIM Cat (before ZIM NFT) ZIM 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 Rectangle(600,stageH,light).center();
const logo = asset("js.png").centerReg().sca(.55).pos(40,30).sha(black.toAlpha(.2),5,5,5);
// The data for terms and definitions are stored in arrays in a remote js file.
// The arrays are named one and two.
// Make labels out of the list of terms and definitions.
// Replace the strings in arrays with label objects.
STYLE = {padding:11, size:16, backgroundColor:purple, color:white, centerReg:true};
loop(one, (a, i)=>{
one[i] = new Label({text:a, align:CENTER, labelWidth:120});
two[i] = new Label({text:two[i], labelWidth:300});
one[i].match = two[i]; // remember the match for later
});
STYLE = {};
// make a tile with one column and two rows from array of labels
// the true value means use the array to make unique objects
// otherwise ZIM would pick randomly from the array (ZIM VEE)
const oneTile = new Tile(one, 1, one.length, 0, 6, true)
const twoTile = new Tile(two, 1, two.length, 0, 6, true);
// make a Scrambler from each tile
const scrambler1 = new Scrambler(oneTile).pos(280, 0, LEFT, CENTER);
const scrambler2 = new Scrambler(twoTile).pos(280, 0, RIGHT, CENTER);
// when we press up on a scrambler, test to see if the answers match
scrambler1.on("pressup", test);
scrambler2.on("pressup", test);
// preparing for the test - we will animate the color of the label background
// to either a good or bad color
goodObj = {props:{color:green}, rewind:true, time:.1, rewindWait:.5, call:done};
badObj = {props:{color:red}, rewind:true, time:.1, rewindWait:.5, call:done};
function good(a,b) {
a.background.animate(copy(goodObj)); // each animate adds things to its animation object
b.background.animate(copy(goodObj)); // so copy the object to set a unuique object
}
function bad(a,b) {
a.background.animate(copy(badObj));
b.background.animate(copy(badObj));
}
function done(target) {
target.color = purple; // just to make sure a test does not conflict
}
// we might have moved the left or right tile
// so do a slightly different test for each one
// a Scrambler usually works on its own and has a complete event
// but here we are matching two scramblers so we are on our own for a solution...
// we will move the selected item over on top of its neighbor
// and do a hit test to find if these values are matching based on the arrays
function test(e) {
// when we drop, the item may not be animated to its final resting point yet
// so we will delay a little before doing the test
timeout(.2, () => {
let a = e.target;
if (e.currentTarget==scrambler1) testLeft(a);
else testRight(a);
});
}
// move the left item (a) over to the right
// and see which of the right hand tile items it is hitting
// so loop through all those items and each time get the item (b)
// and test if a is hitting the reg of b
function testLeft(a) {
a.x += 300; // move the object to overlap the object at right
let wrong = 0; // will return wrong number - used in test
loop(twoTile, b=>{
if (b.hitTestReg(a)) {
// once we find the two items hitting
// then see if they match - the match was stored earlier
if (a.match == b) good(a,b);
else {wrong = 1; bad(a,b);}
}
});
a.x -= 300; // move the object back
return wrong;
}
function testRight(a) {
// same as above but just reversed in direction and arrays
a.x -= 300;
loop(oneTile, b=>{
if (a.hitTestReg(b)) {
if (b.match == a) good(a,b);
else bad(a,b);
}
});
a.x += 300;
}
new Button({
label:"MIX",
backgroundColor:orange,
rollBackgroundColor:green,
corner:0
}).sca(.6).pos(45,45,LEFT,BOTTOM).tap(() => {
scrambler1.scramble(1,0,2);
scrambler2.scramble(1,1,2);
});
const emitter = new Emitter({
obj:logo,
interval:.1,
life:2,
force:{min:2, max:9},
angle:{min:-30, max:45},
animation:{props:{rotation:[{min:1*360, max:4*360, negative:true}]}, time:2, ease:"quadOut"},
startPaused:true
}).loc(logo)
new Button({
label:"TEST",
backgroundColor:blue,
rollBackgroundColor:green,
corner:0
}).sca(.6).pos(45,45,RIGHT,BOTTOM).tap(() => {
let wrong = 0;
oneTile.loop(obj => {
wrong += testLeft(obj, 1);
});
if (wrong == 0) timeout(.5, () => {
emitter.spurt(15);
});
});
STYLE = {size:34, lineHeight:44};
new Pane(stageW+50, 150, "JS DEFINITIONS - drag either side to match\nWould suggest starting at top or bottom", yellow).show();
// DOCS FOR ITEMS USED
// https://zimjs.com/docs.html?item=Frame
// https://zimjs.com/docs.html?item=Rectangle
// https://zimjs.com/docs.html?item=Label
// https://zimjs.com/docs.html?item=Button
// https://zimjs.com/docs.html?item=Pane
// https://zimjs.com/docs.html?item=Scrambler
// https://zimjs.com/docs.html?item=tap
// https://zimjs.com/docs.html?item=hitTestReg
// https://zimjs.com/docs.html?item=animate
// https://zimjs.com/docs.html?item=loop
// https://zimjs.com/docs.html?item=sha
// https://zimjs.com/docs.html?item=pos
// https://zimjs.com/docs.html?item=loc
// https://zimjs.com/docs.html?item=sca
// 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=Emitter
// https://zimjs.com/docs.html?item=timeout
// https://zimjs.com/docs.html?item=toAlpha
// https://zimjs.com/docs.html?item=zog
// https://zimjs.com/docs.html?item=STYLE
stage.update();
}); // end of ready
Also see: Tab Triggers