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.
// 3D Clock
// Inspired by The Coding Train Coding Challenge #74: Clock with p5.js, https://www.youtube.com/watch?v=E4RyStef-gY
State = {
soundOn: true,
// Used to align the clock “agitation” cycle with a second boundary
navStartMsSecondOffset: window.performance.timing.navigationStart % 1000,
numTickMarks: 60,
raiseExtent: Math.PI * 2 / 60,
hourMarkThickness: 10,
minuteMarkThickness: 5,
raisedMarkMaxHeight: 50,
normalMarkHeight: 10,
axleThickness: 10,
axleLength: 200,
freqSeconds: 880,
maxVolumeSeconds: 0.2,
freqMinutes: 440,
maxVolumeMinutes: 0.3
};
function Sound() {
function startOsc(freq) {
const osc = new p5.Oscillator();
osc.setType('sine');
osc.freq(freq);
osc.start();
return osc;
}
this.oscSecs = startOsc(State.freqSeconds);
this.oscMins = startOsc(State.freqMinutes);
this.set = function(msOfCurrentSecond, minutesFraction) {
if (State.soundOn) {
const rampTime = 0.01;
const secsVol = map(msOfCurrentSecond, 0, 1000, State.maxVolumeSeconds, 0);
State.sound.oscSecs.amp(secsVol, rampTime);
const fractionalSecondsAngle = map(msOfCurrentSecond, 0, 1000, 0, TWO_PI);
State.sound.oscSecs.pan(Math.sin(fractionalSecondsAngle));
const fadeZone = 1 / 12;
const minsVol = map(Math.min(fadeZone, minutesFraction), 0, fadeZone,
State.maxVolumeMinutes, 0);
State.sound.oscMins.amp(minsVol, rampTime);
} else {
State.sound.oscSecs.amp(0);
State.sound.oscMins.amp(0);
}
}
}
function setup() {
State.sound = new Sound();
State.colors = { // Set this here because the color function isn’t available before
ticks: color(59, 71, 248),
hour: color(51, 136, 217),
min: color(69, 233, 240),
sec: color(51, 217, 144)
};
const len = Math.min(document.body.clientWidth, window.innerHeight);
createCanvas(len, len, WEBGL);
}
function draw() {
/**
* Draws tick marks along the circumference of the clock.
* @param radius the radius of the circle along which the marks are drawn
* @param msOfCurrentSecond how far we are into the next second
*/
function drawTickMarks(radius, msOfCurrentSecond) {
/**
* Computes the thickness of a tick mark, based on its tick index. Ticks at hour
* positions are thicker.
* @param tickIndex the index from 0 to the number of tick marks - 1
* @returns a thickness in pixels
*/
function markThickness(tickIndex) {
const hourMark = tickIndex % (60 / 12) === 0;
return hourMark ? State.hourMarkThickness : State.minuteMarkThickness;
}
/**
* Computes the height (the length along z) of a tick mark.
* @param angle the angle at which the mark will be drawn
* @returns the mark height in pixels
*/
function markHeight(angle) {
const angleTurnedOneQuarter = (angle + TWO_PI / 4) % TWO_PI;
const msAngle = map(msOfCurrentSecond, 0, 1000, PI * 2, 0);
const tickDistanceFromMsPos = Math.abs(msAngle - angleTurnedOneQuarter);
const limitedTickDistanceFromMsPos = Math.min(State.raiseExtent, tickDistanceFromMsPos);
return map(limitedTickDistanceFromMsPos, 0, State.raiseExtent,
State.raisedMarkMaxHeight, State.normalMarkHeight);
}
for (let tickIndex = 0; tickIndex < State.numTickMarks; ++tickIndex) {
push();
const angle = tickIndex * TWO_PI / State.numTickMarks;
rotateZ(angle);
translate(-radius, 0, 0);
rotateX(PI / 2); // Align with the z axis
cylinder(markThickness(tickIndex), markHeight(angle));
pop();
}
}
function drawAxle() {
push();
rotateX(PI / 2); // Align with the z axis
cylinder(State.axleThickness, State.axleLength);
pop();
}
/**
* Draws a clock hand.
* @param position the position (seconds, minutes, or hours. A real number.
* @param maxUnits 60 or 12
* @param radius the radius of the cylinder forming the hand
* @param length the length of the cylinder
* @param color the color of the cylinder
* @param z where the hand is positioned along the z axis
*/
function drawHand(position, maxUnits, radius, length, color, z) {
push();
fill(color);
const handAngle = map(position, 0, maxUnits, 0, TWO_PI);
rotateZ(-handAngle);
translate(0, -length / 2, z);
cylinder(radius, length);
pop();
}
/** Moves the entire clock on the y axis sinusoidally, in the manner of a washing machine agitator */
function agitateClock(cyclesPerSecond, maxRotationRadians) {
const angleOverTime = cyclesPerSecond / 1000 * (millis() - State.navStartMsSecondOffset) * PI * 2;
rotateY(map(Math.sin(angleOverTime), -1, 1, -maxRotationRadians, maxRotationRadians));
}
agitateClock(0.1, PI / 8);
const millisecondsOfCurrentSecond = new Date().getTime() % 1000;
const secondPlusFraction = second() + millisecondsOfCurrentSecond / 1000;
const minutesFraction = secondPlusFraction / 60;
const minutePlusFraction = minute() + minutesFraction;
const hourPlusFraction = hour() % 12 + minutePlusFraction / 60;
State.sound.set(millisecondsOfCurrentSecond, minutesFraction);
fill(State.colors.ticks);
drawTickMarks(width * 0.35, millisecondsOfCurrentSecond);
fill(0);
drawAxle();
drawHand(hourPlusFraction, 12, 9, width / 6, State.colors.hour, 0);
drawHand(minutePlusFraction, 60, 6, width / 3, State.colors.min, 30);
drawHand(secondPlusFraction, 60, 3, width / 3, State.colors.sec, 60);
}
function keyPressed() {
if (key === 'S') {
State.soundOn = !State.soundOn;
}
}
Also see: Tab Triggers