Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="background_layers">
 <div class="svg_layer bg"></div>
 <div class="svg_layer mg"></div>
 <div class="svg_layer fg"></div>
 <div class="svg_layer road"></div>
</div>

<div class='bm_container'></div>

<div class="angle_container">
 <div class="dial"></div>
</div>
              
            
!

CSS

              
                html,
body {
 width: 100vw;
 height: 100vh;
 margin: 0;
 background-color: #61e8e1;
 overflow: hidden;
 position: relative;
}

.bm_container {
 width: 100%;
 height: 60%;
 left: 0;
 right: 0;
 bottom: 0;
 top: 0;
 margin: auto;
 position: absolute;
}

.background_layers {
 position: absolute;
 top: 0;
 left: 0;
 right: 0;
 bottom: 0;
 width: 100%;
 height: 100%;
 .svg_layer {
  position: absolute;
  width: 200%;
  height: 200%;
  left: -50%;
  right: 0;
  bottom: 0;
 }
 .fg {
  top: 25%;
  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/codevember11_fg.svg")
   repeat-x;
  background-position: 0px 100px;
  background-size: 1920px;
  transform-origin: center 25%;
 }
 .mg {
  top: 20%;
  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/codevember11_mg.svg")
   repeat-x;
  background-position: 0px 0px;
  background-size: 1920px;
  transform-origin: center 20%;
 }
 .bg {
  top: 0%;
  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/codevember11_bg.svg")
   repeat-x;
  background-size: 1920px;
 }
 .road {
  background-color: grey;
  top: 70%;
  transform-origin: top;
 }
}

.angle_container {
 position: absolute;
 bottom: 0;
 height: 20%;
 width: 100%;
 .dial {
  height: 100px;
  width: 100px;
  margin: auto;
  bottom: 5px;
  position: absolute;
  left: 0;
  right: 0;
  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/cyclist_dial.svg")
   no-repeat;
  background-position: bottom;
 }
}

.dragSVG {
 position: absolute;
 width: 100%;
 height: 100%;
 top: 0;
 left: 0;
}

.dragger {
 -webkit-tap-highlight-color: transparent;
}

              
            
!

JS

              
                var animData = {
 wrapper: document.querySelector(".bm_container"),
 animType: "svg",
 loop: true,
 prerender: true,
 autoplay: true,
 path: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/codevember9.json"
};

var anim = bodymovin.loadAnimation(animData);
anim.addEventListener("DOMLoaded", startAnimation);
anim.setSpeed(1);
bodymovin.setSubframeRendering(false);

var dial = document.querySelector(".dial"),
    useDial = false,
    dragDial,
    road = document.querySelector(".road"),
    fg = document.querySelector(".fg"),
    fgXDistance = 0,
    fgYDistance = 100,
    mg = document.querySelector(".mg"),
    mgXDistance = 0,
    mgYDistance = 0,
    bg = document.querySelector(".bg"),
    bgXDistance = 0,
    bgYDistance = 0,
    animationWindow = document.querySelector(".bm_container"),
    rotateMin = -45,
    rotateMax = 45,
    maxRunSpeed = 1.5,
    minRunSpeed = 0.2,
    beta, 
    gamma,
    rotX = 0,
    rotY = 0,
    runSpeed,
    dragPercent,
    segment04 = false,
    isChanging = false,
    goingFast = false;

dragDial = Draggable.create(dial, {
 type: "rotation",
 bounds: { minRotation: rotateMin, maxRotation: rotateMax },
 onDrag: onTilt
});

function onTilt() {
 useDial = true;
 rotateAll();
}

function rotateAll() {
 dragPercent = (dial._gsTransform.rotation + rotateMax) / (rotateMax * 2);
 runSpeed = dragPercent * maxRunSpeed + minRunSpeed;
 anim.setSpeed(runSpeed);
 hillRotation();
}

function changeComplete() {
 isChanging = false;
 anim.removeEventListener("loopComplete", changeComplete);
}

function goFast() {
 if (isChanging) {
  return;
 } else if (goingFast) {
  return;
 } else {
  isChanging = true;
  goingFast = true;
  anim.playSegments([[25, 35], [35, 60]], true);
  anim.addEventListener("loopComplete", changeComplete);
 }
}

function goSlow() {
 if (isChanging) {
  return;
 } else if (goingFast) {
  isChanging = true;
  goingFast = false;
  anim.playSegments([[60, 70], [0, 25]], true);
  anim.addEventListener("loopComplete", changeComplete);
 }
}

window.onload = function() {
 window.requestAnimationFrame(loop);
};

function loop() {
 window.requestAnimationFrame(loop);
 let speed = 1;
 if (runSpeed > 1.2) {
  // console.log("FAST SEGMENT");
  speed = 10;
  goFast();
 }

 if (runSpeed < 1.2 && runSpeed > 0.5) {
  // console.log("SLOW SEGMENT");
  speed = 5;
  goSlow();
 }
 
 fgXDistance -= 1 * speed;
 fg.style.backgroundPosition = `${fgXDistance}px ${fgYDistance}px`;
 
 mgXDistance -= 0.5 * speed;
 mg.style.backgroundPosition = `${mgXDistance}px ${mgYDistance}px`;
 
 bgXDistance -= 0.1 * speed;
 bg.style.backgroundPosition = `${bgXDistance}px ${bgYDistance}px`;
}

function hillRotation() {
 let svg = animData.wrapper;
 const degrees = rotateMin + (dragPercent * (rotateMax * 2));
 svg.style.transform = `rotate(${degrees}deg)`;
 
 road.style.transform = `rotate(${degrees}deg)`;
 fg.style.transform = `rotate(${degrees * 0.9}deg)`;
 mg.style.transform = `rotate(${degrees * 0.1}deg)`;
}

function startAnimation() {
 anim.playSegments([[0, 25]], true);
}

if ( window.DeviceMotionEvent ) { 
 window.ondeviceorientation = function(e) {
  gamma = e.gamma;
  setTimeout(function(){
   normalizeRotation(gamma)
  }, 100)
 }  
}

function normalizeRotation(horizontalRotation){
 if(useDial) return;
 g = Math.round(horizontalRotation);

 rotY += (g - rotY) / 10;
 let normalRot = rotY;
 if(normalRot >= rotateMax) normalRot = rotateMax;
 if(normalRot <= rotateMin) normalRot = rotateMin;

 dial._gsTransform.rotation = Math.round(normalRot);
 dial.style.transform = `rotate(${Math.round(normalRot)}deg)`;
 // dragDial[0].update();
 rotateAll();
}
              
            
!
999px

Console