cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <body data-step="0">

<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
	<defs>
		<symbol id="note" viewBox="0 0 121 174">
			<path d="M72.517 141.958c.741 13.022-12.003 26.182-30.168 30.376-19.642 4.534-38.183-3.132-41.414-17.123-3.23-13.992 10.075-29.01 29.716-33.545 7.576-1.749 14.988-1.683 21.42-.139L52 115.424V0c11.37 11.855 20.084 19.909 26.142 24.16 9.087 6.377 21.997 14.68 30.63 25.999 8.633 11.318 12.207 22.805 11.035 33.937-1.172 11.132-3.8 19.104-8.8 26.987-3.333 5.256-5.444 8.71-6.332 10.36 2.344-11.277 3.06-20.35 2.15-27.22-1.364-10.304-3.914-15.369-12.356-21.72-5.628-4.234-12.885-7.564-21.77-9.991v79.493l-.182-.047z" fill-rule="evenodd"/>
		</symbol>
	</defs>
</svg>

<div id="countdown">01:00</div>

<div id="screen-wrapper">	
	<div id="screens">

		<div class="screen step0"></div>

		<div class="screen step1">
			<div class="lock-input">
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
			</div>
			<p class="hint">Hint: "Amazing Grace" in F</p>
		</div>

		<div class="screen step2">
			<div class="lock-input">
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
				<div class="note"><svg><use xlink:href="#note"></use></svg></div>
			</div>
			<p class="hint">Hint: C7 in root position</p>
		</div>

		<div class="screen step3">
			<p>You win!!</p>
		</div>
	</div>
</div>
</body>
            
          
!
            
              * {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}
html, body {
    height: 100%;
}
body {
    background: radial-gradient(#111628, #0b0f1d);
    font-family: 'Roboto Mono', Arial, sans-serif;
}
body::before {
    content: "";
    position: fixed;
    top: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(rgba(24, 56, 24, 0.57), rgba(40, 109, 40, 0.68));
    opacity: 0;
    transition: 150ms ease;
    z-index: -1;
    will-change: opacity;
}
body.success::before {
    animation: success 2.5s;
}
body::after {
    content: "";
    position: fixed;
    top: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(rgba(244, 67, 54, 0.8), rgba(206, 55, 44, 0.48));
    opacity: 0;
    transition: 500ms ease;
    z-index: -1;
    will-change: opacity;
}
body.gameover::after {
    opacity: 1;
}

#countdown {
    height: 20vh;
    position: absolute;
    top: 0;
    left: 0;
    color: #fff;
    text-align: center;
    font-size: 15vh;
    line-height: 20vh;
    width: 100%;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}
#screen-wrapper {
    overflow: hidden;
    height: 100vh;
}

#screens {
    width: 400%;
    height: 100%;
    color: #ffffff;
    display: flex;
    align-items: center;
    transition: 500ms ease;
    will-change: transform;
}

[data-step="1"] #screens {
    transform: translateX(-100vw);
}
[data-step="2"] #screens {
    transform: translateX(-200vw);
}
[data-step="3"] #screens {
    transform: translateX(-300vw);
}

.screen {
    flex: 1 0 25%;
    text-align: center;
    padding: 2em;
}

.lock-input {
    background: #fff1dc;
    padding: 50px 75px;
    border-radius: 15px;
    box-shadow: 0px 2px 5px #000;
    margin: 20px 0;
    text-align: center;
    overflow: hidden;
    transition: 150ms ease;
    display: inline-block;
}

.lock-input.error {
    background: #ffacba;
    animation: shake 300ms;
}

.note {
    display: inline-block;
}

.note svg {
    width: 55px;
    height: 75px;
    transform: translateY(150px);
    transition: 150ms cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

.note.on svg {
    transform: translateY(0px);
}

.step0, .step3 {
    font-size: 3vw;
}

.hint {
    font-weight: 100;
    font-style: italic;
    font-size: 0.8em;
}


@keyframes success {
    0% {
        opacity: 0;
    }
    10% {
        opacity: 1;
    }
    30% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}

@keyframes shake {
    0% {
        transform: translateX(0px);
    }
    25% {
        transform: translateX(-5px);
    }
    75% {
        transform: translateX(5px);
    }
    100% {
        transform: translateX(0px);
    }
}
            
          
!
            
              // Variable which tell us what step of the game we're on. 
// We'll use this later when we parse noteOn/Off messages
var currentStep = 0;

// Timer length
var timerLength = 1; // in minutes

// Lock 1 variables
var correctNoteSequence = [60, 65, 69, 65, 69, 67, 65, 62, 60]; // Amazing Grace in F
var activeNoteSequence = [];

// Lock 2 variables
var correctChord = [60, 64, 67, 70]; // C7 chord starting on middle C
var activeChord = [];

if (navigator.requestMIDIAccess) {
	console.log('This browser supports WebMIDI!');

	navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);

} else {
	console.log('WebMIDI is not supported in this browser.');
	document.querySelector('.step0').innerHTML = 'Error: This browser does not support WebMIDI.';
}

function onMIDISuccess(midiAccess) {
	document.querySelector('.step0').innerHTML = 'Press any note to begin...';
	var inputs = midiAccess.inputs;
	var outputs = midiAccess.outputs;

	for (var input of midiAccess.inputs.values()) {
		input.onmidimessage = getMIDIMessage;
	}
}

function onMIDIFailure() {
	document.querySelector('.step0').innerHTML = 'Error: Could not access MIDI devices. Connect a device and refresh to try again.';
}

