-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
475 lines (399 loc) · 12.3 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>#SaveASEANSeas</title>
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="manifest.json">
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#068dba">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="bower_components/spectrum/spectrum.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="header col-md-12">
<div class="title">
#SaveASEANSeas
</div>
<div class="tagline">
Support us to #SaveASEANSeas
</div>
</div><!-- .header -->
<div class="app col-md-12">
<div class="row">
<div class="col-md-6">
<div class="preview-box widget">
<div class="board">
<div id="preview">
<div class="name gochi-hand"></div>
<div class="image"></div>
</div><!-- #preview -->
</div>
</div><!-- .widget -->
<div class="canvas-preview-box widget">
<div class="board">
<canvas id="canvas-preview300" class="canvas-preview"></canvas>
</div>
</div><!-- .widget -->
<div class="canvas-preview-box widget">
<div class="board">
<canvas id="canvas-preview960" class="canvas-preview"></canvas>
</div>
</div><!-- .widget -->
<div class="recommended-colors clearfix widget">
<div class="title">
Recommended colors
</div>
<div class="colors board">
</div>
</div><!-- .recommended-colors -->
</div><!-- .col-md-6 -->
<div class="col-md-6">
<div class="inputs clearfix widget">
<div class="board">
<div class="input-row">
<span class="input-label">Image: </span>
<div class="upload-btn input-field">
<span>
Choose image
</span>
<input type="file" id="input-upload" value="Choose image" accept="image/*" />
</div>
</div>
<div class="input-row">
<span class="input-label">Name: </span>
<input type="text" id="input-name" name="name" class="input-field" placeholder="Name">
</div>
<div class="input-row">
<span class="input-label">Color: </span>
<input id="colorpicker" class="input-field">
</div>
<div class="input-row">
<span class="input-label">Black name: </span>
<div class="switch input-field">
<input id="black-name" class="cmn-toggle cmn-toggle-round-flat" type="checkbox">
<label for="black-name"></label>
</div><!-- .switch -->
</div>
</div><!-- .board -->
</div><!-- .inputs -->
</div><!-- .col-md-6 -->
</div><!-- .row -->
</div><!-- .app -->
</div><!-- .row -->
</div><!-- .container -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/spectrum/spectrum.js"></script>
<script src="bower_components/localforage/dist/localforage.min.js"></script>
<script src="js/script.js"></script>
<script>
var appName = '#SaveASEANSeas',
$colorpicker = $('#colorpicker'),
initColor = '#4194e0',
initName = appName,
isDebugMode = true,
dataUriBase64, // original image data
canvasPreview960Id = 'canvas-preview960',
canvas960 = document.getElementById(canvasPreview960Id),
$canvas960 = $('#' + canvasPreview960Id),
ctx960 = canvas960.getContext('2d'),
canvas960Width = 960,
canvas960Height = 960,
canvasPreview300Id = 'canvas-preview300',
canvas300 = document.getElementById(canvasPreview300Id),
$canvas300 = $('#' + canvasPreview300Id),
ctx300 = canvas300.getContext('2d'),
canvas300Width = 300,
canvas300Height = 300,
defaultImagePath = 'images/baby.png';
html = '';
canvas960.width = canvas960Width;
canvas960.height = canvas960Height;
canvas300.width = canvas300Width;
canvas300.height = canvas300Height;
function appLog(msg) {
if (isDebugMode) console.log(msg);
}
function handleImage(e) {
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image(),
imgWidth,
imgHeight;
img.onload = function() {
imgWidth = img.width;
imgHeight = img.height;
// local
var localCanvas = document.createElement('canvas'),
localCtx = localCanvas.getContext('2d');
localCanvas.width = img.width;
localCanvas.height = img.height;
localCtx.drawImage(img, 0, 0);
// global 300
ctx300.clearRect(0, 0, canvas300.width, canvas300.height);
ctx300.drawImage(img, 0, 0, imgWidth, imgHeight, 0, 0, imgWidth, imgHeight);
dataUriBase64 = localCanvas.toDataURL('image/png');
// global 960
ctx960.clearRect(0, 0, canvas960.width, canvas960.height);
ctx960.drawImage(img, 0, 0, imgWidth, imgHeight, 0, 0, imgWidth, imgHeight);
dataUriBase64 = localCanvas.toDataURL('image/png');
Storage.updateImage(dataUriBase64);
Preview.updateImage(dataUriBase64);
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
/**
* Get ImageData
* used for default image only
* used for this app only
*
* @see https://davidwalsh.name/convert-image-data-uri-javascript
* @see http://stackoverflow.com/questions/934012/get-image-data-in-javascript
*
* @param {string} url
* @param {Function} callback callback function
*/
function getImageDataUriBase64(url, cb) {
var image = new Image();
image.onload = function() {
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
dataUriBase64;
canvas.width = this.width;
canvas.height = this.height;
ctx.drawImage(this, 0, 0);
dataUriBase64 = canvas.toDataURL('image/png');
if (typeof cb === 'function') cb(dataUriBase64);
};
image.src = url;
image.style.display = 'none';
}
var Storage = {
appPrefix: 'sas-',
storageImageKey: this.appPrefix + 'image',
getImage: function(cb) {
localforage.getItem(this.storageImageKey, function(err, value) {
cb(err, value);
});
},
updateImage: function(dataUriBase64, cb) {
localforage.setItem(this.storageImageKey, dataUriBase64, function(err) {
if (err) appLog(err);
if (typeof cb === 'function') cb();
});
},
};
var Convert = {
base64ToDataUri: function(base64) {
return base64.replace(/^data:image\/(png|jpg);base64,/, '');
},
// http://stackoverflow.com/questions/17591148/converting-data-uri-to-image-data
dataUriToImageData: function(dataUri) {
return new Promise(function(resolve, reject) {
if (dataUri == null) return reject();
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
image = new Image();
image.addEventListener('load', function() {
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
resolve(ctx.getImageData(0, 0, canvas.width, canvas.height));
}, false);
image.src = dataUri;
});
},
};
var Preview = {
self: this,
recommendedColors: [
'#fdda40',
'#ffef51',
'#fba2b6',
'#fdcff4',
'#fbafed',
'#fd6da0',
'#0bc5ba',
'#88d85d',
'#4cffd7',
'#96fd87',
'#006c54',
'#3e468f',
'#bdfdfc',
'#4194e0',
'#10a2b9',
'#068dba',
'#ff7f2b',
'#ff6f2e',
'#770330',
'#ff3131',
],
$recommendedColorsWidget: $('.recommended-colors'),
$recommendedColors: $('.recommended-colors .colors'),
$preview: $('#preview'),
$previewName: $('#preview .name'),
$previewImage: $('#preview .image'),
$color: $('.recommended-colors .colors .color'),
updateImage: function(dataUriBase64) {
this.$previewImage.css({
'background-image': 'url(' + dataUriBase64 + ')',
});
},
/**
* Update #preview
*
* @param {string} hexCode
*/
updateBackgroundColor: function(hexCode) {
this.$preview.css({
'background-color': hexCode,
});
},
/**
* Update preview name
*
* @param {string} name
*/
updateName: function(name) {
this.$previewName.html(name);
},
updateNameColor: function(hexCode) {
this.$previewName.css({
'color': hexCode,
});
},
// reset to init state
// - remove additional class
resetRecommendedColors: function() {
$('.color').removeClass('active');
},
init: function() {
var self = this;
// render recommended colors
var html = '';
this.recommendedColors.forEach(function(color) {
html += getColorDiv(color);
});
this.$recommendedColors.html(html);
// color
this.$color = $('.recommended-colors .colors .color');
this.$color.on('click', function() {
var hexCode;
// set css class
self.resetRecommendedColors();
$(this).addClass('active');
// set preview
hexCode = $(this).data('hex-code');
updateAllBackgroundColor(hexCode);
});
}
};
var CanvasPreview = {
updateBackgroundImage: function(hexCode) {
this.render();
},
render: function() {
},
};
var Inputs = {
$inputUpload: $('input#input-upload').first(),
$inputName: $('input[name=name]').first(),
$inputBlackNameCheckbox: $('input#black-name').first(),
/**
* Update input name
*
* @param {string} name
*/
updateName: function(name) {
this.$inputName.val(name);
},
init: function() {
// image
this.$inputUpload.on('change', handleImage);
// name
this.$inputName.keyup(function() {
var name = $(this).val();
Preview.updateName(name);
});
// black name
this.$inputBlackNameCheckbox.change(function() {
var hexCode = ($(this).is(':checked')) ? '#000000' : '#ffffff';
Preview.updateNameColor(hexCode);
});
},
};
/**
* This function will set all previews with hexCode
*/
function updateAllBackgroundColor(hexCode) {
Preview.updateBackgroundColor(hexCode);
CanvasPreview.updateBackgroundImage(hexCode);
$colorpicker.spectrum('set', hexCode);
}
/**
* This function will set all previews with hexCode
*
* @param {string} name
*/
function updateAllNames(name) {
Preview.updateName(name);
Inputs.updateName(name);
}
function getColorDiv(hexCode) {
return '<div class="color" style="background-color: ' + hexCode + ';" data-hex-code="' + hexCode + '"></div>';
}
$(function() {
if (isDebugMode) {
$canvas960.addClass('show');
$canvas300.addClass('show');
}
// setup input - color
$colorpicker.spectrum({
color: initColor,
move: function(color) {
var hexCode = color.toHexString();
Preview.updateBackgroundColor(hexCode);
Preview.resetRecommendedColors();
}
});
Preview.init();
Inputs.init();
Storage.getImage(function(err, value) {
// http://stackoverflow.com/questions/4773966/drawing-an-image-from-a-data-url-to-a-canvas
var img = new Image();
if (value) {
dataUriBase64 = value;
// TODO: refactor
img.src = dataUriBase64;
ctx960.drawImage(img, 0, 0, img.width, img.height);
ctx300.drawImage(img, 0, 0, img.width, img.height);
Preview.updateImage(dataUriBase64);
} else {
// load default image instead
getImageDataUriBase64(defaultImagePath, function(base64) {
dataUriBase64 = base64;
// TODO: refactor
img.src = dataUriBase64;
ctx960.drawImage(img, 0, 0, img.width, img.height);
ctx300.drawImage(img, 0, 0, img.width, img.height);
Preview.updateImage(dataUriBase64);
// save
Storage.updateImage(dataUriBase64);
});
}
});
updateAllBackgroundColor(initColor);
updateAllNames(initName);
});
</script>
</body>
</html>