Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                main.game
	.game__canvas
		#play-box.box
			.box__inner
				h1.box__headline Find the rainbow
				p.box__txt Press (click/tap) the buttons until you find it!
				button#play-btn.box__btn.p-btn(type="button") Play now
		.grid
			- for (var i = 1; i <= 25; i++)
				.grid__cell
					button.grid__btn.p-btn(type="button") 
						span.grid__btn__rainbow

              
            
!

CSS

              
                $c_btn: yellow;
$c_border: blue;
$c_bg: pink;

$size: 500px;
$grid_cell: ($size / 5);
$border: 4px;
$min_width: 20em;
$transition: all 0.05s ease-in;

$z_index: (
	rainbow: 90,
	box: 120,
);

@function z_index($name) {
  @if map-has-key($z_index, $name) {
    @return map-get($z_index, $name);  
  }
};

@mixin size($w, $h: $w) {
	width: $w;
	height: $h;
}

html,
body {
	height: 100%;
}

body {
	min-width: $min_width;
	background-color: $c_bg;
	margin: 0;
	padding: 0;
}

::selection {
	background-color: $c_bg;	
}

.p-btn {
	cursor: pointer;
	outline: none;
	-webkit-tap-highlight-color: transparent;
}

.game {
	@include size(100%);
	display: flex;
	// Fixes iOS jagged vertical scrolling
	overflow-y: scroll;
	-webkit-overflow-scrolling: touch;

	&__canvas {
		@include size(100%, auto);
		max-width: $size;
		margin: auto; // Center magic after parent's display flex
		position: relative; // To place box
	}
}

.box {
	@include size(100%);
	position: absolute;
	top: 0;
	left: 0;
	z-index: z_index(box);
	
	&__inner {
		@include size(100%, auto);
		background-color: $c_border;
		box-sizing: border-box;
		color: $c_btn;
		font-family: 'Overpass Mono', monospace;
		padding: 2.2rem 1.5rem;
		position: absolute;
		top: 22%;
		text-align: center;
	}

	&__headline {
		font-size: 2rem;
		line-height: 1;
		margin: 0;
	}
	
	&__txt {
		font-size: 0.9rem;
	}
	
	&__btn {
		background-color: $c_btn;
		color: $c_border;
		font-family: 'Overpass Mono', monospace;
		font-size: 1rem;
		font-weight: bold;
		line-height: 1;
		padding: 0.5rem 1rem;
		text-transform: lowercase;
	}
}

