Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <!-- https://dribbble.com/shots/11303806-Figma-Smart-Animated-Gameboy -->
<!-- https://www.figma.com/community/file/829134069783438261 -->
<main>
<div id="gameboy">
  <div id="header">
    <div id="light">
      <div id="light__stick"></div>
      <div id="light__pill">
        <div id="light__pill__dots"></div>
        <div id="light__pill__pill"></div>
      </div>
    </div>
  </div>
  <div id="screen">
    <div class="screen__inner">
      <div id="screen__black">
        <div id="face">
          <div class="face__eye"></div>
          <div class="mouth"></div>
          <div class="face__eye"></div>
        </div>
      </div>
    </div>
  </div>
  <div id="gamepad">
    <div id="main-control">
      <div id="d-pad">
        <div id="up"></div>
        <div id="right"></div>
        <div id="bottom"></div>
        <div id="left"></div>
        <div class="gamepad__circle"></div>
      </div>
      <div id="a-and-b">
        <div class="circle-button"></div>
        <div class="circle-button"></div>
      </div>
    </div>
    <div id="start-select">
      <div class="slant-button"></div>
      <div class="slant-button"></div>
    </div>
    <div id="mesh">
      <div class="mesh__pil"></div>
      <div class="mesh__pil"></div>
      <div class="mesh__pil"></div>
      <div class="mesh__pil"></div>
      <div class="mesh__pil"></div>
    </div>
    <div id="gamepad__pill">
      <div id="gamepad__pill__pill"></div>
      <div id="gamepad__pill__dots"></div>
    </div>
  </div>
</div>
<input type="range"
       min="-100"
       max="100"
       value="100"
       step="1"
       id="horizontalRange"
       oninput="changeShadow(this.value)"
       onchange="changeShadow(this.value)">
<input type="range"
       min="0"
       max="35"
       value="0"
       step="1"
       id="verticalRange"
       oninput="changeFacePositionY(this.value)"
       onchange="changeFacePositionY(this.value)">
  <div id="follow-me">
    <label><input type="checkbox" name="follow-me" onclick="validateCheckbox(this)">Follow  Me</label>
  </div>
  <p id="citation">Inspired by <cite><a href="https://dribbble.com/shots/11303806-Figma-Smart-Animated-Gameboy" target="_blank" rel="noopener noreferrer">Figma Smart Animated Gameboy</a></cite></p>
</main>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

:root {
  --border-width: 4px;
  --d-pad-width: 36px;
  --d-pad-height: 38px;
  --horizontal-offset: 0;
}

html, body {
  width: 100%;
  height: 100%;
  position: relative;
  margin: 0;
}

#gameboy {
  grid-area: gameboy;
  background-color: white;
  margin: 0 auto;
  
  position: relative;
  
  border: var(--border-width) solid black;
  
  border-top-left-radius: 5% 3%;
  border-top-right-radius: 5% 3%;
  border-bottom-right-radius: 25% 15%;
  border-bottom-left-radius: 5% 3%;
  
  height: 198px;
  width: 118px;
  
  box-shadow:
    15px 0px 0 0px black,
    20px 0px 0 0px white,
    32px 0px 0 0px black;
}

#header {
  position: absolute;
  width: 100%;
  height: 12px;
  border-bottom: var(--border-width) solid black;
  top: 0;
  background-color: white;
}

#light {
  position: absolute;
  bottom: 0;
  left: 7px;
  width: 25px;
  height: 17px;
/*   background-color: grey; */
}

#light__stick::before {
/*   background-color: pink; */
  box-sizing: border-box;
  content: '';
  width: 10px;
  height: 100%;
  position: absolute;
  left: 0;
  border-left: var(--border-width) solid black;
  border-top: var(--border-width) solid black;
  border-top-left-radius: 100% 13px;
}

#light__stick::after {
/*   background-color: pink; */
  box-sizing: border-box;
  content: '';
  position: absolute;
  left: 10px;
  top: 0px;
  height: 6px;
  width: 6px;
  border-top: var(--border-width) solid black;
  border-right: var(--border-width) solid black;
  border-top-right-radius: 5px 100%;
}

