css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <svg id="simonsvg" width="480" height="480" viewBox="0,0,480,480">
  <defs>
    <pattern id="metalimg" patternUnits="userSpaceOnUse" width="450" height="450">
      <image xlink:href="https://i.imgur.com/FphJXKt.jpg" x="0" y="0" width="450" height="450"></image>
    </pattern>
    <style type="text/css">
   <![CDATA[
    @font-face {
        font-family: Orbitron;
        src: url('https://fonts.googleapis.com/css?family=Orbitron');
    }
   ]]>
 </style>
  </defs>
  <filter id="dropshadow" height="130%">
  <feGaussianBlur in="SourceAlpha" stdDeviation="3"/> <!-- stdDeviation is how much to blur -->
  <feOffset dx="2" dy="2" result="offsetblur"/> <!-- how much to offset -->
  <feMerge> 
    <feMergeNode/> <!-- this contains the offset blurred image -->
    <feMergeNode in="SourceGraphic"/> <!-- this contains the element that the filter is applied to -->
  </feMerge>
</filter>
  <circle  r="225" cx="240" cy="240" fill="#333333" style="filter:url(#dropshadow)" />
  <path id="redbutton" name="red" fill="#9F0F17" d="M 255,30 l 0,100 A 115,115 0 0,1 350,225 L 450,225 A 215,215 0 0,0 255,30 z" />
  <path id="greenbutton" name="green" fill="#03A64B" d="M 225,30 l 0,100 A 115,115 0 0,0 130,225 L 30,225 A 215,215 0 0,1 225,30 z" />
  <path id="bluebutton" name="blue" fill="#094A8F" d="M 450,255 l -100,0 A 110,110 0 0,1 255,350 L 255,450 A 210,210 0 0,0 450,255 z" />
  <path id="yellowbutton" name="yellow" fill="#CBA60C" d="M 30,255 l 100,0 A 110,110 0 0,0 225,350 L 225,450 A 210,210 0 0,1 30,255 z" />
  <circle fill="url(#metalimg)"  r="100" cx="240" cy="240" />
  <text x="161" y="215" font-family="'Orbitron'" font-size="44px" font-weight="bold">Simon</text>
  <text x="307" y="195" font-family="sans-serif" font-size="18px" font-weight="bold">&reg;</text>
  <path name="scoreboard" fill="#30050D" d="M 160,265 a 8 8 0 0,1 -8,-8 l 0,-25 a 8 8 0 0,1 8,-8 l 45,0 a 8 8 0 0,1 8,8 l 0,25 a 8 8 0 0,1 -8,8 l-45,0 z" />
  <text id="myscore" x="183" y="245" font-family="'Orbitron'" font-size="32px"  stroke="#FD0000" fill="#FD0000" text-anchor="middle" alignment-baseline="central"></text>
  <circle  r="18" cx="244" cy="246" fill="#333333" />
  <circle  r="12" cx="244" cy="246" fill="#FD0000" name="start" />
  <circle  r="18" cx="300" cy="246" fill="#333333" />
  <circle  r="12" cx="300" cy="246" fill="#FFFF00" name="strict" />
  <circle  r="7" cx="326" cy="236" fill="#333333" />
  <circle id="strictLED" r="4" cx="326" cy="236" fill="#30050D" />
  <path name="onoff" fill="#202020" d="M 220,318 a 2 2 0 0,1 -2,-2 l 0,-20 a 2 2 0 0,1 2,-2 l 40,0 a 2 2 0 0,1 2,2 l 0,20 a 2 2 0 0,1 -2,2 l-40,0 z" />
  <line id="onoffswitch" name="onoffswitch" class="onoffswitch" fill="#3F96F3" stroke="#3F96F3" stroke-width="16" stroke-miterlimit="16" x1="222" y1="306" x2="258" y2="306" />
  <text x="163" y="278" font-family="Arial" font-size="12px" font-weight="bold">COUNT</text>
  <text x="228" y="278" font-family="Arial" font-size="12px" font-weight="bold">START</text>
  <text x="285" y="278" font-family="Arial" font-size="12px" font-weight="bold">STRICT</text>
  <text x="192" y="311" font-family="Arial" font-size="12px" font-weight="bold">OFF</text>
  <text x="269" y="311" font-family="Arial" font-size="12px" font-weight="bold">ON</text>
</svg>
            
          
!
            
              html, body {
  width: 100%;
  height: 100%;
  border: 0;
  margin: 0;
  padding: 0;
  background-image: url('http://raykendo.com/blog/wp-content/uploads/2016/10/wood_floor_by_gnrbishop.jpg');
  overflow: hidden;
}

svg {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -240px 0 0 -240px;
}

svg text {
    pointer-events: none;
}

