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.

            
              <form action="">
	<p>Turn any image into emojis!</p>
	<div class="form-group">
		<div class="form-group-col">
			<label for="emoji_size">Emoji Size <span class="label-tip">(6&ndash;64px)</span></label>
			<input id="emoji_size" type="number" name="emoji_size" min="6" max="64" value="10" required>
		</div>
		<div class="form-group-col">
			<label for="img_upload">Upload Image</label>
			<input id="img_uploadq" type="file" name="img_upload" required>
		</div>
	</div>
	<button type="submit" disabled>Emojify!</button>
	<hr>
</form>
<canvas width="300" height="300" aria-label="Output"></canvas>
            
          
!
            
              *, *:before, *:after {
	border: 0;
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
:root {
	font-size: calc(16px + (24  - 16) * (100vw - 320px)/(1600 - 320));
}
body, button, input {
	color: #222;
	font: 1em "Hind", sans-serif;
	line-height: 1.5;
}
body {
	overflow-y: scroll;
}
form {
	padding: 1.5em 0.75em 0 0.75em;
	max-width: 27em;
}
canvas, input, label {
	display: block;
}
canvas, form {
	margin: auto;
}
button, input {
	border: 0;
	-webkit-appearance: none;
	width: 100%;
}
label {
	font-weight: bold;
}
button, button:disabled:hover, button:disabled:active  {
	background: #4b4;
}
button {
	border-radius: 0.375em;
	color: #fff;
	cursor: pointer;
	padding: 0.75em 1em;
	transition: background 0.1s linear;
}
button:disabled {
	cursor: not-allowed;
	opacity: 0.5;
}
button:hover {
	background: #5c5;
}
button:active {
	background: #3a3;
}
input {
	margin-bottom: 0.75em;
	padding: 0.375em;
}
input[type=number] {
	box-shadow: 0 0 0 1px #aaa inset;
	width: 4.5em;
}
button, hr, p {
	margin-bottom: 1.5em;
}
hr {
	border-top: 1px solid #aaa;
	margin-top: -1px;
}
.form-group {
	display: flex;
	flex-wrap: wrap;
}
.form-group-col {
	flex-basis: 100%;
}
.label-tip {
	color: #555;
	font-weight: normal;
}
@media screen and (min-width: 481px) {
	button {
		width: auto;
	}
	.form-group-col {
		flex-basis: 50%;
	}
}
            
          
!
            
              window.addEventListener("load", app);
function app() {
	let canvas = document.getElementsByTagName("canvas")[0],
		form = document.getElementsByTagName("form")[0],
		emojiSize = document.getElementsByName("emoji_size")[0],
		imgUpload = document.getElementsByName("img_upload")[0],
		btn = document.getElementsByTagName("button")[0],
		c = canvas.getContext('2d'),
		W = 300,
		H = 300,
		S = 2,
		uploadFilename = "",
		img = null;

	c.fillStyle = "#e0e0e0";
	c.fillRect(0,0,W,H);
	c.fillStyle = "#888";
	c.font = "28px Hind";
	c.textAlign = "center";
	c.textBaseline = "middle";
	c.fillText("Output",W / 2,H / 2);

	var emojiSet = function(hue,emojiLevelArrays){
			this.hue = hue;
			this.emojiLevelArrays = emojiLevelArrays;
		},
		emojiSets = [
			// lightness left-to-right, saturation top-to-bottom
			new emojiSet(0,[
				["๐Ÿ›ข","๐ŸฅŠ","๐ŸŒบ","๐Ÿง "],
				["๐Ÿฅ€","๐Ÿ’‹","โค๏ธ","๐Ÿ‘…"]
			]),
			new emojiSet(30,[
				["๐Ÿฆƒ","๐Ÿฟ","๐Ÿ•Œ","๐Ÿ‘"],
				["๐Ÿ’ฉ","๐Ÿฅจ","๐Ÿ˜ก","๐Ÿก"]
			]),
			new emojiSet(60,[
				["๐Ÿ“ป","๐Ÿ’ฐ","โšฑ๏ธ","๐Ÿ“œ"],
				["๐Ÿฅƒ","โ˜€๏ธ","๐Ÿ˜€","๐Ÿ "]
			]),
			new emojiSet(90,[
				["๐ŸŠ","๐Ÿข","๐Ÿ","๐Ÿ‘’"],
				["๐ŸŒฟ","๐ŸŒณ","๐Ÿคข","๐Ÿ"]
			]),
			new emojiSet(120,[
				["๐ŸŒฒ","๐ŸŠ","๐Ÿ”‹","๐Ÿ"],
				["๐ŸŒฒ","๐Ÿš›","๐Ÿธ","๐Ÿต"]
			]),
			new emojiSet(150,[
				["๐ŸงŸโ€โ™€๏ธ","๐ŸงŸโ€โ™€๏ธ","๐Ÿงค","๐Ÿ‘—"],
				["๐ŸŠ","๐Ÿ”‹","๐Ÿ“Ÿ","๐Ÿฅ’"]
			]),
			new emojiSet(180,[
				["๐Ÿ—ฃ","๐Ÿ‘ค","๐Ÿ‘•","๐Ÿค–"],
				["๐Ÿฆ•","๐Ÿณ","๐Ÿฆ‹","๐Ÿ’ง"]
			]),
			new emojiSet(210,[
				["๐ŸŒš","๐Ÿฆ•","๐ŸŒŽ","๐ŸŸ"],
				["๐Ÿ›ฐ","๐ŸŒ€","๐Ÿ”ต","๐ŸŒŽ"]
			]),
			new emojiSet(240,[
				["๐ŸŒ‘","๐Ÿ—ฃ","๐Ÿ‘ค","๐Ÿฆ"],
				["๐ŸŒ‘","๐ŸŒš","๐Ÿ‘–","๐ŸŒŒ"]
			]),
			new emojiSet(270,[
				["๐ŸŒ‚","๐Ÿง•","๐Ÿ‘พ","๐Ÿ”ฎ"],
				["๐Ÿ•บ","๐Ÿคทโ€โ™€๏ธ","๐Ÿ˜ˆ","๐Ÿฆ„"]
			]),
			new emojiSet(300,[
				["๐Ÿ†","๐Ÿ ","๐Ÿ‘š","๐Ÿ–"],
				["๐Ÿ“ฟ","๐Ÿ•บ","๐Ÿคฐ๐Ÿผ","๐Ÿฆ„"]
			]),
			new emojiSet(330,[
				["๐Ÿงžโ€โ™€๏ธ","๐Ÿ’—","๐ŸŽ€","๐Ÿ‘ฉโ€๐ŸŽค"],
				["๐Ÿ‡","๐Ÿงžโ€โ™€๏ธ","๐Ÿ’–","๐Ÿฉ"]
			])
		];

	var adjustCanvasSize = function() {
			// to prevent emojis at right and bottom edges from being cut off
			let imgW = img.width,
				imgH = img.height,

				_imgW = imgW % emojiSize.value,
				_imgH = imgH % emojiSize.value;

			imgW -= _imgW;
			imgH -= _imgH;

			canvas.width = imgW;
			canvas.height = imgH;

			c.drawImage(img,0,0,canvas.width,canvas.height);

			W = canvas.width;
			H = canvas.height;
		},
		handleImgUpload = function(e) {
			var reader = new FileReader();

			reader.onload = function(ev){
				img = new Image();
				img.onload = function(){
					adjustCanvasSize();
				}
				img.src = ev.target.result;
			}
			reader.readAsDataURL(e.target.files[0]);
		},
		emojifyImage = function(e) {
			// force invalid input to default for emoji size
			if (emojiSize.value < +emojiSize.min || isNaN(emojiSize.value)) {
				emojiSize.value = +emojiSize.min;

			} else if (emojiSize.value > +emojiSize.max) {
				emojiSize.value = +emojiSize.max;
			}

			btn.disabled = true;
			btn.innerHTML = "Emojifyingโ€ฆ";

			// prevent the emojified output from being emojified again
			if (uploadFilename == imgUpload.files[0].name) {
				c.clearRect(0,0,W,H);
				adjustCanvasSize();

			} else {
				uploadFilename = imgUpload.files[0].name;
			}
			
			setTimeout(function(){
				let imgData = c.getImageData(0,0,W,H),
					data = imgData.data,
					eSize = emojiSize.value;
				
				c.clearRect(0,0,W,H);

				c.font = eSize + "px serif";
				c.textAlign = "left";
				c.textBaseline = "top";
				
				for (var Yp = 0; Yp < H / eSize; ++Yp) {
					for (var Xp = 0; Xp < W / eSize; ++Xp) {
						// get average color of grouped pixels
						var rs = 0,
							gs = 0,
							bs = 0,
							as = 0;
						for (var yp = 0; yp < eSize; ++yp) {
							for (var xp = 0; xp < eSize * 4; xp += 4) {
								let moveYInPixel = yp * W * 4,
								    moveX = Xp * eSize * 4,
									moveY = Yp * W * eSize * 4,
									moveTo = moveYInPixel + moveX + moveY;
								
								rs += data[xp + moveTo]**2;
								gs += data[(xp + 1) + moveTo]**2;
								bs += data[(xp + 2) + moveTo]**2;
								as += data[(xp + 3) + moveTo]**2;
							}
						}
						let ar = Math.round( Math.sqrt(rs / (eSize**2)) ),
							ag = Math.round( Math.sqrt(gs / (eSize**2)) ),
							ab = Math.round( Math.sqrt(bs / (eSize**2)) ),
							aa = Math.round( Math.sqrt(as / (eSize**2)) );

						// for better matchmaking, convert RGB to HSL
						let _r = ar / 255,
							_g = ag / 255,
							_b = ab / 255,

							cmax = Math.max(_r,_g,_b),
							cmin = Math.min(_r,_g,_b),
							delta = cmax - cmin;

						var hue = 0,
							sat = 0,
							light = 0;

						// get hue
						if (delta == 0) {
							hue = 0;

						} else if (cmax == _r) {
							hue = ((_g - _b) / delta) % 6;

						} else if (cmax == _g) {
							hue = (_b - _r) / delta + 2;

						} else {
							hue = (_r - _g) / delta + 4;
						}
						hue = Math.round(hue * 60);

						if (hue < 0) {
							hue += 360;
						}

						// get lightness and saturation
						light = (cmax + cmin) / 2,
						sat = delta == 0 ? 0 : delta / (1 - Math.abs(2 * light - 1));

						sat = Math.round(sat * 100);
						light = Math.round(light * 100);

						// determine emoji closest to average color
						let filteredEmojis = emojiSets.filter(em => 
								Math.ceil(hue / 30) * 30 == em.hue
							),
							fltrResult = filteredEmojis.length ? filteredEmojis[0] : emojiSets[0],
							chosenEmoji = "";
						
						if (aa > 0) {
							if (light <= 0) {
								chosenEmoji = "๐Ÿ’ฃ";

							} else if (light >= 100) {
								chosenEmoji = "๐Ÿ‘ป";
								
							} else if (sat <= 0) {
								chosenEmoji = "๐Ÿ‘ฝ";

							} else {
								if (sat > 100) {
									sat = 100;
								}
								chosenEmoji = fltrResult.emojiLevelArrays[Math.ceil(sat / 50) - 1][Math.ceil(light / 25) - 1];
							}
						}

						c.fillText(
							chosenEmoji,
							eSize * (Xp % (W/eSize)),
							eSize * (Yp % (H/eSize)),
						);
					}
				}

				btn.disabled = false;
				btn.innerHTML = "Emojify!";

			}, 500);
			e.preventDefault();
		},
		validateForm = function(e) {
			if (imgUpload.files.length && 
				imgUpload.files[0].type.match("image.*") && 
				emojiSize.value >= +emojiSize.min && 
				!isNaN(emojiSize.value) && 
				emojiSize.value <= +emojiSize.max) {
				btn.disabled = false;

			} else {
				btn.disabled = true;
			}
		};
	form.addEventListener("submit",emojifyImage);
	emojiSize.addEventListener("change",validateForm);
	imgUpload.addEventListener("change",function(e){
		handleImgUpload(e);
		validateForm(e);
	});
}
            
          
!
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