#light__pill {
  position: absolute;
  top: 9px;
  left: 8px;
  width: 22px;
  height: 7px;
}

#light__pill__dots {
  position: absolute;
  top: 0;
  width: 18px;
  left: 50%;
  transform: translateX(-50%);
  height: 3px;
  background-color: black;
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
}

#light__pill__dots::before,
#light__pill__dots::after {
  box-sizing: border-box;
  content: '';
  height: 100%;
  width: 2px;
  background-color: white;
}

#light__pill__pill {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 5px;
  background-color: black;
  border-radius: 3.5px;
}

#header::after {
  content: '';
  position: absolute;
  right: 0;
  width: 7px;
  height: 100%;
  border-left: var(--border-width) solid black;
  border-top-left-radius: 7px 5px;
  border-top: 1px solid black;
}

#screen {
  height: 75px;
  width: 95px;
  top: 22px;
  left: 50%;
  position: absolute;
  transform: translateX(-50%);
  border: var(--border-width) solid black;
  
  border-top-left-radius: 5% 3%;
  border-top-right-radius: 5% 3%;
  border-bottom-right-radius: 25% 15%;
  border-bottom-left-radius: 5% 3%;
}

.screen__inner {
  background-color: white;
  height: 60px;
  width: 74px;
  top: 50%;
  left: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
  border: var(--border-width) solid black;
  position: absolute;
  overflow: hidden;
  border-top-left-radius: 5% 3%;
  border-top-right-radius: 5% 3%;
  border-bottom-right-radius: 25% 15%;
  border-bottom-left-radius: 5% 3%;
}

#screen__black {
  background-color: black;
  border: var(--border-width) solid black;
  
  height: 52px;
  width: 66px;
  left: 10px;
  position: absolute;
}

#face {
  position: absolute;
  display: flex;
  flex-direction: row;
  justify-content: center;
  top: 35px;
  width: 58px;
  height: auto;
  background-color: black;
}

.face__eye {
  background-color: white;
  height: 11px;
  width: 6px;
  border-radius: 3px;
}

.mouth {
  margin-top: 7px;
  margin-left: 7px;
  margin-right: 7px;
  border-radius: 50% 50% 50% 50% / 0% 0% 70% 70%;
  background-color: white;
  width: 11px;
  height: 5px;
}

#main-control {
  position: absolute;
  display: flex;
  flex-direction: row;
  width: 80%;
  height: var(--d-pad-height);
  justify-content: space-between;
  left: 50%;
  bottom: 41px;
  transform: translateX(-50%);
}

#d-pad {
  width: var(--d-pad-width);
  height: var(--d-pad-height);
  
  position: relative;
}

#up, #bottom, #left, #right {
  background-color: white;
  border-style: solid;
  border-color: black;
}

#up, #bottom {
  width: calc(var(--d-pad-width)/2);
  height: calc(var(--d-pad-height)/3);
}

#left, #right {
  width: calc(var(--d-pad-height)/3);
  height: calc(var(--d-pad-width)/2);
}

#up {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  
  border-bottom-style: none;
  
  border-top-width: var(--border-width);
  border-left-width: var(--border-width);
  border-right-width: var(--border-width);
  
  border-radius: 33.33% 33.33% 0 0;
  
  box-shadow: 3px 0 0px 0 black;
}

#right {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  
  border-left-style: none;
  
  border-top-width: var(--border-width);
  border-right-width: var(--border-width);
  border-bottom-width: var(--border-width);
  
  border-radius: 0 33.33% 33.33% 0;
  box-shadow: 3px 0 0px 0 black;
}

#bottom {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform-origin: center center;
  transform: translateX(-50%);
  
  border-top-style: none;
  
  border-right-width: var(--border-width);
  border-bottom-width: var(--border-width);
  border-left-width: var(--border-width);
  
  border-radius: 0 0 33.33% 33.33%;
  
  box-shadow: 3px 0 0px 0 black;
}

