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 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

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

              
                <section id="container"></section>
<section id="output"></section>

              
            
!

CSS

              
                html, body {
  height: 100%;
}  

body {
  background: #111;
  display: flex;
  flex-direction: row;
}

section {
  height: 100%;
  box-sizing: border-box;
}

#container {
  padding-top: 3rem;
  width: 76%;
  overflow: auto;
}

#output {
  width: 24%;
}

.chunk + .chunk {
  margin-top: 1rem;
}

$w: 27rem;
.chunk {
  position: relative;
  width: $w;
  transform: translateX(-4rem);
  margin-left: auto;
  margin-right: auto;
  font-family: Menlo, "Andale Mono", monospace;
  &:before {
    position: absolute;
    right: 100%;
    top: 0.25rem;
    bottom: 0.25rem;
    font-size: 0.7em;
    padding-right: 0.5rem;
    color: rgba(#FFF,0.5);
  }
}

.task {
  display: block;
  position: relative;
  $indent: 1rem;
  width: 100%;
  margin: 0 auto;
  font-size: 0.875em;
  padding: 0 $indent;
  line-height: 1.4;
  @for $i from 0 through 4 {
    &.indent-#{$i} { 
      padding-left: $indent * $i + 1;
      width: $w - $indent * $i;
    }
  }
  
  color: white;
  opacity: 0.5;
  
  &.past,
  &.present {
    opacity: 1;
  }
  
  .node {
    opacity: 0.5;
  }
  .node.past,
  .node.present {
    opacity: 1;
  }
  
  .node.present {
    border-bottom: 1px solid white;
  }
  
  .notes {
    position: absolute;
    left: calc(100% + 2rem);
    top: 0rem;
    padding: 0.5rem 1rem;
    background: #222;
    width: 8rem;
    opacity: 0;
    font-size: 0.8em;
    transition: left 500ms ease-in-out,
      opacity 300ms ease-in;
    p { margin: 0; }
    &:after {
      content: "";
      position: absolute;
      top: 0.25em;
      left: -8px;
      height: 0;
      border-top: 6px solid transparent;
      border-bottom: 6px solid transparent;
      border-right: 8px solid #222;
    }
  }
  &.present .notes {
    opacity: 1;
    left: calc(100% + 1rem);
    visibility: visible;
  }
}

$c-1: #692DAC;
$c-2: #9D538E;
$c-3: #34ACAF;
.task.past,
.task.present {
  background-color: #222;
}
.type-html {
  &:before { content: "html" }
  .node.present { background: $c-1; }
  .node.past { color: lighten($c-1, 20%); }
  .node.miss { background-color: darken($c-1, 10%); }
}
.type-css {
  &:before { content: "css" }
  .node.present { background: $c-2; }
  .node.past { color: lighten($c-2, 20%); }
  .node.miss { background-color: darken($c-2, 10%); }
}
.type-javascript {
  &:before { content: "js" }
  .node.present { background: $c-3; }
  .node.past { color: lighten($c-3, 20%); }
  .node.miss { background-color: darken($c-3, 10%); }
}


#output {
  padding: 2rem;
  background: #F0F0F0;
}
              
            
!

JS

              
                var lesson = new Lesson({
  song: getSong(),
  container: document.getElementById("container"),
  result: document.getElementById("output"),
  chunks: [
    {
      type: "html",
      codeblock: [
        { c: "<div>", i: 0, n: "Let's create a div element." },
        { c: "<h1 id=\"our-h1\">Learning!</h1>", i: 1, n: "We create an h1 element with an id." },
        { c: "</div>", i: 0, n: "We close the parent div element. When you do this, it will add your h1 to the area on the right." }
      ]
    },
    {
      type: "css",
      codeblock: [
        { c: "h1 {", i: 0, n: "We can select the h1 and open up a new CSS declaration block." },
        { c: "color: #FFBB00;", i: 1, n: "We can now make our h1 yellow."},
        { c: "}", i: 0, n: "Now we close the declaration block. This will add the style to the pane on the right." },
      ]
    },
    {
      type: "css",
      codeblock: [
        { c: "#output {", i: 0, n: "We can select the output block to the right by its id and create a new declaration block." },
        { c: "text-align: center;", i: 1, n: "Let's give it center alignment."},
        { c: "background-color: #444;", i: 1, n: "And change the background color."},
        { c: "}", i: 0, n: "Now we'll close the declaration block." }
      ]
    },
    {
      type: "javascript",
      codeblock: [
        { c: "function ourFirstFN() {", i: 0, n: "Let's name a new JavaScript function." },
        { c: "var our_h1 = document.getElementById(\"our-h1\");", i: 1, n: "We can select the h1 by the id we gave it." },
        { c: "our_h1.style.color = \"white\";", i: 1, n: "Now let's make our h1 white instead of yellow." },
        { c: "}", i: 0, n: "We need to close the function." },
        { c: "ourFirstFN();", i: 0, n: "Now we can call the function that we just made and it will run." }
      ]
    }
  ],
  completeLesson: function() {
    console.log("Lesson Completed.");
  }
});





