-
Notifications
You must be signed in to change notification settings - Fork 608
/
draw_tools.js
1699 lines (1575 loc) · 57.9 KB
/
draw_tools.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global MAIN, HELPER, LAYER, EDIT, POP, GUI, EVENTS, IMAGE, EL, fx, ImageFilters, sketchy_brush, shaded_brush, chrome_brush, BezierCurveBrush */
/* global WIDTH, HEIGHT, COLOR, canvas_active, canvas_front */
var DRAW = new DRAW_TOOLS_CLASS();
/**
* manages draw tools
*
* @author ViliusL
*/
function DRAW_TOOLS_CLASS() {
/**
* user action for selected area
*/
this.select_square_action = '';
/**
* previous line coordinates [x, y]
*/
this.last_line = [];
/**
* user selected area config - array(x, y, width, height)
*/
this.select_data = false;
/**
* currently used tool
*/
this.active_tool = 'brush';
/**
* line points data for curved line
*/
var curve_points = [];
/**
* image data for cloning tool
*/
var clone_data = false;
/**
* fx library object
*/
var fx_filter = false;
//credits to Victor Haydin
this.toolFiller = function (context, W, H, x, y, color_to, sensitivity, anti_aliasing) {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
var img_tmp = canvas_front.getImageData(0, 0, W, H);
var imgData_tmp = img_tmp.data;
var img = context.getImageData(0, 0, W, H);
var imgData = img.data;
var k = ((y * (img.width * 4)) + (x * 4));
var dx = [0, -1, +1, 0];
var dy = [-1, 0, 0, +1];
var color_from = {
r: imgData[k + 0],
g: imgData[k + 1],
b: imgData[k + 2],
a: imgData[k + 3]
};
if (color_from.r == color_to.r && color_from.g == color_to.g && color_from.b == color_to.b && color_from.a == color_to.a) {
return false;
}
var stack = [];
stack.push([x, y]);
while (stack.length > 0) {
var curPoint = stack.pop();
for (var i = 0; i < 4; i++) {
var nextPointX = curPoint[0] + dx[i];
var nextPointY = curPoint[1] + dy[i];
if (nextPointX < 0 || nextPointY < 0 || nextPointX >= W || nextPointY >= H)
continue;
var k = (nextPointY * W + nextPointX) * 4;
if (imgData_tmp[k + 3] != 0)
continue; //already parsed
//check
if (Math.abs(imgData[k + 0] - color_from.r) <= sensitivity &&
Math.abs(imgData[k + 1] - color_from.g) <= sensitivity &&
Math.abs(imgData[k + 2] - color_from.b) <= sensitivity &&
Math.abs(imgData[k + 3] - color_from.a) <= sensitivity) {
//fill pixel
imgData_tmp[k] = color_to.r; //r
imgData_tmp[k + 1] = color_to.g; //g
imgData_tmp[k + 2] = color_to.b; //b
imgData_tmp[k + 3] = color_to.a; //a
stack.push([nextPointX, nextPointY]);
}
}
}
canvas_front.putImageData(img_tmp, 0, 0);
if (anti_aliasing == true) {
context.shadowColor = "rgba(" + color_to.r + ", " + color_to.g + ", " + color_to.b + ", " + color_to.a / 255 + ")";
context.shadowBlur = 5;
}
context.drawImage(document.getElementById("canvas_front"), 0, 0);
//reset
context.shadowBlur = 0;
};
this.tool_magic_wand = function (context, W, H, x, y, sensitivity, anti_aliasing) {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
canvas_front.rect(0, 0, WIDTH, HEIGHT);
canvas_front.fillStyle = "rgba(255, 255, 255, 0)";
canvas_front.fill();
var img_tmp = canvas_front.getImageData(0, 0, W, H);
var imgData_tmp = img_tmp.data;
var img = context.getImageData(0, 0, W, H);
var imgData = img.data;
var k = ((y * (img.width * 4)) + (x * 4));
var dx = [0, -1, +1, 0];
var dy = [-1, 0, 0, +1];
var color_to = {
r: 255,
g: 255,
b: 255,
a: 255
};
var color_from = {
r: imgData[k + 0],
g: imgData[k + 1],
b: imgData[k + 2],
a: imgData[k + 3]
};
if (color_from.r == color_to.r &&
color_from.g == color_to.g &&
color_from.b == color_to.b &&
color_from.a == 0) {
return false;
}
var stack = [];
stack.push([x, y]);
while (stack.length > 0) {
var curPoint = stack.pop();
for (var i = 0; i < 4; i++) {
var nextPointX = curPoint[0] + dx[i];
var nextPointY = curPoint[1] + dy[i];
if (nextPointX < 0 || nextPointY < 0 || nextPointX >= W || nextPointY >= H)
continue;
var k = (nextPointY * W + nextPointX) * 4;
if (imgData_tmp[k + 3] != 0)
continue; //already parsed
if (Math.abs(imgData[k] - color_from.r) <= sensitivity
&& Math.abs(imgData[k + 1] - color_from.g) <= sensitivity
&& Math.abs(imgData[k + 2] - color_from.b) <= sensitivity
&& Math.abs(imgData[k + 3] - color_from.a) <= sensitivity) {
imgData_tmp[k] = color_to.r; //r
imgData_tmp[k + 1] = color_to.g; //g
imgData_tmp[k + 2] = color_to.b; //b
imgData_tmp[k + 3] = color_to.a; //a
stack.push([nextPointX, nextPointY]);
}
}
}
//destination-out + blur = anti-aliasing
if (anti_aliasing == true)
img_tmp = ImageFilters.StackBlur(img_tmp, 2);
canvas_front.putImageData(img_tmp, 0, 0);
context.globalCompositeOperation = "destination-out";
context.drawImage(document.getElementById("canvas_front"), 0, 0);
//reset
context.shadowBlur = 0;
context.globalCompositeOperation = 'source-over';
};
//type = click, right_click, drag, move, release
this.select_tool = function (type, mouse, event) {
if (mouse == undefined)
return false;
if (mouse.valid == false)
return true;
if (mouse.click_valid == false)
return true;
if (event != undefined && event.target.id == "canvas_preview")
return true;
var active_layer_obj = document.getElementById(LAYER.layers[LAYER.layer_active].name);
if (type == 'drag') {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
if(active_layer_obj.style.visibility != 'hidden'){
//hide active layer
active_layer_obj.style.visibility = 'hidden';
}
if(EVENTS.ctrl_pressed == true){
//ctrl is pressed
var xx = mouse.x;
var yy = mouse.y;
if (Math.abs(mouse.click_x - mouse.x) < Math.abs(mouse.click_y - mouse.y))
xx = mouse.click_x;
else
yy = mouse.click_y;
canvas_front.drawImage(canvas_active(true), xx - mouse.click_x, yy - mouse.click_y);
}
else{
canvas_front.drawImage(canvas_active(true), mouse.x - mouse.click_x, mouse.y - mouse.click_y);
}
}
else if (type == 'release') {
//show active layer
active_layer_obj.style.visibility = 'visible';
if (mouse.valid == false || mouse.click_x === false){
return false;
}
if (mouse.x - mouse.click_x == 0 && mouse.y - mouse.click_y == 0){
return false;
}
EDIT.save_state();
var tmp = canvas_active().getImageData(0, 0, WIDTH, HEIGHT);
canvas_active().clearRect(0, 0, WIDTH, HEIGHT);
if(EVENTS.ctrl_pressed == true){
//ctrl is pressed
var xx = mouse.x;
var yy = mouse.y;
if (Math.abs(mouse.click_x - mouse.x) < Math.abs(mouse.click_y - mouse.y))
xx = mouse.click_x;
else
yy = mouse.click_y;
canvas_active().putImageData(tmp, xx - mouse.click_x, yy - mouse.click_y);
}
else{
canvas_active().putImageData(tmp, mouse.x - mouse.click_x, mouse.y - mouse.click_y);
}
}
};
this.magic_wand = function (type, mouse, event) {
if (mouse.valid == false)
return true;
if (type == 'click') {
EDIT.save_state();
this.tool_magic_wand(canvas_active(), WIDTH, HEIGHT, mouse.x, mouse.y, GUI.action_data().attributes.power, GUI.action_data().attributes.anti_aliasing);
}
};
this.erase = function (type, mouse, event) {
if (mouse.valid == false || (mouse.click_valid == false && type != 'move'))
return true;
var strict = GUI.action_data().attributes.strict;
var size = GUI.action_data().attributes.size;
var is_circle = GUI.action_data().attributes.circle;
var strict_element = document.getElementById('strict');
if(is_circle == false){
//hide strict controlls
if(strict_element != undefined)
strict_element.style.display = 'none';
}
else{
//show strict controlls
if(strict_element != undefined)
strict_element.style.display = 'block';
}
if (type == 'click') {
EDIT.save_state();
canvas_active().beginPath();
canvas_active().lineWidth = size;
canvas_active().lineCap = 'round';
canvas_active().lineJoin = 'round';
if (ALPHA < 255)
canvas_active().strokeStyle = "rgba(255, 255, 255, " + ALPHA / 255 / 10 + ")";
else
canvas_active().strokeStyle = "rgba(255, 255, 255, 1)";
if (is_circle == false) {
//rectangle
canvas_active().save();
canvas_active().globalCompositeOperation = 'destination-out';
canvas_active().fillStyle = "rgba(255, 255, 255, " + ALPHA / 255 + ")";
canvas_active().fillRect(mouse.x - Math.ceil(size / 2) + 1, mouse.y - Math.ceil(size / 2) + 1, size, size);
canvas_active().restore();
}
else {
//circle
canvas_active().save();
if (strict == false) {
var radgrad = canvas_active().createRadialGradient(
mouse.x, mouse.y, size / 8,
mouse.x, mouse.y, size / 2);
radgrad.addColorStop(0, "rgba(255, 255, 255, " + ALPHA / 255 + ")");
radgrad.addColorStop(1, "rgba(255, 255, 255, 0)");
}
//set Composite
canvas_active().globalCompositeOperation = 'destination-out';
if (strict == true)
canvas_active().fillStyle = "rgba(255, 255, 255, " + ALPHA / 255 + ")";
else
canvas_active().fillStyle = radgrad;
canvas_active().beginPath();
canvas_active().arc(mouse.x, mouse.y, size / 2, 0, Math.PI * 2, true);
canvas_active().fill();
canvas_active().restore();
}
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
canvas_active().save();
if (strict == false && is_circle == true) {
var radgrad = canvas_active().createRadialGradient(
mouse.x, mouse.y, size / 10,
mouse.x, mouse.y, size / 2);
if (ALPHA < 255)
radgrad.addColorStop(0, "rgba(255, 255, 255, " + ALPHA / 255 / 10 + ")");
else
radgrad.addColorStop(0, "rgba(255, 255, 255, 1)");
radgrad.addColorStop(1, "rgba(255, 255, 255, 0)");
canvas_active().strokeStyle = radgrad;
}
canvas_active().save();
canvas_active().globalCompositeOperation = 'destination-out';
canvas_active().beginPath();
canvas_active().moveTo(mouse.last_x, mouse.last_y);
canvas_active().lineTo(mouse.x, mouse.y);
canvas_active().stroke();
canvas_active().restore();
//show size
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
if (is_circle == false) {
canvas_front.lineWidth = 1;
EL.rectangle_dashed(canvas_front, mouse.x - Math.ceil(size / 2) + 1, mouse.y - Math.ceil(size / 2) + 1, mouse.x + Math.floor(size / 2), mouse.y + Math.floor(size / 2), 1, '#000000');
}
else {
EL.circle(canvas_front, mouse.x, mouse.y, size);
}
}
else if (type == 'move') {
var size1 = Math.floor((size) / 2);
var size2 = Math.floor((size) / 2);
if (size % 2 == 0)
size2--;
else {
size1--;
}
//show size
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
if (is_circle == false) {
canvas_front.lineWidth = 1;
EL.rectangle_dashed(canvas_front, mouse.x - Math.ceil(size / 2) + 1, mouse.y - Math.ceil(size / 2) + 1, mouse.x + Math.floor(size / 2), mouse.y + Math.floor(size / 2), 1, '#000000');
}
else {
EL.circle(canvas_front, mouse.x, mouse.y, size);
}
}
};
this.fill = function (type, mouse, event) {
if (mouse.valid == false){
return true;
}
if (type == 'click') {
if(ALPHA == 0){
POP.add({html: 'Alpha color can not be zero.'});
POP.show('Error', '.');
return;
}
EDIT.save_state();
var color_to = HELPER.hex2rgb(COLOR);
color_to.a = ALPHA;
DRAW.toolFiller(canvas_active(), WIDTH, HEIGHT, mouse.x, mouse.y, color_to, GUI.action_data().attributes.power, GUI.action_data().attributes.anti_aliasing);
}
};
this.pick_color = function (type, mouse, event) {
if (mouse.valid == false)
return true;
if (type == 'click') {
var c = canvas_active().getImageData(mouse.x, mouse.y, 1, 1).data;
COLOR = "#" + ("000000" + HELPER.rgbToHex(c[0], c[1], c[2])).slice(-6);
if(c[3] > 0) {
//set alpha
ALPHA = c[3];
document.getElementById("rgb_a").value = ALPHA;
}
GUI.sync_colors();
}
};
this.pencil = function (type, mouse, event) {
if (mouse.valid == false || mouse.click_valid == false)
return true;
var color_rgb = HELPER.hex2rgb(COLOR);
if (type == 'click') {
EDIT.save_state();
}
else if (type == 'drag') {
//why no simple lines? this way turns off aliasing
if (mouse.last_x != false && mouse.last_y != false) {
//saving
dist_x = mouse.last_x - mouse.x;
dist_y = mouse.last_y - mouse.y;
distance = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y));
radiance = Math.atan2(dist_y, dist_x);
for (var i = 0; i < distance; i++) {
x_tmp = mouse.x + Math.cos(radiance) * i;
y_tmp = mouse.y + Math.sin(radiance) * i;
x_tmp = Math.round(x_tmp);
y_tmp = Math.round(y_tmp);
var my_color = HELPER.hex2rgb(COLOR);
canvas_active().fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().fillRect(x_tmp, y_tmp, 1, 1);
}
}
}
else if (type == 'release') {
canvas_active().fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().fillRect(mouse.x, mouse.y, 1, 1);
}
};
this.line = function (type, mouse, event) {
if (mouse.click_valid == false)
return false;
var color_rgb = HELPER.hex2rgb(COLOR);
//horizontal/vertical only
var xx = mouse.x;
var yy = mouse.y;
var from_x = mouse.click_x;
var from_y = mouse.click_y;
var attribute_type = GUI.action_data().attributes.type;
if(attribute_type == undefined)
attribute_type = 'Simple';
//set line endings
if(attribute_type == 'Simple'){
var lineCap = 'butt';
}
else{
var lineCap = 'round';
}
canvas_front.lineCap = lineCap;
canvas_active().lineCap = lineCap;
if (type == 'move') {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.lineWidth = GUI.action_data().attributes.size;
if (attribute_type == 'Curve') {
//curve
if (curve_points.length == 2) {
canvas_front.beginPath();
canvas_front.moveTo(curve_points[0][0] + 0.5, curve_points[0][1] + 0.5);
canvas_front.quadraticCurveTo(mouse.x + 0.5, mouse.y + 0.5, curve_points[1][0], curve_points[1][1]);
canvas_front.stroke();
}
}
}
else if (type == 'drag') {
document.body.style.cursor = "crosshair";
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
canvas_front.beginPath();
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.lineWidth = GUI.action_data().attributes.size;
if (attribute_type == 'Multi-line' && this.last_line[0] != undefined) {
from_x = this.last_line[0];
from_y = this.last_line[1];
}
if (EVENTS.ctrl_pressed == true) {
if (Math.abs(from_x - mouse.x) < Math.abs(from_y - mouse.y))
xx = from_x;
else
yy = from_y;
}
//arrow
if (attribute_type == 'Arrow') {
var headlen = GUI.action_data().attributes.size * 5;
if (headlen < 15)
headlen = 15;
EL.arrow(canvas_front, from_x + 0.5, from_y + 0.5, xx + 0.5, yy + 0.5, headlen);
}
//line
else {
canvas_front.moveTo(from_x + 0.5, from_y + 0.5);
canvas_front.lineTo(xx + 0.5, yy + 0.5);
canvas_front.stroke();
}
}
else if (type == 'click') {
//curve
if (attribute_type == 'Curve') {
EDIT.save_state();
canvas_active().beginPath();
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = GUI.action_data().attributes.size;
if (EVENTS.ctrl_pressed == true) {
if (Math.abs(from_x - mouse.x) < Math.abs(from_y - mouse.y))
xx = from_x;
else
yy = from_y;
}
if (curve_points.length == 2) {
canvas_active().beginPath();
canvas_active().moveTo(curve_points[0][0] + 0.5, curve_points[0][1] + 0.5);
canvas_active().quadraticCurveTo(xx + 0.5, yy + 0.5, curve_points[1][0], curve_points[1][1]);
canvas_active().stroke();
curve_points = [];
}
}
if (attribute_type != 'Multi-line'){
//reset last line position
DRAW.last_line = [];
}
}
else if (type == 'release') {
document.body.style.cursor = "auto";
if (mouse.x - mouse.click_x == 0 && mouse.y - mouse.click_y == 0 && attribute_type != 'Multi-line')
return false;
EDIT.save_state();
canvas_active().beginPath();
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = GUI.action_data().attributes.size;
if (attribute_type == 'Multi-line' && this.last_line[0] != undefined) {
from_x = DRAW.last_line[0];
from_y = DRAW.last_line[1];
}
if (EVENTS.ctrl_pressed == true) {
if (Math.abs(from_x - mouse.x) < Math.abs(from_y - mouse.y))
xx = from_x;
else
yy = from_y;
}
//arrow
if (attribute_type == 'Arrow') {
var headlen = GUI.action_data().attributes.size * 5;
if (headlen < 15)
headlen = 15;
EL.arrow(canvas_active(), from_x + 0.5, from_y + 0.5, xx + 0.5, yy + 0.5, headlen);
this.last_line[0] = xx;
this.last_line[1] = yy;
}
//curve
else if (attribute_type == 'Curve') {
if (curve_points.length == 0 && (mouse.click_x != mouse.x || mouse.click_y != mouse.y)) {
curve_points.push([mouse.click_x, mouse.click_y]);
curve_points.push([xx, yy]);
}
else if (curve_points.length == 2)
curve_points = [];
}
//line
else {
EL.line(canvas_active(), from_x, from_y, xx, yy);
this.last_line[0] = xx;
this.last_line[1] = yy;
}
}
};
this.letters = function (type, mouse, event) {
var _this = this;
if (mouse.valid == false)
return true;
var xx = mouse.x;
var yy = mouse.y;
if (type == 'click') {
POP.add({name: "text", title: "Text:", value: "", type: 'textarea'});
POP.add({name: "size", title: "Size:", value: 20, range: [2, 1000], step: 2});
POP.add({name: "color", title: "Color:", value: "#000000", type: "color"});
POP.add({name: "style", title: "Font style:", values: ["Normal", "Italic", "Bold", "Bold Italic"], type: 'select'});
POP.add({name: "family", title: "Font family:", values: ["Arial", "Courier", "Impact", "Helvetica", "monospace", "Times New Roman", "Verdana"], type: 'select'});
POP.add({name: "size_3d", title: "3D size:", value: 0, range: [0, 200]});
POP.add({name: "pos_3d", title: "3D position:", values: ["Top-left", "Top-right", "Bottom-left", "Bottom-right"], type: 'select'});
POP.add({name: "shadow", title: "Shadow:", values: ["No", "Yes"]});
POP.add({name: "shadow_blur", title: "Shadow blur:", value: 6, range: [1, 20]});
POP.add({name: "shadow_color", title: "Shadow color:", value: "#000000", type: "color"});
POP.add({name: "fill_style", title: "Fill style:", values: ["Fill", "Stroke", "Both"], type: 'select'});
POP.add({name: "stroke_size", title: "Stroke size:", value: 1, range: [1, 100]});
POP.preview_in_main = true;
POP.show(
'Text',
function (user_response) {
EDIT.save_state();
var trim_details = IMAGE.trim_info(canvas_active(true));
if (trim_details.empty == false) {
LAYER.layer_add();
}
text = user_response.text.split("\n");
for (var i in text) {
user_response.text = text[i];
var yyy = yy + i * (parseInt(user_response.size) + 2);
_this.letters_render(canvas_active(), xx, yyy, user_response);
}
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
},
function (user_response) {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
text = user_response.text.split("\n");
for (var i in text) {
user_response.text = text[i];
var yyy = yy + i * (parseInt(user_response.size) + 2);
_this.letters_render(canvas_front, xx, yyy, user_response);
}
}
);
}
};
this.letters_render = function (canvas, xx, yy, user_response) {
var text = user_response.text;
var size = parseInt(user_response.size);
var color = user_response.color;
var dpth = parseInt(user_response.size_3d);
var pos_3d = user_response.pos_3d;
var shadow = user_response.shadow;
var shadow_blur = parseInt(user_response.shadow_blur);
var shadow_color = user_response.shadow_color;
var font = user_response.family;
var font_style = user_response.style;
var fill_style = user_response.fill_style;
var stroke_size = user_response.stroke_size;
var dx;
var dy;
if (pos_3d == "Top-left") {
dx = -1;
dy = -1;
}
else if (pos_3d == "Top-right") {
dx = 1;
dy = -1;
}
else if (pos_3d == "Bottom-left") {
dx = -1;
dy = 1;
}
else if (pos_3d == "Bottom-right") {
dx = 1;
dy = 1;
}
var color_rgb = HELPER.hex2rgb(color);
canvas.fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas.font = font_style + " " + size + "px " + font;
var letters_height = HELPER.font_pixel_to_height(size);
//shadow
if (shadow == 'Yes') {
canvas.save();
canvas.shadowColor = shadow_color;
canvas.shadowBlur = shadow_blur;
canvas.shadowOffsetX = dx;
canvas.shadowOffsetY = dy;
canvas.fillText(text, xx + dx * (dpth - 1), yy + letters_height + dy * (dpth - 1));
canvas.restore();
}
//3d
if (dpth > 0) {
canvas.fillStyle = HELPER.darkenColor(COLOR, -30);
//canvas.fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
for (cnt = 0; cnt < dpth; cnt++)
canvas.fillText(text, xx + dx * cnt, yy + letters_height + dy * cnt);
//color_rgb = HELPER.hex2rgb(COLOR);
}
//main text
canvas.fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas.lineWidth = stroke_size;
if (fill_style == 'Fill' || fill_style == 'Both')
canvas.fillText(text, xx, yy + letters_height);
if (fill_style == 'Stroke' || fill_style == 'Both')
canvas.strokeText(text, xx, yy + letters_height);
GUI.zoom();
};
this.draw_square = function (type, mouse, event) {
if (mouse.click_valid == false)
return true;
var color_rgb = HELPER.hex2rgb(COLOR);
var fill = GUI.action_data().attributes.fill;
var width = mouse.x - mouse.click_x;
var height = mouse.y - mouse.click_y;
if (type == 'drag') {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
canvas_front.fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.lineWidth = 1;
if (GUI.action_data().attributes.square == true)
EL.square(canvas_front, mouse.click_x, mouse.click_y, width, height, fill);
else
EL.rectangle(canvas_front, mouse.click_x, mouse.click_y, width, height, fill);
}
else if (type == 'release') {
if(mouse.x == mouse.click_x && mouse.y == mouse.click_y)
return false;
EDIT.save_state();
canvas_active().fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = 1;
if (GUI.action_data().attributes.square == true)
EL.square(canvas_active(), mouse.click_x, mouse.click_y, width, height, fill);
else
EL.rectangle(canvas_active(), mouse.click_x, mouse.click_y, width, height, fill);
}
};
this.draw_circle = function (type, mouse, event) {
if (mouse.click_valid == false)
return true;
var color_rgb = HELPER.hex2rgb(COLOR);
if (type == 'drag') {
dist_x = mouse.x - mouse.click_x;
dist_y = mouse.y - mouse.click_y;
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
if (GUI.action_data().attributes.circle == true)
dist_x = dist_y = Math.min(dist_x, dist_y);
if (GUI.action_data().attributes.fill == true)
EL.ellipse_by_center(canvas_front, mouse.click_x, mouse.click_y, dist_x * 2, dist_y * 2, "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")", true);
else
EL.ellipse_by_center(canvas_front, mouse.click_x, mouse.click_y, dist_x * 2, dist_y * 2, "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")");
}
else if (type == 'release') {
dist_x = mouse.x - mouse.click_x;
dist_y = mouse.y - mouse.click_y;
if(dist_x == 0 && dist_y == 0)
return false;
EDIT.save_state();
if (GUI.action_data().attributes.circle == true)
dist_x = dist_y = Math.min(dist_x, dist_y);
canvas_active().lineWidth = 1;
if (GUI.action_data().attributes.fill == true)
EL.ellipse_by_center(canvas_active(), mouse.click_x, mouse.click_y, dist_x * 2, dist_y * 2, "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")", true);
else
EL.ellipse_by_center(canvas_active(), mouse.click_x, mouse.click_y, dist_x * 2, dist_y * 2, "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")");
}
};
this.update_brush = function () {
document.getElementById('anti_aliasing').style.display = '';
document.getElementById('smart_brush').style.display = '';
if (GUI.action_data().attributes.type != 'Brush'){
document.getElementById('anti_aliasing').style.display = 'none';
document.getElementById('smart_brush').style.display = 'none';
}
};
this.desaturate_tool = function (type, mouse, event) {
if (mouse.valid == false)
return true;
var size = GUI.action_data().attributes.size;
var xx = mouse.x - size / 2;
var yy = mouse.y - size / 2;
if (xx < 0)
xx = 0;
if (yy < 0)
yy = 0;
if (type == 'click') {
EDIT.save_state();
var imageData = canvas_active().getImageData(xx, yy, size, size);
var filtered = ImageFilters.GrayScale(imageData); //add effect
EL.image_round(canvas_active(), mouse.x, mouse.y, size, filtered, document.getElementById("canvas_front"), GUI.action_data().attributes.anti_aliasing);
}
else if (type == 'drag') {
var imageData = canvas_active().getImageData(xx, yy, size, size);
var filtered = ImageFilters.GrayScale(imageData); //add effect
EL.image_round(canvas_active(), mouse.x, mouse.y, size, filtered, document.getElementById("canvas_front"), GUI.action_data().attributes.anti_aliasing);
}
if (type == 'move' || type == 'drag') {
//show size
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
EL.circle(canvas_front, mouse.x, mouse.y, size);
}
};
this.brush = function (type, mouse, event) {
if (mouse.valid == false || (mouse.click_valid == false && type != 'move'))
return true;
var brush_type = GUI.action_data().attributes.type;
var color_rgb = HELPER.hex2rgb(COLOR);
var size = parseInt(GUI.action_data().attributes.size);
if (type == 'click')
EDIT.save_state();
if (brush_type == 'Brush') {
if (type == 'click') {
//init settings
canvas_active().beginPath();
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = size;
canvas_active().lineCap = 'round';
canvas_active().lineJoin = 'round';
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
if (ALPHA < 255) {
canvas_front.beginPath();
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.lineWidth = size;
canvas_front.lineCap = 'round';
canvas_front.lineJoin = 'round';
}
//blur
canvas_active().shadowBlur = 0;
if (GUI.action_data().attributes.anti_aliasing == true) {
canvas_active().shadowColor = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().shadowBlur = Math.round(size);
}
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
//detect line size
var max_speed = 20;
var power = 2; //how speed affects size
if (GUI.action_data().attributes.smart_brush == true) {
var new_size = size + size / max_speed * mouse.speed_average * power;
new_size = Math.max(new_size, size/4);
new_size = Math.round(new_size);
canvas_front.lineWidth = new_size;
canvas_active().lineWidth = new_size;
}
else{
canvas_front.lineWidth = size;
canvas_active().lineWidth = size;
}
if (ALPHA == 255)
canvas_active().beginPath();
canvas_active().moveTo(mouse.last_x, mouse.last_y);
canvas_active().lineTo(mouse.x, mouse.y);
if (ALPHA == 255)
canvas_active().stroke();
//now draw preview
if (ALPHA < 255) {
canvas_front.beginPath();
//clean from last line
canvas_front.globalCompositeOperation = "destination-out";
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", 1)";
canvas_front.moveTo(mouse.last_x, mouse.last_y);
canvas_front.lineTo(mouse.x, mouse.y);
canvas_front.stroke();
//reset
canvas_front.strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_front.globalCompositeOperation = "source-over";
//draw new line segment
canvas_front.moveTo(mouse.last_x, mouse.last_y);
canvas_front.lineTo(mouse.x, mouse.y);
canvas_front.stroke();
}
}
else if (type == 'release') {
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
//paint everything
canvas_active().stroke();
//if mouse was not moved
if (mouse.click_x == mouse.x && mouse.click_y == mouse.y) {
canvas_active().beginPath();
canvas_active().arc(mouse.x, mouse.y, size / 2, 0, 2 * Math.PI, false);
canvas_active().fillStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().fill();
}
canvas_active().shadowBlur = 0;
}
else if (type == 'move') {
//show size
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
EL.circle(canvas_front, mouse.x, mouse.y, size);
}
}
else if (brush_type == 'BezierCurve') {
if (type == 'click')
BezierCurveBrush.startCurve(mouse.x, mouse.y);
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
var color_rgb = HELPER.hex2rgb(COLOR);
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = 0.5;
BezierCurveBrush.draw(canvas_active(), color_rgb, mouse.x, mouse.y, size);
}
}
else if (brush_type == 'Chrome') {
if (type == 'click') {
chrome_brush.init(canvas_active());
chrome_brush.strokeStart(mouse.x, mouse.y);
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
var color_rgb = HELPER.hex2rgb(COLOR);
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = 1;
chrome_brush.stroke(color_rgb, mouse.x, mouse.y, size);
}
}
else if (brush_type == 'Fur') {
if (type == 'click') {
points = new Array();
prevMouseX = mouse.x;
prevMouseY = mouse.y;
count = 0;
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
var color_rgb = HELPER.hex2rgb(COLOR);
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", 0.1)";
canvas_active().lineWidth = 1;
f = mouse.x;
c = mouse.y;
var e, b, a, g;
points.push([f, c]);
canvas_active().beginPath();
canvas_active().moveTo(prevMouseX, prevMouseY);
canvas_active().lineTo(f, c);
canvas_active().stroke();
for (e = 0; e < points.length; e++) {
b = points[e][0] - points[count][0];
a = points[e][1] - points[count][1];
g = b * b + a * a;
var g_size = Math.round(400 * size);
if (g < g_size && Math.random() > g / g_size) {
canvas_active().beginPath();
canvas_active().moveTo(f + (b * 0.5), c + (a * 0.5));
canvas_active().lineTo(f - (b * 0.5), c - (a * 0.5));
canvas_active().stroke();
}
}
prevMouseX = f;
prevMouseY = c;
count++;
}
}
else if (brush_type == 'Grouped') {
groups_n = size;
gsize = 10;
random_power = 5;
if (type == 'click') {
chrome_brush.init(canvas_active());
chrome_brush.strokeStart(mouse.x, mouse.y);
groups = [];
for (var g = 0; g < groups_n; g++) {
groups[g] = {};
groups[g].x = HELPER.getRandomInt(-gsize, gsize);
groups[g].y = HELPER.getRandomInt(-gsize, gsize);
}
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = 0.5;
for (var g in groups) {
canvas_active().beginPath();
canvas_active().moveTo(mouse.last_x + groups[g].x, mouse.last_y + groups[g].y);
//randomize here
groups[g].x += HELPER.getRandomInt(-random_power, random_power);
groups[g].y += HELPER.getRandomInt(-random_power, random_power);
if (groups[g].x < -gsize)
groups[g].x = -gsize + random_power;
if (groups[g].y < -gsize)
groups[g].y = -gsize + random_power;
if (groups[g].x > gsize)
groups[g].x = gsize - random_power;
if (groups[g].y > gsize)
groups[g].y = gsize - random_power;
canvas_active().lineTo(mouse.x + groups[g].x, mouse.y + groups[g].y);
canvas_active().stroke();
}
}
}
else if (brush_type == 'Shaded') {
if (type == 'click') {
shaded_brush.init(canvas_active());
shaded_brush.strokeStart(mouse.x, mouse.y);
}
else if (type == 'drag' && mouse.last_x != false && mouse.last_y != false) {
var color_rgb = HELPER.hex2rgb(COLOR);
canvas_active().strokeStyle = "rgba(" + color_rgb.r + ", " + color_rgb.g + ", " + color_rgb.b + ", " + ALPHA / 255 + ")";
canvas_active().lineWidth = 1;
shaded_brush.stroke(color_rgb, mouse.x, mouse.y, size);
}