#left {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  
  border-right-style: none;
  
  border-top-width: var(--border-width);
  border-bottom-width: var(--border-width);
  border-left-width: var(--border-width);
  
  border-radius: 33.33% 0 0 33.33%;
}

.gamepad__circle {
  background-color: black;
  width: 7px;
  height: 7px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 100%;
}

#a-and-b {
  width: var(--d-pad-width);
  height: var(--d-pad-height);
  position: relative;
}

.circle-button:first-child {
  position: absolute;
  top: 0;
  right: 0;
}

.circle-button:last-child {
  position: absolute;
  bottom: 0;
  left: 0;
}

.circle-button {
  background-color: white;
  border-radius: 100%;
  border: var(--border-width) solid black;
  width: 20px;
  height: 20px;
  box-shadow: 3px 0px 0 0px black;
}

#start-select {
  position: absolute;
  display: flex;
  flex-direction: row;
  width: 30px;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%);
}

.slant-button {
  background-color: black;
  height: 4px;
  width: 20px;
  border-radius: 2px;
  transform: rotate(-45deg);
}

#mesh {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 20px;
  height: 33px;
  transform: rotate(60deg);
  position: absolute;
  bottom: 7px;
  right: 13px;
}

.mesh__pil {
  background-color: black;
  height: 4px;
  width: 100%;
  border-radius: 2px;
}

#gamepad__pill {
  position: absolute;
  width: 20px;
  height: 9px;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

#gamepad__pill__pill {
  position: absolute;
  width: 100%;
  height: 5px;
  top: 0;
  background-color: black;
  border-radius: 2.5px;
}

#gamepad__pill__dots {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  background-color: black;
  height: 50%;
  width: 70%;
}

#gamepad__pill__dots::before {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  background-color: white;
  height: 100%;
  width: 30%;
}

input[type=range] {
  -webkit-appearance: none;
  background: transparent;
  width: 200px;
  height: 4px;
}

#horizontalRange {
  grid-area: hslider;
}

#horizontalRange::-webkit-slider-thumb {
  margin-top: -8px;
}

#horizontalRange::-webkit-slider-runnable-track {
  height: 4px;
}

#horizontalRange::-moz-range-track {
  height: 4px;
}

#verticalRange {
  grid-area: vslider;
  -webkit-appearance: none;
  transform: rotate(-90deg);
}

#verticalRange::-webkit-slider-thumb {
  margin-top: -8px;
  box-shadow: -2px 0px 0 0px black;
}

#verticalRange::-moz-range-thumb {
  box-shadow: -2px 0px 0 0px black;
}

#verticalRange::-webkit-slider-runnable-track {
  height: 4px;
}

#verticalRange::-moz-range-track {
  height: 4px;
}

input[type=range]::-webkit-slider-runnable-track {
  cursor: pointer;
  background-color: black;
  border-radius: 2px;
}

input[type=range]::-moz-range-track {
  cursor: pointer;
  background-color: black;
  border-radius: 2px;
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  border: var(--border-width) solid black;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  background-color: white;
  cursor: pointer;
  box-shadow: 0px 2px 0 0px black;
  box-sizing: border-box;
}

input[type=range]::-moz-range-thumb {
  box-sizing: border-box;
  cursor: pointer;
  background-color: white;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  border: var(--border-width) solid black;
  box-shadow: 0px 2px 0 0px black;
}

#citation {
  grid-area: citation;
  word-break: break-all;
}

#follow-me {
  grid-area: follow-me;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  display: flex;
  flex-direction: column;
}

#follow-me > label {
  font-size: 2em;
}

#follow-me input {
  margin-inline-end: 8px;
  transform: rotate(90deg);
}

main {
  width: 240px;
  height: 100%;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-template-areas:
    ". . ."
    "follow-me gameboy vslider"
    ". hslider ."
    ". citation ."
    ". . .";
  grid-template-rows: 1fr 200px 40px 60px 1fr;
  grid-template-columns: 40px 200px 40px;
  align-items: center;
  justify-items: center;
}
              
            
!