/*******************************
LESSON
contains many chunks
tasks are lines of a code block
*******************************/

function Lesson(params) {
  var L = {};
  
  init();
  
  function init() {
    setLesson();
    buildInstrument();
    buildChunks();
    bindKeyEvents();
    enableFirstChunk();
  }
  
  /***********
  setters
  ***********/
  
  function setLesson() {
    L.song = params.song;
    L.note = 0;
    L.container = params.container;
    L.result = params.result;
    L.chunk = 0;
    L.completeLesson = params.completeLesson;
    L.currentChunk = currentChunk;
    L.nextChunk = nextChunk;
  }
    
  /***********
  builders
  ***********/
  
  function buildChunks() {
    L.chunks = [];
    for(var c = 0; c < params.chunks.length; c++) {
      var data = params.chunks[c];
      var chunk = new Chunk({
        type: data.type,
        result: L.result,
        codeblock_pre: data.codeblock_pre,
        codeblock: data.codeblock,
        codeblock_post: data.codeblock_post,
        completeChunk: completeChunk
      });
      L.chunks.push(chunk);
      L.container.appendChild(chunk.el);
    }
  }
  
  /***********
  binders
  ***********/
  
  function bindKeyEvents() {
    document.body.addEventListener("keypress", function(e) {
      var hit = L.currentChunk().currentTask().checkHit(getChar(e.which));
      if(hit) playNote();
    });
  }
  
  function getChar(which) {
    var offset = 32;
    var key_code_map = [" ","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~"];
    return key_code_map[which - offset];
  }
  
  function buildInstrument() {
    L.instrument = new Tone.PolySynth(6, Tone.SimpleAM).toMaster();
    
    L.instrument.set({
      envelope: {
        attack:  0.5,
        release: 0.5
      }
    });
    
    
    L.instrument.set("carrier", {
      volume: 0,
      oscillator: {
        type: "triangle8",
        partials: [1, 0.2, 0.01]
      },
      envelope: {
        attack:  0.05,
        decay:   0.02,
        sustain: 0.6,
        release: 0.8
      }
    });
    L.instrument.set("modulator", {
      volume: -8,
      oscillator: {
        type: "triangle8",
        detune: 0,
        phase: 90,
        partials: [1, 0.2, 0.01]
      },
      envelope: {
        attack:  0.05,
        decay:   0.01,
        sustain: 1,
        release: 1
      }
    });
  }
  
  /***********
  private
  ***********/
  
  function enableFirstChunk() {
    L.currentChunk().enableChunk();
  }
  
  function currentChunk() {
    return L.chunks[L.chunk];
  }

  function nextChunk() {
    return (L.chunk < (L.chunks.length - 1)) ? L.chunks[L.chunk + 1] : null;
  }
  
  function completeChunk() {
    var curr = L.currentChunk();
    if((L.chunk + 1) < L.chunks.length) {
      var next = L.nextChunk();
      if(next) {
        next.el.className = next.base_class_name + " present";
        var next_task = next.currentTask();
        next_task.el.className = next_task.base_class_name + " present";
      }
      curr.el.className = curr.base_class_name + " past";
      L.chunk++;
      L.currentChunk().enableChunk();
    } else {
      L.completeLesson();
    }
  }
  
  function playNote() {
    L.instrument.triggerAttackRelease(L.song[L.note], "8n");
    L.note = ((L.note + 1) < L.song.length) ? L.note + 1 : 0;
  }
  
  
  return L;
}




/*******************************
CHUNK
contains many tasks
tasks are lines of a code block
*******************************/

