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

              
                <!-- Programmer's Day 2020 challenge - Type Writer Machine with JS and some fun audio features  -->
<!-- Desktop recommendation for better experience  -->
<!-- Feel free to make some text (JS line 10) and rain (JS line 16) changes! have fun!  -->

<!--  single stroke -->
<audio id="keystroke-sound-1" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_single_key_press_medium_typebar_rear_close_vintage_1960_lemair_helvetia_011_41027.mp3?_=1"></audio>
<audio id="keystroke-sound-2"src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_single_key_press_medium_typebar_rear_close_vintage_1960_lemair_helvetia_012_41028.mp3?_=1"></audio>
<audio id="keystroke-sound-3"src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_single_key_press_medium_typebar_rear_close_vintage_1960_lemair_helvetia_018_41034.mp3?_=1"></audio>
<audio id="keystroke-sound-4" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_single_key_press_medium_typebar_rear_close_vintage_1960_lemair_helvetia_019_41035.mp3?_=1"></audio>
<audio id="keystroke-sound-5" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_single_key_press_medium_typebar_rear_close_vintage_1960_lemair_helvetia_020_41036.mp3?_=1"></audio>
<!-- spacebar sound -->
<audio id="spacebar-sound-1" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_spacebar_key_press_typebar_rfront_close_vintage_1960_lemair_helvetia_003_41039.mp3?_=1"></audio>
<audio id="spacebar-sound-2" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_spacebar_single_key_press_rear_close_vintage_1960_lemair_helvetia_001_41040.mp3?_=1"></audio>
<link href="https://fonts.googleapis.com/css2?family=Special+Elite&display=swap" rel="stylesheet">
<!--  carriage return -->
<audio id="carriage-sound-1" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_carriage_return_margin_bell_vintage_1960_lemair_helvetia_001_40992.mp3?_=1"></audio>
<audio id="carriage-sound-2" src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-35448/zapsplat_office_typewriter_carriage_return_margin_bell_vintage_1960_lemair_helvetia_002_40993.mp3?_=1"></audio>

<div class="main">
	<div class="bar">
		<button id="replay">
			<i class="fas fa-redo"></i>	
		</button>
		<div class="action action-rain">
			<input type="checkbox" id="rainMaker">
			<label for="rainMaker">
				<i class="fas fa-tint-slash"></i>
				<i class="fas fa-tint"></i>
			</label>
			
		</div>
		<div class="action action-sound">
			<input type="checkbox" id="soundOn">
			<label for="soundOn">
				<i class="fas fa-volume-mute"></i>
				<i class="fas fa-volume-up"></i>
			</label>
		</div>
	</div>
	<div class="container" id="typingArea"></div>
</div>

              
            
!

CSS

              
                body {
	font-family: 'Special Elite', cursive;
	background: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	margin: 3rem;
}

button {
	background: #fff;
	border: none;
	cursor: pointer;
	font-size: 22px;
	
	&:hover {
		transition: transform .2s ease-in-out;
		transform: scale(1.1);
	}
	
	&:focus {
		outline: none;
	}
}

.main {
	position: relative;
	width: 500px;
	min-height: 30vh;
	padding: 65px;
	background-color: #fff;
	box-shadow: 0 0 5px rgba(0, 0, 0, 0.2), inset 0 0 50px rgba(0, 0, 0, 0.1);
}

.main:before, 
.main:after
{
	position: absolute;
	width: 40%;
	height: 10px;
	content: '';
	left: 12px;
	bottom: 12px;
	background: transparent;
	transform: skew(-5deg) rotate(-5deg);
	box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
	z-index: -1;
} 

.main:after
{
	left: auto;
	right: 10px;
	transform: skew(5deg) rotate(5deg);
}

.bar {
	display: grid;
	grid-template-columns: 1fr 1fr 1fr;
	margin-bottom: 80px;
}

.container {
	font-size: 25px;
	text-align: center;
}

.action {
	display: grid;
	justify-content: center;
	align-items: center;
	
	input {
		display: none;
	}
	
	label {
		cursor: pointer;
		font-size: 22px;
		
		i:hover {
			transition: transform .2s ease-in-out;
			transform: scale(1.1);
		}
	}
	
	&.action-rain {
		input:checked {
			+ label {
				.fa-tint-slash {
					display: none;
				}
			}
		}
		
		input:not(:checked) {
			+ label {
				.fa-tint {
					display: none;
				}
			}
		}
	}
	
	&.action-sound {
		input:checked {
			+ label {
				.fa-volume-mute {
					display: none;
				}
			}
		}
		
		input:not(:checked) {
			+ label {
				.fa-volume-up {
					display: none;
				}
			}
		}
	}
}

.rain-drop {
    position: fixed;
    top: -1vh;
    font-size: 2rem;
    transform: translateY(0);
    animation: raining 7s linear forwards;
}

@keyframes raining {
    to {
        transform: translateY(105vh) rotate(30deg);
    }
}
              
            
!

JS

              
                const [
	textToType,
	typingArea,
	replayBtn,
	rainMakerBtn,
	checkbox,
	typingTempo,
	rainDropType 
] = [
	"Happy Programmer's Day!",
	document.getElementById("typingArea"),
	document.getElementById('replay'),
	document.getElementById('rainMaker'),
	document.getElementById('soundOn'),
	400,
	'💰'
]

let [
	currentInterval,
	prevIteration,
	iteration,
	print,
	raining
] = [
	'',
	0,
	0,
	[],
	''
]

replayBtn.addEventListener('click', () => {
	clearInterval(currentInterval);
	iteration = 0;
	prevIteration = 0;
	print = [];
	const replay = setInterval(() =>  addLetter(typingArea, textToType) , typingTempo);
	currentInterval = replay;
});

rainMakerBtn.addEventListener('change', () => {
	if (rainMakerBtn.checked) {
		raining = setInterval(() => this.rain(rainDropType), 500);
	} else {
		clearInterval(raining);
	}
});

function makeSound(sound) {
	checkbox.checked ? sound.muted = false : sound.muted = true;
	sound.pause();
	sound.currentTime = 0;
	sound.play()
}

function addLetter(box, message) {
	iteration++;
	let char = message.slice(prevIteration, iteration);
	if (char === ' ') {
		makeSound(document.getElementById(`spacebar-sound-${this.getRandomInt(1,2)}`));
	} else {
		makeSound(document.getElementById(`keystroke-sound-${this.getRandomInt(1,5)}`));
	}
	print.push(message.slice(prevIteration, iteration));
	box.innerText = [...print].join('');
	prevIteration++;

	if (iteration >= message.length) {
		iteration = 0;
		clearInterval(currentInterval);
		makeSound(document.getElementById(`carriage-sound-${this.getRandomInt(1,2)}`));
	}
}

function rain(icon) {
	const rainDrop = document.createElement("div");
	rainDrop.classList.add("rain-drop");
	rainDrop.style.left = Math.random() * 100 + "vw";
	rainDrop.style.animationDuration = Math.random() * 2 + 4 + "s";
	rainDrop.style.transform = `rotate(${Math.random() * 180}deg)`
	rainDrop.innerText = icon;
	document.body.appendChild(rainDrop);
	setTimeout(() => {
		rainDrop.remove();
	}, 7000);
}


function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

const mainInfoWrite = setInterval(() =>  addLetter(typingArea, textToType) , typingTempo);
currentInterval = mainInfoWrite;
              
            
!
999px

Console