function getMIDIMessage(message) {
	var command = message.data[0];
	var note = message.data[1];
	var velocity = (message.data.length > 2) ? message.data[2] : 0; // a velocity value might not be included with a noteOff command

	switch (command) {
		case 144: // noteOn
			if (velocity > 0) {
				noteOnListener(note, velocity);
			} else {
				noteOffListener(note);
			}
			break;
		case 128: // noteOff
			noteOffCallback(note);
			break;
		// we could easily expand this switch statement to cover other types of commands such as controllers or sysex
	}
}


function noteOnListener(note, velocity) {

	switch(currentStep) {
		// If the game hasn't started yet.
		// The first noteOn message we get will run the first sequence
		case 0: 
			// Run our start up sequence
			runSequence('gamestart');

			// Increment the currentStep so this is only triggered once
			currentStep++;
			
			break;

		// The first lock - playing a correct sequence
		case 1:
			// add the note to the array
			activeNoteSequence.push(note);

			// show the requisite number of note placeholders
			for (var i = 0; i < activeNoteSequence.length; i++) {
				document.querySelector('.step1 .note:nth-child(' + (i + 1) + ')').classList.add('on');
			}

			// when the array is the same length as the correct sequence, compare the two
			if (activeNoteSequence.length == correctNoteSequence.length) {
				var match = true;
				for (var index = 0; index < activeNoteSequence.length; index++) {
					if (activeNoteSequence[index] != correctNoteSequence[index]) {
						match = false;
						break;
					}
				}

				if (match) {
					// Run the next sequence and increment the current step
					runSequence('lock1');
					currentStep++;
				} else {
					// Clear the array and start over
					activeNoteSequence = [];
					
					var lockInput = document.querySelector('.step1 .lock-input');
					
					lockInput.classList.add('error');
					window.setTimeout(function(){
						lockInput.classList.remove('error');
						for (var note of lockInput.querySelectorAll('.note')) {
							note.classList.remove('on');
						}
					}, 500);
				
				}
			}
			break;

		case 2:
			// add the note to the active chord array
			activeChord.push(note);

			// show the number of active notes
			for (var i = 0; i < activeChord.length; i++) {
				document.querySelector('.step2 .note:nth-child(' + (i + 1) + ')').classList.add('on');
			}

			// If the array is the same length as the correct chord, compare
			if (activeChord.length == correctChord.length) {
				var match = true;
				for (var index = 0; index < activeChord.length; index++) {
					if (correctChord.indexOf(activeChord[index]) < 0) {
						match = false;
						break;
					}
				}

				if (match) {
					runSequence('lock2');
					currentStep++;
				} else {
					var lockInput = document.querySelector('.step2 .lock-input');
					
					lockInput.classList.add('error');
					window.setTimeout(function(){
						lockInput.classList.remove('error');
					}, 500);
				}
			}
			break;
	}
}

function noteOffListener(note) {

	switch(currentStep) {
		case 2:
			// Remove the note value from the active chord array
			activeChord.splice(activeChord.indexOf(note), 1);

			// Hide the last note shown
			document.querySelector('.step2 .note:nth-child(' + (activeChord.length + 1) + ')').classList.remove('on');
			break;
	}
}

function runSequence(sequence) {
	switch(sequence) {
		case 'gamestart':			
			// Now we'll start a countdown timer...
			startTimer();
			
			// code to trigger animations, give a clue for the first lock
			advanceScreen();
			successFlicker();
			break;
		
		case 'lock1':
			// code to trigger animations and give clue for the next lock
			advanceScreen();
			successFlicker();
			break;
		
		case 'lock2':
			// code to trigger animations, stop clock, end game
			advanceScreen();
			successFlicker();
			break;

		case 'gameover':
			currentStep = 3;
			document.querySelector('.step3 p').innerHTML = "You lose...";
			document.querySelector('body').dataset.step = "3";
			document.querySelector('body').classList.add('gameover');
			break;
	}
}

function advanceScreen() {
	document.querySelector('body').dataset.step++;
}
function successFlicker() {
	var b = document.querySelector('body')
	b.classList.add('success');
	window.setTimeout(function(){
		b.classList.remove('success');
	}, 2500);
}

function startTimer(){
  // set timer for 60 minutes from start
  var now = new Date();
  timeEnd = new Date(now.getTime() + (timerLength*60*1000) - 1);

  updateTimer();
}
/**
 * Function to update the time remaining every second
 */
function updateTimer() {
	var now = new Date();
	var distance = timeEnd.getTime() - now.getTime();
	var minutes = Math.floor(distance / (1000 * 60));
	var seconds = Math.floor((distance % (1000 * 60)) / 1000);

	if (minutes < 10) minutes = "0" + minutes;
	if (seconds < 10) seconds = "0" + seconds;

	if (currentStep < 3) {
		document.querySelector('#countdown').innerText = minutes + ":" + seconds;
		
		if (minutes > 0 || seconds > 0) {
			window.setTimeout(function() {
				updateTimer();
			}, 1000);
		} else if (minutes == 0 && seconds == 0) {
			runSequence('gameover');
  		}
	}  
}

var lockInput = document.querySelector('.step2 .lock-input');
lockInput.addEventListener('click', function(){
	lockInput.classList.add('error');
	window.setTimeout(function(){
		lockInput.classList.remove('error');
		for (var note of lockInput.querySelectorAll('.note')) {
			note.classList.remove('on');
		}
	}, 500);
});

var notes = lockInput.querySelectorAll('.note');
for (var note of notes) {
	note.addEventListener('mouseover', function(){
		this.classList.add('on');
	});
}
            
          
!
999px
Loading ..................

Console