function Chunk(params) {
  var C = {};
  
  init();
  
  function init() {
    setChunk();
    buildElement();    
    buildTasks();
  }
  
  /***********
  setters
  ***********/
  
  function setChunk() {
    C.result = params.result;
    C.type = params.type;
    C.task = 0;
    C.codeblock_pre = params.codeblock_pre;
    C.codeblock = params.codeblock;
    C.codeblock_post = params.codeblock_post;
    C.completeChunk = params.completeChunk;
    C.increment = increment;    
    C.currentTask = currentTask;    
    C.nextTask = nextTask;    
    C.enableChunk = enableChunk;    
  }
  
  /***********
  builders
  ***********/
  
  function buildElement() {
    C.el = document.createElement("div");
    C.base_class_name = "chunk type-" + C.type;
    C.el.className = C.base_class_name;
  }
    
  function buildTasks() {
    C.tasks = [];
    for(var c = 0; c < C.codeblock.length; c++) {
      var line = C.codeblock[c];
      var task = new Task({
        code: line.c,
        indent: line.i,
        notes_el: params.notes_el,
        notes: line.n,
        completeTask: completeTask
      });
      C.tasks.push(task);
      C.el.appendChild(task.el);
    }
  }
  
  /***********
  private
  ***********/
  
  function currentTask() {
    return C.tasks[C.task];
  }

  function nextTask() {
    return (C.task < (C.tasks.length - 1)) ? C.tasks[C.task + 1] : null;
  }
  
  function increment() {
    if(C.task < (C.tasks.length - 1)) {
      C.task++;
    } else {
      C.completeChunk();
    }
  }
  
  function completeTask() {
    var curr = C.currentTask();
    if((C.task + 1) < C.tasks.length) {
      var next = C.nextTask();
      if(next) {
        next.el.className = next.base_class_name + " present";
        next.currentNode().el.className = "node present";
      }
      curr.el.className = curr.base_class_name + " past";
      C.task++;
    } else {
      curr.el.className = curr.base_class_name + " past";
      C.completeChunk();
      runCode();
    }
  }
  
  function runCode() {
    var code = "";
    
    if(C.codeblock_pre) code += C.codeblock_pre + "\n";
    for(var i = 0; i < C.codeblock.length; i++) {
      code += C.codeblock[i].c + "\n";
    }
    if(C.codeblock_post) code += "\n" + C.codeblock_post;
    
    switch(C.type) {
      case "javascript":
      case "js":
        eval(code);
        break;
      case "html":
        C.result.innerHTML += code;
        break;
      case "css":
        injectCSS(code);
        break;
      default:
        // console.debug(code);
    }
  }
  
  function enableChunk() {
    C.tasks[0].el.className += " present";
    C.tasks[0].nodes[0].el.className += " present";
  }
  
  function injectCSS(code) {
    var el = document.createElement("style");
    el.innerHTML = code;
    document.body.appendChild(el);
  }
    
  return C;

}



/*******************************
TASK
contains many nodes
nodes are individual characters
*******************************/

function Task(params) {
  var T = {};
  
  init();
  
  function init() {
    setTask();
    buildElement();
    buildNodes();
    setEvents();
  }
  
  /***********
  setters
  ***********/
  
  function setTask() {
    T.code = params.code;
    T.indent = params.indent;
    T.notes = params.notes;
    T.base_class_name = "task indent-" + T.indent;
    T.nodes = [];
    T.node = 0;
  }
  
  function setEvents() {
    T.checkHit = checkHit;
    T.increment = increment;
    T.currentNode = currentNode;
    T.nextNode = nextNode;
    T.completeTask = params.completeTask;
  }
  
  /***********
  builders
  ***********/
  
  function buildElement() {
    var el = document.createElement("span");
    el.className = T.base_class_name;
    T.el = el;
    if(T.notes) {
      T.task_el = document.createElement("div");
      T.task_el.className = "notes";
      T.task_el.innerHTML = "<p>" + T.notes + "</p>";
      T.el.appendChild(T.task_el);
    }
  }
  
  function buildNodes() {
    var chars = T.code.split("");
    for(var c = 0; c < chars.length; c++) {
      var char = chars[c]
      var node = document.createElement("span");
      node.className = "node";
      node.innerHTML = chars[c];
      T.nodes.push({ el: node, val: chars[c] });
      T.el.appendChild(node);
    }
  }
  
  /***********
  events
  ***********/
  
  function checkHit(key) {
    var curr = T.currentNode();
    if(key === curr.val) {
      var next = T.nextNode();
      if(next) next.el.className = "node present";
      curr.el.className = "node past";
      T.increment();
      return true;
    } else {
      curr.el.className = "node present miss";
      setTimeout(function() {
        if(curr.el.className !== "node past") {
          curr.el.className = "node present";
        }
      }, 200);
      return false;
    }
  }
  
  /***********
  private
  ***********/
  
  function currentNode() {
    return T.nodes[T.node];
  }

  function nextNode() {
    return (T.node < (T.nodes.length - 1)) ? T.nodes[T.node + 1] : null;
  }
  
  function increment() {
    if(T.node < (T.nodes.length - 1)) {
      T.node++;
    } else {
      T.completeTask();
    }
  }
    
  return T;
  
}


function getSong() {
  var notes = ["C3","D3","E3","F3","G3","A3","B3","C4","B3","A3","G3","F3","E3","D3"];
  return notes;
}
              
            
!
999px

Console