-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
660 lines (575 loc) · 16.7 KB
/
index.js
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Helper function that converts an object with r,g,b,a properties to a string
// of rgba(r,g,b,a) format
function objToRGBA(obj){
return 'rgba(' + obj.r + ',' + obj.g + ',' + obj.b + ',' + obj.a + ')';
}
// Takes RGBA string and turns it into object (with r,g,b,a properties).
// Assumes RGB [0,255] and A [0,1] and returns object RGBA [0,1].
function RGBAToObj(rgba){
var obj = {};
var sliced = rgba.concat("");
sliced = sliced.substr(sliced.indexOf('(')+1, ((sliced.indexOf(')'))-5));
obj.r = (sliced.substr(0, sliced.indexOf(',')))/255;
sliced = sliced.substr(sliced.indexOf(',')+1);
obj.g = (sliced.substr(0, sliced.indexOf(',')))/255;
sliced = sliced.substr(sliced.indexOf(',')+1);
obj.b = (sliced.substr(0, sliced.indexOf(',')))/255;
sliced = sliced.substr(sliced.indexOf(',')+1);
obj.a = sliced;
return obj;
}
// Addditive colouring between background and new colour.
// Can handle objects or strings of rgba.
function aOverB(a,b){
var aObj = {};
var bObj = {};
if (typeof a == 'string'){
aObj = RGBAToObj(a);
} else if (typeof a == 'object'){
aObj.r = a.r/255;
aObj.g = a.g/255;
aObj.b = a.b/255;
aObj.a = a.a;
};
if (typeof b == 'string'){
bObj = RGBAToObj(b);
} else if (typeof b == 'object'){
bObj.r = b.r/255;
bObj.g = b.g/255;
bObj.b = b.b/255;
bObj.a = b.a;
};
var alpha = 1 - (1 - aObj.a) * (1 - bObj.a);
return objToRGBA({
a: alpha,
r: Math.round((aObj.r * aObj.a / alpha + bObj.r * bObj.a * (1 - aObj.a) / alpha)*255),
g: Math.round((aObj.g * aObj.a / alpha + bObj.g * bObj.a * (1 - aObj.a) / alpha)*255),
b: Math.round((aObj.b * aObj.a / alpha + bObj.b * bObj.a * (1 - aObj.a) / alpha)*255)
});
}
// Current status of the mouse. Only updated when needed.
var mouse = {
down: false,
button: 0,
moving: false,
position: {}
};
// Holds data about the image being constructed by the user.
var image = {
map: [],
buffer: [],
stateLog: [],
state: -1,
origin: {},
topLeft: {x: 1, y: 1},
bottomRight: {x: 0, y: 0}
};
// Takes a pixel object with properties x, y, colour and undoColour.
image.addToStateLog = function(pixel){
var existsInCurrentState = false;
// If the state is empty, prep it for adding pixels.
if (!image.stateLog[image.state]) {
image.stateLog[image.state] = [];
// If it is not empty, check if it already exists and just change colour if so.
} else {
for (var j=0;j<image.stateLog[image.state].length;j++){
if (
image.stateLog[image.state][j].x == pixel.x &&
image.stateLog[image.state][j].y == pixel.y
){
existsInCurrentState = true;
// If the pixel colour is false, erase it.
if (pixel.colour === false){
image.stateLog[image.state][j].colour = false;
// If the pixel has a colour, combine with previous colour.
} else {
image.stateLog[image.state][j].colour =
aOverB(pixel.colour, image.stateLog[image.state][j].colour);
}
image.stateLog[image.state][j].undoColour = pixel.undoColour;
break;
}
}
}
// If it does not exist yet, add it.
if (!existsInCurrentState) {
image.stateLog[image.state].push({
"x": pixel.x, "y": pixel.y,
"colour": pixel.colour, "undoColour": pixel.undoColour
});
}
};
image.undo = function(){
// For each pixel in the current state,
for (var i=0;i<image.stateLog[image.state].length;i++){
var mapElement = image.existsIn(image.stateLog[image.state][i], image.map);
// If it doesn't exist, add a new one.
if (mapElement === -1){
if (image.stateLog[image.state][i].undoColour != false){
image.map.push({
"x": image.stateLog[image.state][i].x,
"y": image.stateLog[image.state][i].y,
"colour": image.stateLog[image.state][i].undoColour
});
view.drawPixel(image.map[image.map.length-1]);
}
// If the pixel can be found on the map
} else {
// If the colour was changed, change it on the map.
if (image.stateLog[image.state][i].undoColour){
image.map[mapElement].colour = image.stateLog[image.state][i].undoColour;
view.clearPixel(image.map[mapElement]);
view.drawPixel(image.map[mapElement]);
// If it was erased, erase it on the map.
} else {
view.clearPixel({"x": image.map[mapElement].x, "y": image.map[mapElement].y});
image.map.splice(mapElement,1);
}
}
}
}
image.redo = function(){
// For each pixel in the current state
for (var i=0;i<image.stateLog[image.state].length;i++){
var mapElement = image.existsIn(image.stateLog[image.state][i], image.map);
// If the pixel is already on the map, just modify it.
if (mapElement != -1){
image.map[mapElement].colour = image.stateLog[image.state][i].colour;
// If the pixel is not on the map, add it.
} else {
image.map.push({
"x": image.stateLog[image.state][i].x,
"y": image.stateLog[image.state][i].y,
"colour": image.stateLog[image.state][i].colour
});
mapElement = image.map.length-1;
}
view.clearPixel(image.map[mapElement]);
view.drawPixel(image.map[mapElement]);
}
}
image.existsIn = function(pixel, array){
for (var element in array){
if (array[element].x === pixel.x && array[element].y === pixel.y){
return element;
}
}
// Return -1 if not found in array
return -1;
}
image.addBufferToMap = function(){
for (var i=0; i<image.buffer.length; i++){
var mapElement = image.existsIn({"x": image.buffer[i].x, "y": image.buffer[i].y}, image.map);
var undoColour;
// If the pixel doesn't exist on the map, push it to the map.
if (mapElement === -1){
undoColour = false;
image.map.push({
"x": image.buffer[i].x,
"y": image.buffer[i].y,
"colour": image.buffer[i].colour
});
// If it does exist, change the colour.
} else {
undoColour = image.map[mapElement].colour;
image.map[mapElement].colour = aOverB(image.buffer[i].colour, image.map[mapElement].colour);
};
// In all cases, add it to the state log.
image.addToStateLog({
"x": image.buffer[i].x,
"y": image.buffer[i].y,
"colour": image.buffer[i].colour,
"undoColour": undoColour
});
}
image.buffer = [];
}
image.addPixelToBuffer = function(pixel){
var pixelInBuffer = image.existsIn(pixel, image.buffer);
if (pixelInBuffer >= 0){
image.buffer[pixelInBuffer].colour =
aOverB(pixel.colour, image.buffer[pixelInBuffer].colour);
} else {
image.buffer.push(pixel);
}
}
var view = {
scale: 25,
width: $(window).width(),
height: $(window).height(),
grid: true,
mode: "draw",
movementOffset: { // Used when mouse moves.
x: 0,
y: 0
},
toolbar: {
x: 49,
y: 49,
backColour: "rgba(0,0,0,0.2)",
foreColour: "rgba(255,0,0,0.2)",
},
canvas: document.getElementById('pixdraw'),
sharing: false,
topLeft: {x: 0, y: 0}
};
// Set the size of the canvas element. Literally.
view.canvas.width = view.width-1;
view.canvas.height = view.height-5;
// Get the context of the canvas. So we can draw!
view.ctx = view.canvas.getContext('2d');
view.ctx.strokeStyle = "#888888";
// Set the size of the toolbar element. Again, literally.
view.toolbar.elem = document.getElementById('toolbar');
view.toolbar.elem.width = 61;
view.toolbar.elem.height = 401;
// Set the location of the toolbar element... literally. Like on the page.
$(view.toolbar.elem).css('top', view.toolbar.x);
$(view.toolbar.elem).css('left', view.toolbar.y);
view.redraw = function(){
view.ctx.clearRect(0,0,view.width, view.height);
if (view.grid){
// Draw grid: vertical lines
for (var i=-0.5;i<view.canvas.width-0.5;i+=view.scale){
view.drawLine(
{
x:i+(view.movementOffset.x % view.scale),
y:-0.5
},
{
x:i+(view.movementOffset.x % view.scale),
y:view.canvas.height-0.5
},
'rgba(200,200,200,1)'
);
}
// Draw grid: horizontal lines
for (var j=-0.5;j<view.canvas.height-0.5;j+=view.scale){
view.drawLine(
{
x:-0.5,
y:j + (view.movementOffset.y % view.scale)
},
{
x:view.canvas.width-0.5,
y:j + (view.movementOffset.y % view.scale)
},
'rgba(200,200,200,1)'
);
}
}
// Draw pixels from map.
if (image && image.map){
for (var k=0;k<image.map.length;k++){
view.drawPixel(image.map[k]);
}
}
// Draw pixels from buffer.
if (image.buffer){
for (var m=0;m<image.buffer.length;m++){
view.drawPixel(image.buffer[m]);
}
}
};
view.drawLine = function(start,end,colour){
view.ctx.strokeStyle = colour;
view.ctx.beginPath();
view.ctx.moveTo(start.x, start.y);
view.ctx.lineTo(end.x,end.y);
view.ctx.closePath();
view.ctx.stroke();
};
view.draw = function(event){
// Round down, then multiply back up to find the screen coordinates of the box.
var clickBox = {
"x":
Math.floor(
(event.pageX - view.movementOffset.x)/view.scale
) * view.scale
- view.topLeft.x,
"y":
Math.floor(
(event.pageY - view.movementOffset.y)/view.scale
) * view.scale
- view.topLeft.y
};
// If no pixels have been drawn, the origin becomes the location of this box,
// relative to the rest of the boxes on the screen.
if (!image.origin.x){
image.origin = {
"x": Math.floor(clickBox.x/view.scale),
"y": Math.floor(clickBox.y/view.scale)
};
}
// After origin is found, we can find out what coord the pixel has.
var pixel = {
"x":
Math.floor(
(
(event.pageX - view.movementOffset.x)
- (image.origin.x * view.scale)
) / view.scale
),
"y":
Math.floor(
(
(event.pageY - view.movementOffset.y)
- (image.origin.y * view.scale)
) / view.scale
)
};
// We have two drawing colours depending on mouse button.
if (view.mode === "draw"){
if (mouse.button === 1){
pixel.colour = view.toolbar.foreColour;
} else if (mouse.button === 3){
pixel.colour = view.toolbar.backColour;
}
} else if (view.mode === "erase"){
pixel.colour = false;
}
// Add the pixel to the buffer, then draw it.
image.addPixelToBuffer({"x": pixel.x, "y": pixel.y, "colour": pixel.colour});
view.drawPixel(pixel);
}
view.move = function(event){
if (mouse.moving === "grid"){
// Move the view!
view.movementOffset.x += event.clientX - mouse.position.x;
view.movementOffset.y += event.clientY - mouse.position.y;
view.redraw();
mouse.position = {x: event.clientX, y: event.clientY};
}
}
view.drawPixel = function(pixel){
if (pixel.colour === false){
view.clearPixel(pixel);
} else {
view.ctx.fillStyle = pixel.colour;
var grid = 1;
if (view.grid == false) grid = 0;
view.ctx.fillRect(
(image.origin.x + pixel.x)*view.scale - view.topLeft.x + view.movementOffset.x,
(image.origin.y + pixel.y)*view.scale - view.topLeft.y + view.movementOffset.y,
view.scale-grid, view.scale-grid
);
}
}
view.clearPixel = function(pixel){
var grid = 1
if (view.grid == false) grid = 0;
view.ctx.clearRect(
(image.origin.x + pixel.x)*view.scale - view.topLeft.x + view.movementOffset.x,
(image.origin.y + pixel.y)*view.scale - view.topLeft.y + view.movementOffset.y,
view.scale-grid, view.scale-grid
);
}
view.share = function(){
view.sharing = !view.sharing;
// Reset the corner finder, as we are about to re-search for corners.
image.topLeft = {x: 1, y: 1};
image.bottomRight = {x: 0, y: 0};
for (var i=0;i<image.map.length;i++){
if (image.topLeft.x > image.map[i].x) image.topLeft.x = image.map[i].x;
if (image.topLeft.y > image.map[i].y) image.topLeft.y = image.map[i].y;
if (image.bottomRight.x < image.map[i].x) image.bottomRight.x = image.map[i].x;
if (image.bottomRight.y < image.map[i].y) image.bottomRight.y = image.map[i].y;
}
// Find the size of the image so we can crop to it.
var size = {
x: image.bottomRight.x - image.topLeft.x + 1,
y: image.bottomRight.y - image.topLeft.y + 1
};
if (view.sharing){
$('body').append(
'<div id="dimmer">' +
'<button id="close">x</button>' +
'</div>'
);
$("#dimmer").children("#close").on('click', function(){
view.unshare();
$("#dimmer").children("#close").off('click');
});
$('div#dimmer').css({'width': view.width, 'height': view.height});
view.canvas.width = size.x*view.scale;
view.canvas.height = size.y*view.scale;
$('#pixdraw').css({
'top':50,'left':150,
'position':'absolute','border':'1px solid black'
});
view.topLeft = {
x: (image.origin.x+ image.topLeft.x) * view.scale,
y: (image.origin.y+ image.topLeft.y) * view.scale
};
} else {
view.unshare();
}
view.redraw();
/*if (view.sharing){
share.upload(view.canvas.toDataURL('image/png').split(',')[1]);
}*/
}
view.unshare = function(){
view.sharing = !view.sharing;
view.width = $(window).width();
view.height = $(window).height();
view.canvas.width = view.width-1;
view.canvas.height = view.height-5;
view.redraw();
$('#pixdraw').css({
'top':0,'left':0,
'position':'absolute','border':0
});
view.topLeft = {x:0,y:0};
$('div#dimmer').remove();
}
share = {
upload: function(image){
$.ajax({
url: 'http://api.imgur.com/2/upload.json',
type: 'POST',
data: {
type: 'image/png;base64',
key: '24d00bbfcd3d433095c97d48a4b10ebd',
name: '',
title: 'test title',
caption: 'test caption',
'image': image
},
dataType: 'json'
}).success(function(data) {
console.log(data);
}).error(function(data) {
console.log(data);
});
}
}
view.redraw();
// This enables you to draw in the background colour without the
// context menu getting in the way when you right-click.
$("#pixdraw").bind("contextmenu", function(e) {
return false;
});
// Colour picker!
$('#fore').css({'background-color': view.toolbar.foreColour});
$('#back').css({'background-color': view.toolbar.backColour});
$('#fore').colorpicker({format: 'rgba'}).on('changeColor', function(event){
view.toolbar.foreColour = objToRGBA(event.color.toRGB());
$('#fore').css({'background-color': objToRGBA(event.color.toRGB())});
});
$('#back').colorpicker({format: 'rgba'}).on('changeColor', function(event){
view.toolbar.backColour = objToRGBA(event.color.toRGB());
$('#back').css({'background-color': objToRGBA(event.color.toRGB())});
});
// Mousedown bindings.
$('#share').mousedown(function(){
view.share();
});
$('#mover').mousedown(function(){view.mode = "move";});
$('#drawer').mousedown(function(){view.mode = "draw";});
$('#eraser').mousedown(function(){view.mode = "erase";});
$('#toggle-grid').mousedown(function(){view.grid = !view.grid; view.redraw();});
$('#pixdraw').mousedown(function(event){
mouse.position = {x: event.clientX, y: event.clientY};
mouse.down = true;
mouse.button = event.which;
mouse.moving = "grid";
if (view.mode === "move"){
view.move(event);
} else if (view.mode === "draw" || view.mode === "erase"){
view.draw(event);
}
/*
if (view.mode == "draw"){
image.state++;
view.drawClick(event);
} else if (view.mode == "move"){
view.move(event);
} else if (view.mode == "erase"){
image.state++;
view.erase(event);
};
*/
});
$('#toolbar').children().mousedown(function(event){
event.stopPropagation();
});
$('#toolbar').mousedown(function(event){
mouse.position = {x: event.clientX, y: event.clientY};
mouse.down = true;
if (view.mode == "move"){
mouse.moving = "toolbar";
}
});
$('#undo').mousedown(function(event){
if (image.state >= 0) {
image.undo();
image.state--;
}
});
$('#redo').mousedown(function(event){
if (image.state < image.stateLog.length-1){
image.state++;
image.redo();
}
});
// Mouseup bindings.
$('#pixdraw').mouseup(function(){
mouse.down = false;
mouse.button = false;
mouse.moving = false;
if (view.mode === "draw" || view.mode === "erase"){
image.stateLog = image.stateLog.slice(0, image.state+1);
image.state++;
image.addBufferToMap();
}
});
$('#toolbar').mouseup(function(){
mouse.down = false;
mouse.moving = false;
});
// Mousemove binding.
$(window).mousemove(function(event){
if (mouse.down === true){
if (view.mode === "move"){
if (mouse.moving === "toolbar"){
view.toolbar.x+= event.clientX - mouse.position.x;
view.toolbar.y+= event.clientY - mouse.position.y;
mouse.position = {x: event.clientX, y: event.clientY};
$('#toolbar').css({'top': view.toolbar.y, 'left': view.toolbar.x});
}
if (mouse.moving === "grid"){
view.move(event);
}
}
if (view.mode === "draw" || view.mode === "erase"){
if (mouse.moving === "grid"){
view.draw(event);
mouse.position = {x: event.clientX, y: event.clientY};
}
}
}
});
// Mousewheel binding.
$("#pixdraw").bind('mousewheel', function(event, delta) {
// Determine which way the mouse wheel spun, and scale the page.
if (!view.sharing){
if (delta > 0){
if (view.scale<100) view.scale++;
} else if (delta < 0){
if (view.scale>1) view.scale--;
}
view.ctx.clearRect(0,0,view.canvas.width,view.canvas.height);
view.redraw();
}
});
// Resize binding.
$(window).resize(function(){
if (!view.sharing){
view.width = $(window).width();
view.height = $(window).height();
view.canvas.width = view.width-1;
view.canvas.height = view.height-5;
view.redraw();
}
});