-
Notifications
You must be signed in to change notification settings - Fork 133
/
index.html
207 lines (189 loc) · 6.25 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="emojiColors.js"></script>
<script src="poisson-disc-sampler.js"></script>
<h1>Emoji Mosaic</h1>
<p>Upload an image to turn into a mosaic. Be patient, processing may take a moment.</p>
<input type="file">
<BR>
<script>
emojiMap = loadEmojis();
emojiInts = emojiPoints();
function loadEmojis(){
var colorMap = {};
for (var i=0; i<emojiColors.length; i++){
if ( emojiColors[i] == 'false' ) {
continue;
}
fileNumber = pad(i+1 , 4);
img = 'resized-emoji-images/' + fileNumber + '.png';
colorMap[emojiColors[i]] = img
}
return colorMap;
}
function pad(num, size){ return ('000' + num).substr(-size); }
/**
* For a given RGB color, find the closest match Emoji.
*
* @param {array} RGB color value.
* @return {string} Emoji filepath.
*/
var closestEmoji = function(color) {
var distance = 99999999;
var c, d, p;
for (var i=0; i<emojiInts.length; i++){
p = emojiInts[i];
d = Math.sqrt( Math.pow((color[0]-p[0]), 2) + Math.pow((color[1]-p[1]), 2) + Math.pow((color[2]-p[2]), 2) );
if (d < distance){
distance = d;
c = p[3];
}
}
return emojiMap[c];
}
function emojiPoints(){
var pointArray = [];
var c, r, g, b;
for (var i=0; i<emojiColors.length; i++){
c = emojiColors[i];
r = parseInt("0x"+c.substr(1,2));
g = parseInt("0x"+c.substr(3,2));
b = parseInt("0x"+c.substr(5,2));
pointArray.push([r, g, b, c]);
}
return pointArray;
}
/**
* Create a new Emoji Mosaic.
*
* @param {string} dataURI
*/
function EmojiMosaic(dataURI) {
var img = this.img = new Image;
this.dataURI = dataURI;
img.addEventListener( 'load', this.imageLoaded.bind( this ) );
img.src = this.dataURI;
}
/**
* After the image is loaded, create the layout.
*/
EmojiMosaic.prototype.imageLoaded = function() {
var margin,
width,
height,
createSample,
samples,
ratio;
margin = 100;
// If the image is too big, make the dimensions smaller.
ratio = ( window.innerWidth ) / ( this.img.width + margin * 2 );
this.img.height *= ratio;
this.img.width *= ratio;
// A canvas element for extracting image pixel color data.
this.extractionCanvas = document.createElement('canvas');
this.extractionCanvas.width = this.img.width;
this.extractionCanvas.height = this.img.height;
this.extractionCanvasContext = this.extractionCanvas.getContext('2d');
this.extractionCanvasContext.drawImage(this.img, 0, 0, this.extractionCanvas.width, this.extractionCanvas.height );
var saveButton = document.createElement( 'a' );
saveButton.innerHTML = 'Save as image';
document.body.appendChild( saveButton );
var self = this;
// A canvas element that will be drawn to with Emoji.
this.targetCanvas = document.createElement('canvas');
this.targetCanvas.width = this.extractionCanvas.width + margin * 2;
this.targetCanvas.height = this.extractionCanvas.height + margin * 2;
this.targetCanvasContext = this.targetCanvas.getContext('2d');
// Fill a background.
this.targetCanvasContext.fillStyle = '#ffffff';
this.targetCanvasContext.fillRect( 0, 0, this.targetCanvas.width, this.targetCanvas.height );
document.body.appendChild( this.targetCanvas );
createSample = poissonDiscSampler(this.targetCanvas.width, this.targetCanvas.height, 8);
samples = [];
// Create an array of samples (x,y coordinates) to place Emoji in.
while (true) {
var s = createSample();
// If no new sample was can be made, all samples have been created.
if (!s) break;
// Bail if the sample is out of bounds.
if ( s[0] < margin || s[1] < margin ) {
continue;
}
if ( s[0] > this.targetCanvas.width - margin || s[1] > this.targetCanvas.height - margin ) {
continue;
}
samples.push(s);
}
var emojisRendered = 0;
// At each sample, place an Emoji according to the image data at that point.
samples.forEach(function(sample){
var imageData, sums, i, red, green, blue, closest;
// Get image pixel data.
imageData = this.extractionCanvasContext.getImageData(Math.round( sample[0] - margin ), Math.round( sample[1] - margin ), 1, 1);
// Get average color data for an area.
sums = { red: 0, green: 0, blue: 0 };
for ( i = 0; i < imageData.data.length; i++ ) {
switch ( i % 4 ) {
case 0:
sums.red += imageData.data[i];
break;
case 1:
sums.green += imageData.data[i];
break;
case 2:
sums.blue += imageData.data[i];
break;
}
}
red = sums.red / ( imageData.data.length / 4 );
green = sums.green / ( imageData.data.length / 4 );
blue = sums.blue / ( imageData.data.length / 4 );
closest = closestEmoji( [ red, green, blue ] );
emojiImage = new Image();
emojiImage.src = closest;
emojiImage.setAttribute('data-x', (sample[0] - 10 ) );
emojiImage.setAttribute('data-y', (sample[1] - 10 ) );
// After the image is loaded, draw it onto the canvas.
emojiImage.onload = (function(event){
var image = event.target;
this.targetCanvasContext.save();
this.targetCanvasContext.translate( image.getAttribute('data-x'), image.getAttribute('data-y') );
this.targetCanvasContext.rotate( Math.ceil( Math.random() * 360 ) * Math.PI / 180 );
this.targetCanvasContext.drawImage( image, -15, -15, 30, 30 );
this.targetCanvasContext.restore();
emojisRendered++;
if ( emojisRendered === samples.length ) {
var canvasDataURI = self.targetCanvas.toDataURL('image/jpeg'),
blob = dataURItoBlob(canvasDataURI),
url = window.URL.createObjectURL(blob),
a = document.querySelector('a');
a.href = url;
a.download = 'emoji-mosaic.jpg';
}
}).bind(this);
}, this );
}
function dataURItoBlob(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
}
document.addEventListener('DOMContentLoaded', function(event) {
var inputEl = document.querySelector('input');
document.querySelector('input').addEventListener('change', function() {
// When a file is uploaded, initialize
var file = inputEl.files[0];
var reader = new FileReader();
reader.onloadend = function () {
new EmojiMosaic(reader.result);
}
reader.readAsDataURL(file);
});
});
</script>