svg.nightmare {
  animation-name: spinaround; 
  animation-duration: 2s; 
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

.onoffswitch {
  stroke-dasharray: 16, 24;
}
.turnon {
  stroke-dashoffset: -20;
}

@keyframes dash {
  to {
    stroke-dashoffset: -20;
  }
}

@keyframes spinaround {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
            
          
!
            
              (function() {
  var gameSVG = document.getElementById("simonsvg"),
    onOffSwitch = document.getElementById("onoffswitch"),
    scoreNode = document.getElementById("myscore"),
    redButton = document.getElementById("redbutton"),
    blueButton = document.getElementById("bluebutton"),
    greenButton = document.getElementById("greenbutton"),
    yellowButton = document.getElementById("yellowbutton"),
    strictLED = document.getElementById("strictLED"),
    NS = gameSVG.getAttribute('xmlns'),
    appState = {
      on: false,
      strict: false,
      locked: false,
      nightmareMode: false,
      count: 0,
      step: 0,
      moves: [],
      highlightTime: 600,
      moveWidth: 1000,
      maxMoves: 20
    },
    mouseDownListener, touchStartListener, mouseUpListener, touchEndListener, nightmareTimer, winningInterval;

  function getRandomMoves(maxNum) {
    var list = ["red", "blue", "green", "yellow"],
      answer = [],
      i, item;

    for (i = 0; i < maxNum; i++) {
      item = Math.floor(Math.random() * 4);
      answer.push(list[item]);
    }

    return answer;
  }

  function youWin() {
    var oscillator = true,
      oscillator2 = true;
    winningInterval = window.setInterval(function() {

      if (oscillator) {
        updateScore(appState.moves.length);
      } else {
        updateScore("  ");
      }

      if (oscillator2) {
        highlight(blueButton, "#3F96F3", "#094A8F", appState.highlightTime * 2);
        highlight(greenButton, "#46FB98", "#03A64B", appState.highlightTime * 2);
      } else {
        highlight(redButton, "#F05C63", "#9F0F17", appState.highlightTime * 2);
        highlight(yellowButton, "#F7DE78", "#CBA60C", appState.highlightTime * 2);
      }

      oscillator = !oscillator;
      if (oscillator) {
        oscillator2 = !oscillator2;
      }
    }, appState.highlightTime);
  }

  function resetBoard() {
    redButton.setAttributeNS(NS, "fill", "#9F0F17");
    blueButton.setAttributeNS(NS, "fill", "#094A8F");
    greenButton.setAttributeNS(NS, "fill", "#03A64B");
    yellowButton.setAttributeNS(NS, "fill", "#CBA60C");
    window.clearInterval(winningInterval);
  }

  function startGame() {
    resetBoard();

    // start with appState;
    appState.count = 1;
    appState.moves = getRandomMoves(appState.maxMoves);
    window.setTimeout(startRound, appState.moveWidth);
  }

  function startRound() {
    // lock play controls while displaying moves
    appState.locked = true;
    // reset user step to 0
    appState.step = 0;
    // show score 
    updateScore(appState.count);
    // play moves
    playMoves(appState.moves.slice(0, appState.count));
  }

  function playMoves(moves) {
    if (!moves || !moves.length) {
      appState.locked = false;
      return;
    }
    var currentMove = moves.shift();
    switch (currentMove) {
      case "red":
        highlight(redButton, "#F05C63", "#9F0F17", appState.highlightTime);
        new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3").play();
        break;
      case "blue":
        highlight(blueButton, "#3F96F3", "#094A8F", appState.highlightTime);
        new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3").play();
        break;
      case "green":
        highlight(greenButton, "#46FB98", "#03A64B", appState.highlightTime);
        new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3").play();
        break;
      case "yellow":
        highlight(yellowButton, "#F7DE78", "#CBA60C", appState.highlightTime);
        new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3").play();
        break;
      default:
        alert("Unknown current move: " + currentMove);
    }

    setTimeout(function() {
      playMoves(moves);
    }, appState.moveWidth);
  }

  function highlight(node, highlightColor, regularColor, timer) {
    node.setAttributeNS(NS, "fill", highlightColor);

    if (timer && timer > 0) {
      setTimeout(function() {
        node.setAttributeNS(NS, "fill", regularColor);
      }, timer);
    }
  }

  /**
   * @function clickToPlay
   * @param {object} data
   * @param {object} data.button - button to change
   * @param {string} data.highlight - highlight hex color
   * @param {string} data.color - name of color selected
   * @param {string} data.sound - url of the sound to play when the button is clicked.
   */
  function clickToPlay(data) {
    if (appState.on && !appState.locked) {
      data.button.setAttributeNS(NS, "fill", data.highlight);
      new Audio(data.sound).play();
      if (appState.moves.length === 0) {
        return;
      }

      if (appState.moves[appState.step] === data.color) {
        appState.step++;

        if (appState.step === appState.moves.length) {
          youWin();
        } else if (appState.step === appState.count) {
          setTimeout(function() {
            appState.count++;
            startRound();
          }, appState.moveWidth);
        }
      } else {
        updateScore("!!");
        setTimeout(function() {
          if (appState.strict) {
            startGame();
          } else {
            startRound();
          }
        }, appState.moveWidth);
      }
    }

  }

  function letGoRed() {
    if (appState.on) {
      redButton.setAttributeNS(NS, "fill", "#9F0F17");
    }
  }

  function letGoBlue() {
    if (appState.on) {
      blueButton.setAttributeNS(NS, "fill", "#094A8F");
    }
  }

  function letGoGreen() {
    if (appState.on) {
      greenButton.setAttributeNS(NS, "fill", "#03A64B");
    }
  }

  function letGoYellow() {
    if (appState.on) {
      yellowButton.setAttributeNS(NS, "fill", "#CBA60C");
    }
  }

  function clickStart() {
    if (appState.on) {
      startGame();
      nightmareTimer = window.setTimeout(toggleNightmare, 5 * 1000);
    }
  }

  function letGoStart() {
    if (nightmareTimer) {
      window.clearTimeout(nightmareTimer);
    }
    //if (appState.on) { }
  }

  function clickStrict() {
    if (appState.on) {
      appState.strict = !appState.strict;
      if (appState.strict) {
        // todo: turn on LED svg to show strict appState
        strictLED.setAttributeNS(NS, "fill", "#FD0000");
      } else {
        // todo: turn off LED svg to show strict appState
        strictLED.setAttributeNS(NS, "fill", "#30050D");
      }
    }
  }

  function letGoStrict() {
    //if (appState.on) { }
  }

  function toggleNightmare() {
    appState.nightmareMode = !appState.nightmareMode;
    if (appState.nightmareMode) {
      alert("Nightmare mode enabled!");
      gameSVG.setAttributeNS(NS, "class", "nightmare");
    } else {
      alert("Nightmare mode disabled!");
      gameSVG.setAttributeNS(NS, "class", "");
    }
  }

  function toggleOn(switchSVG) {
    appState.on = !appState.on;
    resetBoard();
    strictLED.setAttributeNS(NS, "fill", "#30050D");
    if (appState.on) {
      onOffSwitch.setAttributeNS(NS, "class", "onoffswitch turnon");
      updateScore("--");
    } else {
      onOffSwitch.setAttributeNS(NS, "class", "onoffswitch");
      // reset appState;
      appState.count = 0;
      appState.moves = [];
      appState.strict = false;
      appState.nightmareMode = false;
      appState.locked = false;
      updateScore("  ");
      gameSVG.setAttributeNS(NS, "class", "");
    }
  }

  function updateScore(value) {
    if (typeof value === "string") {
      scoreNode.textContent = value;
    } else if (typeof value === "number") {

      scoreNode.textContent = parseInt(value % 100 / 10, 10) + parseInt(value % 10, 10);
    }
  }

  function onClick(evt) {
    var target, name;
    if (evt.target) {
      target = evt.target;
      name = target.getAttributeNS(null, "name");
      if (!name) {
        return;
      }
      switch (name) {
        case "blue":
          clickToPlay({
            color: name,
            button: blueButton,
            sound: "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3",
            highlight: "#3F96F3"
          });
          break;
        case "red":
          clickToPlay({
            color: name,
            button: redButton,
            sound: "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3",
            highlight: "#F05C63"
          });
          break;
        case "green":
          clickToPlay({
            color: name,
            button: greenButton,
            sound: "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3",
            highlight: "#46FB98"
          });
          break;
        case "yellow":
          clickToPlay({
            color: name,
            button: yellowButton,
            sound: "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3",
            highlight: "#F7DE78"
          });
          break;
        case "onoff":
        case "onoffswitch":
          toggleOn(target);
          break;
        case "start":
          clickStart();
          break;
        case "strict":
          clickStrict();
          break;
      }
    }
  }

  function letItGo(evt) {
    var target, name;
    if (evt.target) {
      target = evt.target;
      name = target.getAttributeNS(null, "name");
      if (!name) {
        return;
      }
      switch (name) {
        case "blue":
          letGoBlue();
          break;
        case "red":
          letGoRed();
          break;
        case "green":
          letGoGreen();
          break;
        case "yellow":
          letGoYellow();
          break;
        case "start":
          letGoStart(target);
          break;
        case "strict":
          letGoStrict(target);
          break;
      }
    }
  }

  function mouseClick(evt) {
    if (touchStartListener && touchEndListener) {
      gameSVG.removeEventListener("touchstart", touchStartListener);
      gameSVG.removeEventListener("touchend", touchEndListener);
    }
    onClick(evt);

  }

  function touchClick(evt) {
    if (mouseDownListener && mouseUpListener) {
      gameSVG.removeEventListener("mousedown", mouseDownListener);
      gameSVG.removeEventListener("mouseup", mouseUpListener);
    }
    onClick(evt);
  }

  mouseDownListener = gameSVG.addEventListener("mousedown", mouseClick);
  touchStartListener = gameSVG.addEventListener("touchstart", touchClick);
  mouseUpListener = gameSVG.addEventListener("mouseup", letItGo);
  touchEndListener = gameSVG.addEventListener("touchend", letItGo);

})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console