JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<button id="btnHelixStart" class="big-button">Go go helix!</button>
<div id="helix-container">
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
<span class="segment"> </span>
</div>
.big-button {
display: block;
font-size: 30px;
font-weight: bold;
margin: 15px auto 10px;
height: 60px;
width: 420px;
}
.segment {
background-color: #05f;
clear: both;
float: left;
left: 0px;
position: relative;
height: 20px;
width: 20px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
#helix-container {
background: #eaeaea;
border: solid 1px #000;
overflow: hidden;
margin: 0px auto 40px;
padding: 50px;
width: 320px;
}
const CYCLE_SPEED = 1500;
const HELIX_WIDTH = 300;
const SEGMENT_BUFFER = 0.2;
let btnHelixStart;
let helixIsInitted;
let helixIsRunning;
let startTime;
let pausedAtTime;
let segmentContainer;
let segments;
let stepStateLists;
let updateHandle;
let isPlaying;
let shiftyDemo;
const now = () => +(new Date());
const getPosition = (loopPosition, startBuffer) => {
let moddedLoopPosition;
let interpolator;
let interpolatedValue;
if ((loopPosition - startBuffer) < 0) {
return 0;
}
interpolator = moddedLoopPosition = ((loopPosition - startBuffer) % 2);
if (moddedLoopPosition > 1) {
interpolator = moddedLoopPosition - 1;
}
interpolatedValue = shifty.interpolate(
{ left: 0 }, { left: HELIX_WIDTH }, interpolator, 'easeInOutSine');
return moddedLoopPosition > 1 ?
{ left: HELIX_WIDTH - interpolatedValue.left } :
interpolatedValue;
}
const updateSegment = (index, loopPosition) => {
let segment = segments[index];
let calculated = getPosition(loopPosition, index * SEGMENT_BUFFER);
const { style } = segment;
style.MozTransform = style.webkitTransform =
`translateX(${parseFloat(calculated.left)}px)`;
}
const updateSegments = (startTime, callback) => {
let timeDelta = now() - startTime;
let normalizedTime = timeDelta / CYCLE_SPEED;
for (let i = 0; i < segments.length; i++) {
updateSegment(i, normalizedTime);
}
callback();
}
const loop = () => {
updateHandle = setTimeout(function () {
updateSegments(startTime, loop);
}, 1000 / 60);
}
const pause = () => {
if (!isPlaying) {
return;
}
isPlaying = false;
pausedAtTime = now();
clearTimeout(updateHandle);
}
const play = () => {
if (isPlaying) {
return;
}
isPlaying = true;
startTime = now() - (pausedAtTime - startTime);
loop();
}
const init = containerId => {
pausedAtTime = startTime = now();
segmentContainer = document.getElementById(containerId);
segments = segmentContainer.children;
stepStateLists = [];
for (let i = 0; i < segments.length; i++) {
stepStateLists.push([])
}
window.pause = pause;
window.play = play;
}
const toggleHelix = () => {
if (!helixIsInitted) {
helixIsInitted = true;
shiftyDemo.helixInit('helix-container');
}
if (helixIsRunning) {
shiftyDemo.helixPause();
btnHelixStart.innerHTML = 'Go go helix!';
} else {
shiftyDemo.helixPlay();
btnHelixStart.innerHTML = 'Stop stop helix!';
}
helixIsRunning = !helixIsRunning;
}
shiftyDemo = {
helixInit: init,
helixPlay: play,
helixPause: pause
};
btnHelixStart = document.getElementById('btnHelixStart');
btnHelixStart.addEventListener('click', toggleHelix, true);
helixIsRunning = false;
helixIsInitted = false;
Also see: Tab Triggers