-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_backend.js
More file actions
161 lines (141 loc) · 4.08 KB
/
javascript_backend.js
File metadata and controls
161 lines (141 loc) · 4.08 KB
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
// @script_author Simon
// treating bodies as random bodies requires colors.js at execution-time
let markerGenSettings = [
// background color:
[
// treat values as random body, instead of a random list
false,
// values
["#222"]
],
// fill of the triangles
[
// treat values as random body, instead of a random list
true,
// values
[
"#fff",
"#aaa"
],
],
// fill of the font
[
// treat values as random body, instead of a random list
true,
// values
[
"#4a4",
"#573",
"#375"
],
]
];
function markerGen_Sample() {
// requires colors.js
let test = document.getElementById('test');
let img = document.createElement('img');
generateMarker(img, '', 1000, function () {});
test.appendChild(img);
}
let side;
function generateMarker(img, text, sideIn, callback) {
side = sideIn;
// generate Marker
let canvas = document.createElement('canvas');
canvas.setAttribute("width",side);
canvas.setAttribute("height",side);
if (canvas.getContext) {
let ctx = canvas.getContext("2d");
background(ctx);
fillMarker(ctx);
printCentredText(ctx,text);
// put logo br
let logo02 = new Image();
let offset = 0.85 * side;
logo02.onload = function () {
ctx.drawImage(logo02, offset, offset);
img.src = canvas.toDataURL("image/jpeg", 0.95);
callback();
};
logo02.src = "img/logo02.small.png";
}
return canvas;
}
function printCentredText(ctx,text) {
ctx.font = "300px sans-serif";
if (markerGenSettings[2][0]) {
// get random color between
ctx.fillStyle = colors_GetRandomColorBetween(markerGenSettings[2][1]);
} else {
// get a random entry from the provided values
ctx.fillStyle = markerGenSettings[2][1]
[Math.floor(Math.random() * markerGenSettings[0][1].length)];
}
ctx.textAlign = "center";
ctx.fillText(text, side / 2, side / 2 + 50);
ctx.strokeText(text, side / 2, side / 2 + 50);
}
function background(context) {
if (markerGenSettings[0][0]) {
// get random color between
context.fillStyle = colors_GetRandomColorBetween(markerGenSettings[0][1]);
} else {
// get a random entry from the provided values
context.fillStyle = markerGenSettings[0][1]
[Math.floor(Math.random() * markerGenSettings[0][1].length)];
}
context.fillRect(0,0,side,side);
}
function fillMarker(context) {
// marker triangles
for (let i = 1; i < side; i += 3) {
let x1,x2,x3,y1,y2,y3;
// Get a start point
x1 = Math.random() * side;
y1 = Math.random() * side;
// Get 2 x and y coordinates, which aren't too far away and do not overlap the border
x2 = moarPts(x1,i);
x3 = moarPts(x1,i);
y2 = moarPts(y1,i);
y3 = moarPts(y1,i);
addTriangle(context,x1,y1,x2,y2,x3,y3);
}
}
function moarPts(z,progress) {
let val;
let invalid;
do {
val = calcPtVal(z,progress);
invalid =
val > side
|| val < 0;
} while (invalid);
return val;
}
function calcPtVal(z,progress) {
return z + randomPosNeg() * maxD(progress);
}
function maxD(progress) {
return 40 * side / progress+50;
}
function randomPosNeg() {
return (Math.random() * 2) - 1;
}
function addTriangle(context,x1,y1,x2,y2,x3,y3) {
context.beginPath();
context.moveTo(x1,y1);
context.lineTo(x2,y2);
context.lineTo(x3,y3);
context.closePath();
if (markerGenSettings[1][0]) {
// get random color between
context.fillStyle = colors_GetRandomColorBetween(markerGenSettings[1][1]);
} else {
// get a random entry from the provided values
context.fillStyle = markerGenSettings[1][1]
[Math.floor(Math.random() * markerGenSettings[1][1].length)];
}
context.fill();
context.lineWidth = 5;
context.stroke();
}