-
Notifications
You must be signed in to change notification settings - Fork 110
/
clip.min.js
1855 lines (1613 loc) · 45.5 KB
/
clip.min.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
shape_array = {
polygon: [
{
name: "Triangle",
coords: [[50, 0], [0, 100], [100, 100]]
},
{
name: "Trapezoid",
coords: [[20, 0], [80, 0], [100, 100], [0, 100]]
},
{
name: "Parallelogram",
coords: [[25, 0], [100, 0], [75, 100], [0, 100]]
},
{
name: "Rhombus",
coords: [[50, 0], [100, 50], [50, 100], [0, 50]]
},
{
name: "Pentagon",
coords: [[50, 0], [100, 38], [82, 100], [18, 100], [0, 38]]
},
{
name: "Hexagon",
coords: [[25, 0], [75,0], [100, 50], [75, 100], [25, 100], [0, 50]]
},
{
name: "Heptagon",
coords: [
[50, 0],
[90, 20],
[100, 60],
[75, 100],
[25, 100],
[0, 60],
[10, 20]
]
},
{
name: "Octagon",
coords: [
[30, 0],
[70, 0],
[100, 30],
[100, 70],
[70, 100],
[30, 100],
[0, 70],
[0, 30]
]
},
{
name: "Nonagon",
coords: [
[50, 0],
[83, 12],
[100, 43],
[94, 78],
[68, 100],
[32, 100],
[6, 78],
[0, 43],
[17, 12]
]
},
{
name: "Decagon",
coords: [
[50, 0],
[80, 10],
[100, 35],
[100, 70],
[80, 90],
[50, 100],
[20, 90],
[0, 70],
[0, 35],
[20, 10]
]
},
{
name: "Bevel",
coords: [
[20, 0],
[80, 0],
[100, 20],
[100, 80],
[80, 100],
[20, 100],
[0, 80],
[0, 20]
]
},
{
name: "Rabbet",
coords: [
[0, 15],
[15, 15],
[15, 0],
[85, 0],
[85, 15],
[100, 15],
[100, 85],
[85, 85],
[85, 100],
[15, 100],
[15, 85],
[0, 85]
]
},
{
name: "Left arrow",
coords: [
[40, 0],
[40, 20],
[100, 20],
[100, 80],
[40, 80],
[40, 100],
[0, 50]
]
},
{
name: "Right arrow",
coords: [
[0, 20],
[60, 20],
[60, 0],
[100, 50],
[60, 100],
[60, 80],
[0, 80]
]
},
{
name: "Left Point",
coords: [[25, 0], [100, 0], [100, 100], [25, 100], [0, 50]]
},
{
name: "Right Point",
coords: [[0, 0], [75, 0], [100, 50], [75, 100], [0, 100]]
},
{
name: "Left Chevron",
coords: [[100, 0], [75, 50], [100, 100], [25, 100], [0, 50], [25, 0]]
},
{
name: "Right Chevron",
coords: [[75, 0], [100, 50], [75, 100], [0, 100], [25, 50], [0, 0]]
},
{
name: "Star",
coords: [
[50, 0],
[61, 35],
[98, 35],
[68, 57],
[79, 91],
[50, 70],
[21, 91],
[32, 57],
[2, 35],
[39, 35]
]
},
{
name: "Cross",
coords: [
[10, 25],
[35, 25],
[35, 0],
[65, 0],
[65, 25],
[90, 25],
[90, 50],
[65, 50],
[65, 100],
[35, 100],
[35, 50],
[10, 50]
]
},
{
name: "Message",
coords: [
[0, 0],
[100, 0],
[100, 75],
[75, 75],
[75, 100],
[50, 75],
[0, 75]
]
},
{
name: "Close",
coords: [
[20, 0],
[0, 20],
[30, 50],
[0, 80],
[20, 100],
[50, 70],
[80, 100],
[100, 80],
[70, 50],
[100, 20],
[80, 0],
[50, 30]
]
},
{
name: "Frame",
coords: [
[0, 0],
[0, 100],
[25, 100],
[25, 25],
[75, 25],
[75, 75],
[25, 75],
[25, 100],
[100, 100],
[100, 0]
]
}
],
circle: [
{
name: "Circle",
radius: 50,
position: [50, 50],
coords: [[100, 50], [50, 50]],
disabled: true
}
],
ellipse: [
{
name: "Ellipse",
radius: [25, 40],
position: [50, 50],
coords: [[50, 50], [0, 50], [0, 50]],
disabled: false
}
],
inset: [
{
name: "Inset",
coords: [5, 20, 15, 10],
disabled: false
}
],
custom: [
{
name: "Custom Polygon",
coords: [
[10, 75],
[10, 25],
[35, 0],
[100, 10],
[90, 30],
[50, 30],
[40, 40],
[40, 60],
[50, 70],
[90, 70],
[100, 90],
[35, 100]
],
disabled: false
}
]
};
$html = $("html");
$body = $("body");
$box = $("#box");
$side = $(".side");
$clipboard = $(".clipboard");
$handles = $(".handles");
$shapes = $(".shapes ul");
$functions = $(".functions");
$clip_path = $(".clip-path");
$unprefixed = $(".unprefixed");
$demo = $(".demo");
$codepen = $(".edit-in-codepen");
$inset_round = $("#inset_round");
$demo_width = $("#demo_width");
$demo_height = $("#demo_height");
var start = shape_array.polygon[0];
(start_type = "polygon"),
(start_coords = start.coords),
(start_name = start.name),
(mobile_px_breakpoint = 800),
(width = 280),
(height = 280),
(flickity = false),
(grid = [0, 0]);
$(function() {
sizes();
init();
// Reevaluates max width/height on window resize
$(window).resize(function() {
var old_width = width;
var old_height = height;
handleReposition(old_width, old_height);
sizes();
});
// Add/remove prefixes
// Classes determine if code block is displayed
$("#webkit").change(function() {
if ($(this).is(":checked")) {
$(".webkit").addClass("show");
} else {
$(".webkit").removeClass("show");
}
finishCustomizing();
clipIt();
scrollTop();
});
// Toggle showing background outside clip-path
$("#shadowboard-toggle").change(function() {
if ($(this).is(":checked")) {
$(".shadowboard").css("opacity", ".25");
} else {
$(".shadowboard").css("opacity", "0");
}
scrollTop();
});
// Resize width/height of the demo
$('input[type="number"]').change(function() {
var old_width = width;
var old_height = height;
width = $demo_width.val();
if ($(window).width() < 800) {
var max_width = $(window).width() - 42;
} else {
var max_width = $(".demo-container").width() - 42;
}
var min_width = 100;
if (width > max_width) {
width = max_width;
}
if (width < min_width) {
width = min_width;
}
height = $demo_height.val();
if ($(window).width() < 800) {
var max_height = $(window).height() - $("header").outerHeight() - 42;
} else {
var max_height = $(".demo-container").height() - 42;
}
var min_height = 100;
if (height > max_height) {
height = max_height;
}
if (height < min_height) {
height = min_height;
}
// Calculate new position for each handle
handleReposition(old_width, old_height);
// Resize the demo
$demo_width.val(width);
$demo_height.val(height);
sizes();
scrollTop();
});
// Change of radius value of inset shape
$(".inset-round").focus(function() {
var val = $(this).val();
if (val == "5% 20% 0 10%") {
$(this).val("");
}
$(this).blur(function() {
var val = $(this).val();
if (val !== "") {
$(".round-value").text(" round " + val);
} else {
$(".round-value").text("");
$(this).val("5% 20% 0 10%");
}
clipIt();
});
});
// Change clipboard background image
$(".backgrounds img").mousedown(function() {
var url = $(this).attr("src");
setCustomBackground(url);
});
// Change clipboard background to custom url
$("#custom_url").blur(function() {
var url = $(this).val();
if (url !== "") {
setCustomBackground(url);
}
});
// Edit in Codepen
$codepen.click(codePen);
});
function setCustomBackground(url) {
var style =
".shadowboard, .clipboard { background-image: url(" + url + "); }";
$("#custom_background").html(style);
// Scroll to top of page
scrollTop();
}
function scrollTop() {
// Only if we are on the small screen
if ($(window).width() < mobile_px_breakpoint) {
$(window).scrollTop(0);
}
}
function init() {
console.log("init();");
type = start_type;
// Setup polygons
$.each(shape_array.polygon, function(i, shape) {
paths = "";
$.each(shape.coords, function(i, coord) {
type = "polygon";
var x = coord[0] + "%";
var y = coord[1] + "%";
var path = "clip-path: polygon()";
var coord = "";
if (i == shape.coords.length - 1) {
// last coordinate to add, omits a comma at the end
paths += x + " " + y;
var clip_path = "polygon(" + paths + ")";
appendFigure(clip_path, shape);
} else {
// loops through each coordinate and adds it to a list to add
paths += x + " " + y + ", ";
}
});
});
// Setup insets
$.each(shape_array.inset, function(i, shape) {
type = "inset";
var top = shape.coords[0] + "%";
var right = shape.coords[1] + "%";
var bottom = shape.coords[2] + "%";
var left = shape.coords[3] + "%";
var clip_path =
"inset(" + top + " " + right + " " + bottom + " " + left + ")";
appendFigure(clip_path, shape);
});
// Setup custom
$.each(shape_array.custom, function(i, shape) {
paths = "";
$.each(shape.coords, function(i, coord) {
type = "custom";
var x = coord[0] + "%";
var y = coord[1] + "%";
var path = "clip-path: polygon()";
var coord = "";
if (i == shape.coords.length - 1) {
// last coordinate to add, omits a comma at the end
paths += x + " " + y;
var clip_path = "polygon(" + paths + ")";
appendFigure(clip_path, shape);
} else {
// loops through each coordinate and adds it to a list to add
paths += x + " " + y + ", ";
}
});
});
// Setup circles
$.each(shape_array.circle, function(i, shape) {
type = "circle";
var radius = shape.radius + "%";
var x_pos = shape.position[0] + "%";
var y_pos = shape.position[1] + "%";
var clip_path = "circle(" + radius + " at " + x_pos + " " + y_pos + ")";
appendFigure(clip_path, shape);
});
// Setup ellipses
$.each(shape_array.ellipse, function(i, shape) {
type = "ellipse";
var radius_x = shape.radius[0] + "%";
var radius_y = shape.radius[1] + "%";
var x_pos = shape.position[0] + "%";
var y_pos = shape.position[1] + "%";
var clip_path =
"ellipse(" +
radius_x +
" " +
radius_y +
" at " +
x_pos +
" " +
y_pos +
")";
appendFigure(clip_path, shape);
});
type = start_type;
setupDemo(start_coords);
}
function appendFigure(clip_path, shape) {
// Add all the buttons to the .shapes container
// considering using some other element other than figure for buttons to be more semantic...
var webkit = "";
var unprefixed = "clip-path: " + clip_path;
// Disable the element if we are not ready for it to be enabled
if (shape.disabled == true) {
var disabled = 'class="disabled" ';
} else {
var disabled = "";
}
if ($(".webkit.block").hasClass("show")) {
var webkit = "-webkit-clip-path: " + clip_path + ";";
}
if (type == "polygon") {
var fig =
'<figure class="gallery-cell" ' +
disabled +
'data-name="' +
shape.name +
'" data-type="polygon" data-coords="' +
shape.coords.join(" ") +
'">' +
'<div style="' +
webkit +
" " +
" " +
unprefixed +
'" class="shape ' +
shape.name +
'"></div>' +
"<figcaption>" +
shape.name +
"</figcaption>" +
"</figure>";
}
if (type == "custom") {
var fig =
'<figure class="gallery-cell" ' +
disabled +
'data-name="' +
shape.name +
'" data-type="custom" data-coords="' +
shape.coords.join(" ") +
'">' +
'<div style="' +
webkit +
" " +
" " +
unprefixed +
'" class="shape ' +
shape.name +
'"></div>' +
"<figcaption>" +
shape.name +
"</figcaption>" +
"</figure>";
}
if (type == "inset") {
var fig =
'<figure class="gallery-cell" ' +
disabled +
'data-name="' +
shape.name +
'" data-type="inset" data-coords="' +
shape.coords.join(" ") +
'">' +
'<div style="' +
webkit +
" " +
" " +
unprefixed +
'" class="shape ' +
shape.name +
'"></div>' +
"<figcaption>" +
shape.name +
"</figcaption>" +
"</figure>";
}
if (type == "circle") {
var fig =
'<figure class="gallery-cell" ' +
disabled +
'data-name="Circle" data-type="circle">' +
'<div style="' +
webkit +
" " +
" " +
unprefixed +
'" class="shape ' +
shape.name +
'"></div>' +
"<figcaption>" +
shape.name +
"</figcaption>" +
"</figure>";
}
if (type == "ellipse") {
var fig =
'<figure class="gallery-cell" ' +
disabled +
'data-name="Ellipse" data-type="ellipse">' +
'<div style="' +
webkit +
" " +
" " +
unprefixed +
'" class="shape ' +
shape.name +
'"></div>' +
"<figcaption>" +
shape.name +
"</figcaption>" +
"</figure>";
}
console.log("appendFigure();");
$shapes.append(fig);
// Add .on class to the figure we are starting with
$('[data-name="' + start.name + '"]').addClass("on");
// listen for clicks on the figure buttons
$("figure:not(.disabled)")
.unbind()
.click(function() {
$("figure").removeClass("on");
$(this).addClass("on");
type = $(this).attr("data-type");
if (type == "inset") {
var shape = shape_array.inset[0];
start_coords = shape.coords;
setupDemo(shape.coords);
}
if (type == "custom") {
setupDemo();
}
if (type == "circle") {
var shape = shape_array.circle[0];
setupDemo(shape.coords);
}
if (type == "ellipse") {
var shape = shape_array.ellipse[0];
setupDemo(shape.coords);
}
if (type == "polygon") {
new_shape = [];
// Coords at stored with data-coords attribute and turned into array
var coords = $(this)
.attr("data-coords")
.split(" ");
var coords = $.each(coords, function(i, coordinate) {
var coordinate = coordinate.split(",");
new_shape.push(coordinate);
if (i == coords.length - 1) {
start_coords = new_shape;
setupDemo(start_coords);
}
});
}
});
}
function setupDemo(coords) {
console.log("setupDemo();");
clearDemo();
start_flickity();
if (type == "custom") {
// Prepare for customizing
$html.addClass("customizing start-customizing customizing-no-poly");
$handles.empty();
$functions.html('polygon(<span class="function"></span>)');
// Close customization if finish button is clicked
$(".finish").click(function() {
finishCustomizing();
readyDrag();
});
clipIt();
var i = 0;
$demo.click(function(e) {
i++;
// Get where on demo the click is
var offset = $(this).offset();
var x_px = e.pageX - offset.left - 10;
var y_px = e.pageY - offset.top - 10;
// Keep clicks in bounds
if (x_px < 0) {
var x_px = 0;
}
if (x_px > width) {
var x_px = width;
}
if (y_px < 0) {
var y_px = 0;
}
if (y_px > height) {
var y_px = height;
}
// Convert px to % coordinates
var code_x = Math.round((x_px / width) * 100) + "%";
var code_y = Math.round((y_px / height) * 100) + "%";
// Add the handle
$handles.append(
'<div class="handle" data-handle="' +
i +
'" style="top: ' +
y_px +
"px; left: " +
x_px +
'px;"></div>'
);
// Add css point and detect if comma is necessary
var $functions = $(".function", $functions);
if (i > 1) {
// comma
$functions.append(
', <code class="point" data-point="' +
i +
'">' +
code_x +
" " +
code_y +
"</code>"
);
} else {
// no comma
$functions.append(
'<code class="point" data-point="' +
i +
'">' +
code_x +
" " +
code_y +
"</code>"
);
$html.removeClass("start-customizing");
}
if (i > 2) {
// We have at least 3 points and a polygon
// Tell the person the name of the shape they made for fun
var shapes = [
"triangle",
"quadrilateral",
"pentagon",
"hexagon",
"heptagon",
"octagon",
"nonagon",
"decagon",
"hendecagon",
"dodecagon",
"tridecagon",
"tetradecagon",
"pentadecagon",
"hexadecagon",
"heptadecagon",
"octadecagon",
"nonadecagon",
"icosagon",
"icosagon",
"icosikaihenagon",
"icosikaidigon",
"icosikaitrigon",
"icosikaitetragon",
"icosikaipentagon",
"icosikaihexagon",
"icosikaiheptagon",
"icosikaioctagon",
"icosikaienneagon",
"triacontagon",
"triacontakaihenagon",
"triacontakaidigon",
"triacontakaitrigon",
"triacontakaitetragon",
"triacontakaipentagon",
"triacontakaihexagon",
"triacontakaiheptagon",
"triacontakaioctagon",
"triacontakaienneagon",
"tetracontagon",
"tetracontakaihenagon",
"tetracontakaidigon",
"tetracontakaitrigon",
"tetracontakaitetragon",
"tetracontakaipentagon",
"tetracontakaihexagon",
"tetracontakaiheptagon",
"tetracontakaioctagon",
"tetracontakaienneagon",
"pentacontagon",
"pentacontakaihenagon",
"pentacontakaidigon",
"pentacontakaitrigon",
"pentacontakaitetragon",
"pentacontakaipentagon",
"pentacontakaihexagon",
"pentacontakaiheptagon",
"pentacontakaioctagon",
"pentacontakaienneagon",
"hexacontagon",
"hexacontakaihenagon",
"hexacontakaidigon",
"hexacontakaitrigon",
"hexacontakaitetragon",
"hexacontakaipentagon",
"hexacontakaihexagon",
"hexacontakaiheptagon",
"hexacontakaioctagon",
"hexacontakaienneagon",
"heptacontagon",
"heptacontakaihenagon",
"heptacontakaidigon",
"heptacontakaitrigon",
"heptacontakaitetragon",
"heptacontakaipentagon",
"heptacontakaihexagon",
"heptacontakaiheptagon",
"heptacontakaioctagon",
"heptacontakaienneagon",
"octacontagon",
"octacontakaihenagon",
"octacontakaidigon",
"octacontakaitrigon",
"octacontakaitetragon",
"octacontakaipentagon",
"octacontakaihexagon",
"octacontakaiheptagon",
"octacontakaioctagon",
"octacontakaienneagon",
"enneacontagon",
"enneacontakaihenagon",
"enneacontakaidigon",
"enneacontakaitrigon",
"enneacontakaitetragon",
"enneacontakaipentagon",
"enneacontakaihexagon",
"enneacontakaiheptagon",
"enneacontakaioctagon",
"enneacontakaienneagon",
"hectogon"
];
$(".finish").attr("data-shape", shapes[i - 3]);
$html.removeClass("customizing-no-poly");
clipIt();
// End adding new points if first handle is clicked
$('[data-handle="1"]').click(function() {
finishCustomizing();
readyDrag();
});
}
});
} else {
finishCustomizing();
// Run through each coordinate for polygons, circles, ellipses, and inset
$.each(coords, function(i, coord) {
var x = coord[0];
var y = coord[1];
// Add unit to % coordinates
var code_x = x + "%";
var code_y = y + "%";
// Convert % to px coordinates
var x_px = Math.round((x / 100) * width);
var y_px = Math.round((y / 100) * height);
console.log("type: " + type);
// Setup Circle demo
if (type == "circle") {
// Grab preset values
var shape = shape_array.circle[0];
var radius = shape.radius; // For 1:1 ratio
// Adjust radius handle to edge of circle if ratio is not 1:1
if (width !== height) {
var radius_x_px = width * getRadiusModifier();
} else {
var radius_x_px = width;
}
// Setup radius handle
if (i == 0) {
$handles.append(
'<div class="radius handle" data-handle="' +
i +
'" style="top: ' +
y_px +
"px; left: " +
radius_x_px +
'px;"></div>'
);
}
// Setup center position handle
if (i == 1) {
$handles.append(
'<div class="position handle" data-handle="' +
i +
'" style="top: ' +
y_px +
"px; left: " +
x_px +