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.
#app Loading…
$faderWidth: 6px
$faderHeight: 200px
$faderContainerWidth: $faderWidth * 8
$cfg: #111
$cfg-emphasis: #000
$cui: #ff0
$cui-emphasis: $cui - #222
$cui-error: #f00
$cbg: #c6f
body
margin: 0
background: $cbg
color: $cfg
font: italic 600 24px sans-serif
overflow: hidden
body, #app, .app-container, .faderbank, .faders
display: flex
align-items: center
justify-content: center
#app
height: 100vh
width: 100vw
.faderbank
flex-direction: column
.faderbank + .faderbank
padding-left: $faderWidth * 3
.faders
padding: $faderWidth * 4 0
position: relative
&::before, &::after
content: ''
position: absolute
height: $faderWidth * 2
border: solid $cfg
&::before
bottom: 0
width: 100%
border-width: 0 3px 3px 3px
&::after
bottom: -$faderWidth * 2
border-width: 0 0 0 3px
.fader-container
width: $faderContainerWidth
height: $faderHeight
// Cross-browser fader styling from http://danielstern.ca/range.css/
.fader
-webkit-appearance: none
cursor: pointer
width: $faderHeight
height: $faderWidth
margin: 0
padding: 0
border: 0
outline: 0
border-radius: $faderWidth / 2
background-color: $cfg
transform: translate(($faderContainerWidth - $faderHeight) / 2, ($faderHeight - $faderContainerWidth) / 2 + 13px) rotate(-90deg)
// Note: keeping these cross-browser styles in separate (redundant) rules so the browser understands it.
&::-webkit-slider-runnable-track
width: $faderHeight
height: $faderWidth
cursor: pointer
background: $cfg
border: 0
border-radius: $faderWidth / 2
&::-moz-range-track
width: $faderHeight
height: $faderWidth
cursor: pointer
background: $cfg
border: 0
border-radius: $faderWidth / 2
&::-webkit-slider-thumb
box-sizing: content-box
width: $faderWidth * 3
height: $faderWidth * 3
background: $cui
border: $faderWidth solid $cfg
border-radius: 50%
cursor: pointer
-webkit-appearance: none
appearance: none
margin-top: -$faderWidth * 2
.locked &::-webkit-slider-thumb
background: $cui-error !important
&::-moz-range-thumb
width: $faderWidth * 3
height: $faderWidth * 3
background: $cui
border: $faderWidth solid $cfg
border-radius: 50%
cursor: pointer
-webkit-appearance: none
appearance: none
.locked &::-moz-range-thumb
background: $cui-error !important
&::-ms-thumb
width: $faderWidth * 3
height: $faderWidth * 3
background: $cui
border: $faderWidth solid $cfg
border-radius: 50%
cursor: pointer
-webkit-appearance: none
appearance: none
// Needed to keep the Edge thumb centred
margin-top: 0px
&.locked::-ms-thumb
background: $cui-error !important
&::-ms-track
width: $faderHeight
height: $faderWidth
cursor: pointer
background: transparent
border-color: transparent
border-width: 9px 0
color: transparent
&::-ms-fill-lower, &::-ms-fill-upper
background: $cfg
border: 0
border-radius: $faderWidth / 2
&:active
&::-webkit-slider-thumb
background: $cui-emphasis
&::-moz-range-thumb
background: $cui-emphasis
&::-ms-thumb
background: $cui-emphasis
&:focus, &:hover
outline: none
background: $cfg-emphasis
::-webkit-slider-runnable-track
background: $cfg-emphasis
&::-ms-fill-lower
background: $cfg
&::-ms-fill-upper
background: $cfg-emphasis
&:focus
box-shadow: 0 0 2px rgba($cfg-emphasis, 0.5)
@supports (-ms-ime-align:auto)
// Pre-Chromium Edge only styles, selector taken from hhttps://stackoverflow.com/a/32202953/7077589,
.fader
margin: 0
.label
margin: $faderWidth * 3 0 0
padding: 0
button
border: $faderWidth solid $cfg
padding: 1em 1em .9em
background: $cui
font: inherit
cursor: pointer
border-radius: $faderWidth
&:active
background: $cui-emphasis
const {useEffect, useState} = React;
const {gainToDb, Transport, Destination, Player} = Tone;
let players;
const loops = [
{file: 'Beat1', initialVolume: 0.75},
{file: 'Bass1', initialVolume: 0.75},
{file: 'Layers1', initialVolume: 0.5},
{file: 'Layers2', initialVolume: 0.25},
{file: 'Chords1', initialVolume: 0},
{file: 'Beat2', initialVolume: 0},
{file: 'Flute1', initialVolume: 0},
{file: 'Bass2', initialVolume: 0},
];
const playerPromises = loops.map(({file, initialVolume}) => new Promise((resolve, reject) => {
const player = new Player(`https://rileyjshaw.com/mp3/loops/${file}.mp3`, () => resolve(player)).toDestination();
player.volume.value = gainToDb(initialVolume);
}));
Promise.all(playerPromises).then(_players => {
players = _players;
players.forEach(player => {
player.sync();
player.start();
});
Transport.bpm.value = 100;
Transport.loop = true;
Transport.loopStart = 0;
Transport.loopEnd = '8:0:0';
ReactDOM.render(<App />, document.getElementById('app'));
});
const nTracks = loops.length;
const add = (a, b) => a + b;
const bound = (n, lo, hi) => Math.max(lo, Math.min(hi, n)) || lo;
const FaderBank = ({values, label, onChange, className}) => <div className={`faderbank ${className ?? ""}`}>
<div className="faders">
{values.map((value, i) => <Fader
key={i}
value={value}
onChange={({target: {value}}) => onChange(+value, i)}
/>)}
</div>
<p className="label">{label}</p>
</div>;
const Fader = ({onChange, value}) => <div className="fader-container">
<input
type="range"
onChange={onChange}
value={value}
min={0}
max={1}
step="any"
className="fader"
/>
</div>;
const App = () => {
const [started, setStarted] = useState(false);
const [linkedFaders, setLinkedFaders] = useState({
volume: loops.map(loop => loop.initialVolume).reduce(add) / nTracks,
tracks: loops.map(loop => loop.initialVolume),
volumeLocked: !loops.some(({initialVolume}) => initialVolume && initialVolume < 1),
});
const [gain, setGain] = useState(0.5);
const updateVolume = newVolume => {
setLinkedFaders(({tracks}) => {
const nActiveTracks = tracks.filter(track => track).length;
const limitedVolume = bound(newVolume, 0, nActiveTracks / nTracks);
const factor = limitedVolume * nTracks / tracks.reduce(add);
return {
volume: limitedVolume,
tracks: tracks.map(unscaled => bound(factor * unscaled, 0, 1)),
volumeLocked: limitedVolume !== newVolume,
};
});
};
const updateTrack = (newValue, i) => {
setLinkedFaders(({volume, tracks}) => {
const newTracksUnscaled = [...tracks.slice(0, i), +newValue, ...tracks.slice(i + 1)];
const factor = (volume * nTracks - newValue) / (newTracksUnscaled.reduce(add) - newValue);
const newTracks = newTracksUnscaled.map((unscaled, j) => i === j ? unscaled : bound(factor * unscaled, 0, 1));
return {
volume: newTracks.reduce(add) / nTracks,
tracks: newTracks,
volumeLocked: !newTracks.some(track => track && track < 1),
};
});
};
const updateGain = (newGain) => setGain(newGain);
// Update track volume.
useEffect(() => {
linkedFaders.tracks.forEach((volume, i) => {
players[i].volume.value = gainToDb(volume);
});
}, linkedFaders.tracks);
// Update master gain.
useEffect(() => {
Destination.volume.value = gainToDb(gain);
}, [gain]);
return <div className="app-container">
{started
? <>
<FaderBank label="Volume" values={[linkedFaders.volume]} onChange={updateVolume} className={linkedFaders.volumeLocked && "locked"} />
<FaderBank label="Tracks" values={linkedFaders.tracks} onChange={updateTrack} />
<FaderBank label="Gain" values={[gain]} onChange={updateGain} />
</>
: <button onClick={() => {
setStarted(true);
Tone.start();
Transport.start();
}}>Start demo</button>}
</div>;
};
Also see: Tab Triggers