JS

              
                const gameboy = document.getElementById("gameboy"),
      screenBlack = document.getElementById("screen__black"),
      horizontalRange = document.getElementById("horizontalRange"),
      verticalRange = document.getElementById("verticalRange"),
      aAndBs = document.getElementsByClassName("circle-button"),
      face = document.getElementById("face"),
      up = document.getElementById("up"),
      right = document.getElementById("right"),
      bottom = document.getElementById("bottom"),
      left = document.getElementById("left");

let horizontalRangeRect = horizontalRange.getBoundingClientRect();
let verticalRangeRect = verticalRange.getBoundingClientRect();

// https://gist.github.com/vectorsize/7031902
const scale = function(opts){
  const istart = opts.domain[0],
        istop  = opts.domain[1],
        ostart = opts.range[0],
        ostop  = opts.range[1];
  
  return function scale(value) {
    return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
  }
};

const rightButtonScale = scale({
  domain: [0, 100],
  range: [0, 3]
});

const leftButtonScale = scale({
  domain: [-100, 0],
  range: [-3, 0]
});

function changeShadow(value) {
  gameboy.style.boxShadow = `
    ${15 * value / 100}px 0px 0 0px black,
    ${20 * value / 100}px 0px 0 0px white,
    ${32 * value / 100}px 0px 0 0px black
  `;
  screenBlack.style.left = `${value/10}px`;
  up.style.boxShadow = `${3 * value/100}px 0px 0 0px black`;
  bottom.style.boxShadow = `${3 * value/100}px 0px 0 0px black`;
  if (value > 0) {
    right.style.boxShadow = `${rightButtonScale(value)}px 0px 0 0px black`;
    left.style.boxShadow = "0px 0 0px black";
  } else {
    right.style.boxShadow = "0px 0 0px black";
    left.style.boxShadow = `${leftButtonScale(value)}px 0px 0 0px black`;
  }
  aAndBs[0].style.boxShadow = `${3 * value/100}px 0px 0 0px black`;
  aAndBs[1].style.boxShadow = `${3 * value/100}px 0px 0 0px black`;
}

function changeFacePositionY(value) {
  face.style.top = `${35 - value}px`;
}

// https://stackoverflow.com/a/18953446
function round5(x)
{
    return Math.ceil(x/5)*5;
}

window.addEventListener("resize", function(e) {
  horizontalRangeRect = horizontalRange.getBoundingClientRect();
  verticalRangeRect = verticalRange.getBoundingClientRect();
});

function handleHorizontalRange(clientX) {
  if (clientX < horizontalRangeRect.left) {
    horizontalRange.value = -100;
    changeShadow(-100);
    return;
  }
  
  if (clientX > horizontalRangeRect.right) {
    horizontalRange.value = 100;
    changeShadow(100);
    return;
  }
  
  const xScale = scale({
    domain: [horizontalRangeRect.left, horizontalRangeRect.right],
    range: [-100, 100]
  });
  
  horizontalRange.value = xScale(clientX);
  changeShadow(xScale(clientX));
}

function handleVerticalRange(clientY) {
  if (clientY < verticalRangeRect.top) {
    verticalRange.value = 35;
    changeFacePositionY(35);
    return;
  }
  
  if (clientY > verticalRangeRect.bottom) {
    verticalRange.value = 0;
    changeFacePositionY(0);
    return;
  }
  
  const yScale = scale({
    domain: [verticalRangeRect.bottom, verticalRangeRect.top],
    range: [0, 35]
  });
  
  verticalRange.value = yScale(clientY);
  changeFacePositionY(yScale(clientY));
}

function handleMouseMove(e) {
  handleHorizontalRange(e.clientX);
  handleVerticalRange(e.clientY);
}

function validateCheckbox(element) {
  if (element.checked) {
    document.body.addEventListener("mousemove", handleMouseMove);
    horizontalRange.style.display = "none";
    verticalRange.style.display = "none";
  } else {
    document.body.removeEventListener("mousemove", handleMouseMove);
    horizontalRange.style.display = "block";
    verticalRange.style.display = "block";
  }
}
              
            
!
999px

Console