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 id="wrap">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="800" height="500" id="svg3906" version="1.1" viewBox="0 0 800 500">
<defs id="defs3908">
<filter color-interpolation-filters="sRGB" id="filter4001" x="-0.08" width="1.15" y="-0.39" height="1.77">
<feGaussianBlur stdDeviation="18.06" id="feGaussianBlur4003"/>
</filter>
<mask maskUnits="userSpaceOnUse" id="mask12847">
<rect id="rect12849" width="562.5" height="112.5" x="145.27" y="425.5" style="fill:#fffcfc;filter:url(#filter4001)"/>
</mask>
</defs>
<metadata id="metadata3911"/>
<g id="layer4">
<g id="meterParent" mask="url(#mask12847)" transform="translate(196.23395,-28)">
<g id="meter">
<rect y="475.5" x="-566.64" height="12.5" width="2" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-503.809" height="12.5" width="2" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-440.97" height="12.5" width="2" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-378.15" height="12.5" width="2" style="fill-opacity:0.87;fill:#000"/>
<rect id="rect3019" width="2" height="37.5" x="-315.32" y="450.5" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-252.49" height="12.5" width="2" id="use3023" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-189.66" height="12.5" width="2" id="use3025" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-126.82" height="12.5" width="2" id="use3027" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="-63.99" height="12.5" width="2" id="use3029" style="fill-opacity:0.87;fill:#000"/>
<rect y="463" x="-1.16" height="25" width="2" id="use3031" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="61.67" height="12.5" width="2" id="use3033" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="124.5" height="12.5" width="2" id="use3035" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="187.34" height="12.5" width="2" id="use3037" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="250.17" height="12.5" width="2" id="use3039" style="fill-opacity:0.87;fill:#000"/>
<rect y="450.5" x="313" height="37.5" width="2" id="use3041" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="375.83" height="12.5" width="2" id="use3043" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="438.66" height="12.5" width="2" id="use3045" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="501.5" height="12.5" width="2" id="use3047" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="564.33" height="12.5" width="2" id="use3049" style="fill-opacity:0.87;fill:#000"/>
<rect y="463" x="627.16" height="25" width="2" id="use3051" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="689.99" height="12.5" width="2" id="use3053" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="752.82" height="12.5" width="2" id="use3055" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="815.66" height="12.5" width="2" id="use3057" style="fill-opacity:0.87;fill:#000"/>
<rect y="475.5" x="878.49" height="12.5" width="2" id="use3059" style="fill-opacity:0.87;fill:#000"/>
</g>
</g>
</g>
<g id="layer3">
<g id="tWheelset" transform="translate(510,220)">
<g id="wheelset">
<g id="wheel" transform="translate(-150,-300)">
<path id="path5301" d="m150 100 0 400" style="fill:none;stroke-width:4;stroke:#000"/>
<path d="M350 300C350 410.46 260.46 500 150 500 39.54 500-50 410.46-50 300-50 189.54 39.54 100 150 100c110.46 0 200 89.54 200 200z" id="path3778" style="fill-opacity:0.85;fill:#dbdbdb;stroke-opacity:0.3;stroke:#040404"/>
<path id="path3858" d="M262.5 300C262.5 237.87 212.13 187.5 150 187.5 87.87 187.5 37.5 237.87 37.5 300l225 0z" style="fill-opacity:0.85;fill:#f60;stroke:#000"/>
<path id="path3849" d="m262.5 300-225 0c0 62.13 50.37 112.5 112.5 112.5 62.13 0 112.5-50.37 112.5-112.5z" style="fill-opacity:0.87;fill:#fffcfc;stroke:#000"/>
</g>
<circle d="M 40,0 C 40,22.09139 22.09139,40 0,40 -22.09139,40 -40,22.09139 -40,0 c 0,-22.09139 17.90861,-40 40,-40 22.09139,0 40,17.90861 40,40 z" r="40" cy="0" cx="0" id="sprocket" style="fill:none;opacity:0.5;stroke-width:15;stroke:#ddd"/>
</g>
</g>
</g>
<g id="layer5">
<path d="m-120 10 40 0c24.93 0 45 20.07 45 45 0 24.93-20.07 45-45 45l-40 0c-24.93 0-45-20.07-45-45 0-24.93 20.07-45 45-45z" id="chain" style="fill:none;stroke-dasharray:15;stroke-linecap:round;stroke-linejoin:bevel;stroke-width:6;stroke:#000"/>
</g>
<g id="layer2">
<g id="tCrankset" transform="translate(210,220)">
<g id="crankset">
<circle id="chainring" cx="0" cy="0" r="70" style="fill:none;opacity:0.5;stroke-width:15;stroke:#ddd"/>
<g id="crankarm" stroke-width="5">
<path d="M10 0A10 10 0 1 1-10 0 10 10 0 1 1 10 0z" id="path12042" style="fill-opacity:0.55;fill:#f9f9f9;opacity:0.98;stroke-width:5;stroke:#000"/>
<path d="m-110 0a10 10 0 1 1-20 0 10 10 0 1 1 20 0z" id="path12046" style="fill-opacity:0.55;fill:#f9f9f9;opacity:0.98;stroke-width:5;stroke:#000"/>
<path id="path12048" d="M-110 0-10 0" style="fill:none;stroke-width:5;stroke:#000"/>
</g>
</g>
</g>
</g>
<g id="layer6">
<path id="shiftFront" d="m75.2 379.86 0-18.99 16.44 9.49z" transform="translate(144.79695,119.13139)"/>
<path transform="matrix(-1,0,0,1,275.20305,119.13139)" d="m75.2 379.86 0-18.99 16.44 9.49z" id="shiftFrontDown"/>
<path transform="translate(444.79695,119.13139)" d="m75.2 379.86 0-18.99 16.44 9.49z" id="shiftBackUp"/>
<path id="shiftBackDown" d="m75.2 379.86 0-18.99 16.44 9.49z" transform="matrix(-1,0,0,1,575.20305,119.13139)"/>
</g>
</svg>
</div>
body {
background: #f7f4ee;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 100vh;
}
#wrap {
width: 100%;
max-width: 700px;
margin: 0 auto;
}
svg {
width: 100%;
height: 100%;
}
#layer6 path {
cursor: pointer;
fill: black;
stroke-width: 0;
}
#layer6 path:hover {
fill: #f60;
}
window.onload = function () {
(function(){
// Representation of the drivetrain modules
var crankset = Crankset();
var chain = Chain ();
var wheel = Wheel();
crankset.init(1);
chain.init( crankset.getChainspeed() );
wheel.init( crankset.getChainspeed() );
// Update/control functions. Perhaps this is just a overcomplicated way to
// define event handlers
var shiftFrontSprocket = function ( d ) {
var r = crankset.chainring.getRadius ( ) + d;
if ( r <= 0 || r > 200 ) return;
crankset.chainring.setRadius ( r );
chain.updateFrontSprocket( r );
wheel.update ( crankset.getChainspeed() );
meter.update ( Math.PI / wheel.getAngVel() );
};
var shiftBackSprocket = function ( d ) {
var r = wheel.sprocket.getRadius ( ) + d;
if ( r <= 0 || r > 200 ) return;
wheel.sprocket.setRadius ( r );
chain.updateBackSprocket( r );
wheel.update ( crankset.getChainspeed() );
meter.update ( Math.PI / wheel.getAngVel() );
};
Shifter ( 'shiftFront', shiftFrontSprocket, 15 );
Shifter ( 'shiftFrontDown', shiftFrontSprocket, -15 );
Shifter ( 'shiftBackUp', shiftBackSprocket, 10 );
Shifter ( 'shiftBackDown', shiftBackSprocket, -10 );
// The terrain
var meter = Meter();
// How long should the pan take? Just as long as it takes for the wheel to do a half loop,
// also PI/ang_vel
meter.init( Math.PI / wheel.getAngVel() );
})();
}
var Meter = function() {
var sprite = document.getElementById( 'meter' );
var tween = null;
var orig_dur;
return {
init: function ( dur ) {
orig_dur = dur;
// Start pan:
tween = new TweenMax( sprite, dur, {
x: Math.PI*200,
ease: 'Linear.easeNone',
repeat: -1
});
},
update: function ( dur ) {
tween.timeScale ( orig_dur / dur );
}
};
};
var Crankset = function( ){
var sprite = document.getElementById( 'crankset' );
var tween = null;
var ang_vel; // Constant after initialization
var chainspeed;
// As a reminder, to show what's the difference with the modular pattern
// here a normal literal object: observe the use of 'this'
var chainring = {
sprite: document.getElementById( 'chainring' ),
setRadius: function ( r ) {
this.sprite.setAttribute('r', r);
chainspeed = r * ang_vel;
},
getRadius: function ( ) {
return parseInt(this.sprite.getAttribute('r'));
}
};
return {
chainring: chainring,
init: function ( w ) {
ang_vel = w;
chainspeed = ang_vel * chainring.getRadius();
tween = new TweenMax(sprite, 2*Math.PI/w, {
rotation: -360,
ease: 'Linear.easeNone',
repeat: -1
});
},
getChainspeed: function () {
return chainspeed;
}
}
};
var Chain = function ( ) {
var sprite = document.getElementById( 'chain' );
// Deriving the chain from the svg is hard because getClientRect and getBBox
// do not give the result in svg screen coordinates. (How about snap's getBBox()?)
// (Or how about extracting the coordinates from the transform property?)
// Just type in the numbers then
var sprocket0 = { m: { x: 210, y: 220}, r: 70};
var sprocket1 = { m: { x: 510, y: 220}, r: 40};
// Define the shape of the chain by calculating the 2 lines tangent to both sprockets
var tangentLines = [];
calcTangents = function () {
// Calculate angle between the line through the centers of the circles and 1 of the 2 lines tangent to both circles
var dx = sprocket0.m.x-sprocket1.m.x;
var dy = sprocket0.m.y-sprocket1.m.y;
var d = Math.sqrt(dx*dx + dy*dy);
var angle = Math.asin ( (sprocket0.r - sprocket1.r) / d );
// Now do some math to get the actual tangent lines
angle -= Math.PI/2;
var ca = Math.cos( angle );
var sa = Math.sin( angle );
tangentLines[0] =[
sprocket1.m.x + sprocket1.r * ca,
sprocket1.m.y + sprocket1.r * sa,
sprocket0.m.x + sprocket0.r * ca,
sprocket0.m.y + sprocket0.r * sa,
];
tangentLines[1] = [
sprocket0.m.x + sprocket0.r * ca,
sprocket0.m.y - sprocket0.r * sa,
sprocket1.m.x + sprocket1.r * ca,
sprocket1.m.y - sprocket1.r * sa,
];
};
/*
* Draw the convex hull around the two circles
*/
getChainPathData = function ( ) {
var sweep = sprocket1.r > sprocket0.r ? 1 : 0;
var d = "M" + tangentLines[0][0] + " " + tangentLines[0][1] + " L" + tangentLines[0][2] + " " + tangentLines[0][3];
// add arc sprocket1
d += " A " + sprocket0.r + " " + sprocket0.r + ", 0, " + parseInt(1-sweep) + ", 0, " + tangentLines[1][0] + " " + tangentLines[1][1]
d += " L " + tangentLines[1][2] + " " + tangentLines[1][3];
// add arc voorblad
d += " A " + sprocket1.r + " " + sprocket1.r + ", 0, " + sweep + ", 0, " + tangentLines[0][0] + " " + tangentLines[0][1];
return d;
};
var update = function() {
calcTangents();
// draw new calculated chain shape
sprite.setAttribute('d', getChainPathData() );
// timescale tween
tween.timeScale(sprocket0.r/sprocket0.orig_r);
};
return {
init: function( speed ) {
// remember original radius, to alter tween afterwards
sprocket0.orig_r = sprocket0.r;
tween = new TweenMax( sprite, 30/speed, {
'strokeDashoffset': -30,
ease: 'Linear.easeNone',
repeat: -1
});
update();
},
updateFrontSprocket: function ( r ) {
sprocket0.r = r;
update();
},
updateBackSprocket: function ( r ) {
sprocket1.r = r;
update();
}
}
}
var Wheel = function ( ) {
var sprite = document.getElementById( 'wheelset' );
var tween = null;
var ang_vel,
orig_ang_vel;
var sprocket = {
sprite: document.getElementById( 'sprocket' ),
setRadius: function ( r ) {
this.sprite.setAttribute('r', r);
},
getRadius: function ( ) {
return parseInt(this.sprite.getAttribute('r'));
}
};
return {
sprocket: sprocket,
init: function ( chainspeed ) {
ang_vel = chainspeed / sprocket.getRadius();
tween = new TweenMax(sprite, 2*Math.PI/ang_vel, {
rotation: -360,
ease: 'Linear.easeNone',
repeat: -1
});
orig_ang_vel = ang_vel;
},
update: function( chainspeed ) {
// timescale tween
ang_vel = chainspeed / sprocket.getRadius();
tween.timeScale(ang_vel/orig_ang_vel);
},
getAngVel: function() {
return ang_vel;
}
}
};
var Shifter = function( spriteId, callback, d ) {
var sprite = document.getElementById ( spriteId );
sprite.onclick = function() {
callback( d );
};
};
Also see: Tab Triggers