-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
125 lines (123 loc) · 3.34 KB
/
script.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
var s = new XMLSerializer();
function getcolours() {
var colours = [];
$(".colour").each(function () {
colours.push($(this).val());
});
return colours;
}
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function generate() {
x_colors = shuffle(getcolours());
if (x_colors.length == 0) {
x_colors = "random";
} else if (x_colors.length == 1) {
x_colors.push(x_colors[0]);
}
var pattern = Trianglify({
width: parseInt($("#width").val()),
height: parseInt($("#height").val()),
cell_size: parseInt($("#cell").val()),
variance: parseFloat($("#variance").val()),
x_colors: x_colors,
});
$("canvas").last().replaceWith(pattern.canvas());
$(".download-links").replaceWith(
"<div class='download-links'><a href='data:image/svg+xml;utf-8," +
s.serializeToString(pattern.svg()) +
"' download='triangles'>svg</a><a href='" +
pattern.png() +
"' download='triangles'>png</a></div>"
);
}
$(function () {
var clipboard = new ClipboardJS(".copy", {
text: function () {
return window.location.href;
},
});
clipboard.on("success", function (e) {
$(".copy").text("Copied!");
setTimeout(function () {
$(".copy").text("Copy Link");
}, 3000);
});
clipboard.on("error", function (e) {
prompt("Copy this to your clipboard.", window.location.href);
$(".copy").text("Copied!");
setTimeout(function () {
$(".copy").text("Copy Link");
}, 3000);
});
generate();
$("#form").change(generate);
$("#regenerate").click(function (e) {
e.preventDefault();
generate();
});
$("#addcolour").click(function (e) {
e.preventDefault();
$(".colours").append(
"<div class='colourinput'><input type='color' class='colour' value='" +
$(".colour").last().val() +
"'><button class='x'>X</button></div>"
);
generate();
$(".x").click(function (e) {
e.preventDefault();
$(this).parent().remove();
generate();
});
});
$("#addrandomcolour").click(function (e) {
e.preventDefault();
$(".colours").append(
"<div class='colourinput'><input type='color' class='colour' value='#" +
Math.floor(Math.random() * 16777215).toString(16) +
"'><button class='x'>X</button></div>"
);
generate();
$(".x").click(function (e) {
e.preventDefault();
$(this).parent().remove();
generate();
});
});
$("#removeallcolour").click(function (e) {
e.preventDefault();
$(".colours").empty();
generate();
});
$("#exportcolour").click(function (e) {
e.preventDefault();
prompt("Copy this to your clipboard.", getcolours().toString());
});
$("#importcolour").click(function (e) {
e.preventDefault();
inputcolours = prompt(
'Paste colours seperated by commas. Ex: "#cc2e5a,#9dead8" This will add onto the current colours.'
).split(",");
inputcolours.forEach((colour) =>
$(".colours").append(
"<div class='colourinput'><input type='color' class='colour' value='#" +
colour.replace("#", "") +
"'><button class='x'>x</button></div>"
)
);
generate();
});
$(".x").click(function (e) {
e.preventDefault();
$(this).parent().remove();
generate();
});
});