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.

            
              main.game
	.game__canvas
		.box#play-box
			.box__inner
				h1.box__headline Find the rainbow
				p.box__txt Press (click/tap) the buttons until you find it!
				button(type='button' id='play-btn').box__btn.p-btn Play now
		.grid
			- for (var i = 1; i <= 25; i++)
				.grid__cell
					button(type='button').grid__btn.p-btn 
						span.grid__btn__rainbow 
            
          
!
            
              $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);
	}
}
            
          
!
            
              /* 
 * 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 gameOver = false;
const rainbowString = "🌈";
const btnPressClass = "is-pressed";
const rainbowClass = "has-rainbow";

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

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

const getRandomIntInclusive = maxNum => {
	const min = 0;
	const max = Math.floor(maxNum);

	return Math.floor(Math.random() * (max - min + 1)) + min;
};

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

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

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

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

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

const btnPlay = document.getElementById("play-btn");
btnPlay.addEventListener("click", game.run, false);
            
          
!
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.

Console