.grid {
	display: grid;
	place-items: center;
	justify-content: center;
	grid-template-columns: repeat(auto-fit, $grid_cell);
	
	&__cell {
		@include size($grid_cell);
	}
	
	&__btn {		
		@include size(60%);
		background-color: $c_btn;
		border: $border solid $c_border;
		line-height: 1.5;
		margin: 13% 26%;
		position: relative; // To place buttons' parts

		transition: $transition;
		&.is-pressed {
			background-color: $c_border;
			cursor: default;
			transform: translate(calc(-10% + 1px), calc(10% - 1px));
			
			&::before {
				width: 10%;
			}
			
			&::after {
				height: 10%;
			}
		}
		
		&::before,
		&::after {
			border: $border solid $c_border;
			content: '';	
			background: yellow;
			position: absolute;
			transition: $transition;
		}
		
		// Left part
		&::before {
			@include size(20%, calc(100% + #{$border / 2}));
			border-right: 0;
			border-bottom-width: ($border / 2);
			transform: skew(0deg, -45deg) translateX($border * -1);
			transform-origin: top right;
			top: ($border * -2);
			right: 99%;
		}
		
		// Bottom part
		&::after {
			@include size(calc(100% + #{round($border / 3)}), 20%);
			border-top: 0;
			border-left-width: round($border / 1.2);
			transform: skew(-45deg, 0deg) translateY($border);
			transform-origin: top right;
			top: 100%;
			right: ($border * -2);
		}

		&__rainbow {
			position: absolute;
			top: 40%;
			left: 40%;
			z-index: z_index(rainbow);
			
			font-size: 0;		
			&.has-rainbow {
				animation: rainbowFound 1s ease-in-out alternate infinite;
				font-size: round($size / 18);
			}
		}
	}
}

@keyframes rainbowFound {
	$fontsize: 6rem;
	$translateX: ($grid_cell / -2.2);
	$translateY: ($grid_cell / -1.8);
	$translate_sm: ($translateY / 5);
	$rot_right: 25deg;
	$rot_left: -15deg;
	
	0% {
		transform: translate($translate_sm, $translate_sm);
	}
	25% {
		font-size: $fontsize;
		transform: translate($translateX, $translateY) rotate($rot_right);
	}
	50% {
		font-size: $fontsize;
		transform: translate($translateX, $translateY) rotate($rot_left);
	}
	75% {
		font-size: $fontsize;
		transform: translate($translateX, $translateY) rotate($rot_right);
	}
	100% {
		font-size: $fontsize;
		transform: translate($translateX, $translateY) rotate($rot_left);
	}
}
              
            
!

JS

              
                /* 
 * FIND THE RAINBOW
 * Press (click/tap) the buttons until you find the rainbow!
 * Game is fully responsive via CSS Grid + Flexbox. 
 * 
 * You can play as many times as you'd like, because the rainbow it's always hidden in a different place.
 *
 * Feel free to improve this game, or give me feedback.
 *
 * #006 - #100DaysOfCode
 * By ilithya | 2019
 */
let GAME_OVER = false;
let rainbowBox = null;
const rainbowString = "🌈";
const btnPressClass = "is-pressed";
const rainbowClass = "has-rainbow";

const dialog = {
	hide(dialogBox) {
		dialogBox.style.display = "none";
	},
	updateContent() {
		const boxHeadline = document.querySelector("#play-box .box__headline");
		boxHeadline.textContent = "Play again?";

		const boxText = document.querySelector("#play-box .box__txt");
		boxText.textContent = "Rainbow is never in the same place.";
	},
	showPlayAgain(dialogBox) {
		dialog.updateContent();
		dialogBox.style.display = "block";
	}
};

const getRandomIntInclusive = (maxNum, not) => {
	const min = 0;
	const max = Math.floor(maxNum);
	
	const item = Math.floor(Math.random() * (max - min + 1)) + min;
	
	// Trick learned with Wes Bos 
	// Avoid getting the same random number from last time
	if (item === not) return getRandomIntInclusive(maxNum, not);

	return item;
};

const rainbow = {
	insert(el) {
		const btnCount = el.length - 1;
		const luckyBtn = getRandomIntInclusive(btnCount, rainbowBox);
		rainbowBox = luckyBtn;

		el[luckyBtn].children[0].textContent = rainbowString;
		el.forEach((item) => game.addBtnClick(item));
	},
	remove(el) {
		el.forEach((item) => {
			const itemChild = item.children[0];
			itemChild.textContent = "";
			itemChild.classList.remove(rainbowClass);
			item.classList.remove(btnPressClass);
		});
	}
};

const game = {
	btn: document.querySelectorAll(".grid__btn"),
	box: document.getElementById("play-box"),
	start() {
		rainbow.insert(game.btn);
		dialog.hide(game.box);	
	},
	run() {
		if (GAME_OVER){ 
			rainbow.remove(game.btn); 
			GAME_OVER = false;
		}
		game.start();
	},
	stop() {
		GAME_OVER = true;

		game.btn.forEach((item) => game.removeBtnClick(item));
		
		setTimeout(() => dialog.showPlayAgain(game.box), 5000);
	},
	play(e) {
		const thisItem = e.target;
		thisItem.classList.add(btnPressClass);
		game.removeBtnClick(thisItem);

		const rainbowEl = thisItem.children[0];
		if (rainbowEl.textContent === rainbowString) {
			rainbowEl.classList.add(rainbowClass);
			game.stop();
		}
	},
	addBtnClick(el) {
		el.addEventListener("click", game.play, false);
	},
	removeBtnClick(el) {
		el.removeEventListener("click", game.play, false);
	}
};

const btnPlay = document.getElementById("play-btn");
btnPlay.addEventListener("click", game.run, false);
              
            
!
999px

Console