-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
407 lines (336 loc) · 9.82 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
/*
* Lots of code here is copied 1:1 from actual game files
*
*/
const maxLayer = 4;
/** @enum {string} */
const enumSubShape = {
rect: "rect",
circle: "circle",
star: "star",
windmill: "windmill",
};
/** @enum {string} */
const enumSubShapeToShortcode = {
[enumSubShape.rect]: "R",
[enumSubShape.circle]: "C",
[enumSubShape.star]: "S",
[enumSubShape.windmill]: "W",
};
/** @enum {enumSubShape} */
const enumShortcodeToSubShape = {};
for (const key in enumSubShapeToShortcode) {
enumShortcodeToSubShape[enumSubShapeToShortcode[key]] = key;
}
const arrayQuadrantIndexToOffset = [
{ x: 1, y: -1 }, // tr
{ x: 1, y: 1 }, // br
{ x: -1, y: 1 }, // bl
{ x: -1, y: -1 }, // tl
];
// From colors.js
/** @enum {string} */
const enumColors = {
red: "red",
green: "green",
blue: "blue",
yellow: "yellow",
purple: "purple",
cyan: "cyan",
white: "white",
uncolored: "uncolored",
};
/** @enum {string} */
const enumColorToShortcode = {
[enumColors.red]: "r",
[enumColors.green]: "g",
[enumColors.blue]: "b",
[enumColors.yellow]: "y",
[enumColors.purple]: "p",
[enumColors.cyan]: "c",
[enumColors.white]: "w",
[enumColors.uncolored]: "u",
};
/** @enum {string} */
const enumColorsToHexCode = {
[enumColors.red]: "#ff666a",
[enumColors.green]: "#78ff66",
[enumColors.blue]: "#66a7ff",
// red + green
[enumColors.yellow]: "#fcf52a",
// red + blue
[enumColors.purple]: "#dd66ff",
// blue + green
[enumColors.cyan]: "#87fff5",
// blue + green + red
[enumColors.white]: "#ffffff",
[enumColors.uncolored]: "#aaaaaa",
};
/** @enum {enumColors} */
const enumShortcodeToColor = {};
for (const key in enumColorToShortcode) {
enumShortcodeToColor[enumColorToShortcode[key]] = key;
}
CanvasRenderingContext2D.prototype.beginCircle = function (x, y, r) {
if (r < 0.05) {
this.beginPath();
this.rect(x, y, 1, 1);
return;
}
this.beginPath();
this.arc(x, y, r, 0, 2.0 * Math.PI);
};
const possibleShapesString = Object.keys(enumShortcodeToSubShape).join("");
const possibleColorsString = Object.keys(enumShortcodeToColor).join("");
const layerRegex = new RegExp(
"([" + possibleShapesString + "][" + possibleColorsString + "]|-{2}){4}"
);
/////////////////////////////////////////////////////
function radians(degrees) {
return (degrees * Math.PI) / 180.0;
}
/**
* Generates the definition from the given short key
*/
function fromShortKey(key) {
const sourceLayers = key.split(":");
if (sourceLayers.length > maxLayer) {
throw new Error("Only " + maxLayer + " layers allowed");
}
let layers = [];
for (let i = 0; i < sourceLayers.length; ++i) {
const text = sourceLayers[i];
if (text.length !== 8) {
throw new Error("Invalid layer: '" + text + "' -> must be 8 characters");
}
if (text === "--".repeat(4)) {
throw new Error("Empty layers are not allowed");
}
if (!layerRegex.test(text)) {
throw new Error("Invalid syntax in layer " + (i + 1));
}
const quads = [null, null, null, null];
for (let quad = 0; quad < 4; ++quad) {
const shapeText = text[quad * 2 + 0];
const subShape = enumShortcodeToSubShape[shapeText];
const color = enumShortcodeToColor[text[quad * 2 + 1]];
if (subShape) {
if (!color) {
throw new Error("Invalid shape color key: " + key);
}
quads[quad] = {
subShape,
color,
};
} else if (shapeText !== "-") {
throw new Error("Invalid shape key: " + shapeText);
}
}
layers.push(quads);
}
return layers;
}
function renderShape(layers) {
const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById(
"result"
));
const context = canvas.getContext("2d");
context.save();
context.fillStyle = "#fff";
const w = 512;
const h = 512;
const dpi = 1;
context.fillRect(0, 0, w, h);
context.translate((w * dpi) / 2, (h * dpi) / 2);
context.scale((dpi * w) / 28, (dpi * h) / 28);
context.fillStyle = "#e9ecf7";
const quadrantSize = 10;
const quadrantHalfSize = quadrantSize / 2;
context.fillStyle = "rgba(40, 50, 65, 0.1)";
context.beginCircle(0, 0, quadrantSize * 1.15);
context.fill();
for (let layerIndex = 0; layerIndex < layers.length; ++layerIndex) {
const quadrants = layers[layerIndex];
const layerScale = Math.max(0.1, 0.9 - layerIndex * 0.22);
for (let quadrantIndex = 0; quadrantIndex < 4; ++quadrantIndex) {
if (!quadrants[quadrantIndex]) {
continue;
}
const { subShape, color } = quadrants[quadrantIndex];
const quadrantPos = arrayQuadrantIndexToOffset[quadrantIndex];
const centerQuadrantX = quadrantPos.x * quadrantHalfSize;
const centerQuadrantY = quadrantPos.y * quadrantHalfSize;
const rotation = radians(quadrantIndex * 90);
context.translate(centerQuadrantX, centerQuadrantY);
context.rotate(rotation);
context.fillStyle = enumColorsToHexCode[color];
context.strokeStyle = "#555";
context.lineWidth = 1;
const insetPadding = 0.0;
switch (subShape) {
case enumSubShape.rect: {
context.beginPath();
const dims = quadrantSize * layerScale;
context.rect(
insetPadding + -quadrantHalfSize,
-insetPadding + quadrantHalfSize - dims,
dims,
dims
);
break;
}
case enumSubShape.star: {
context.beginPath();
const dims = quadrantSize * layerScale;
let originX = insetPadding - quadrantHalfSize;
let originY = -insetPadding + quadrantHalfSize - dims;
const moveInwards = dims * 0.4;
context.moveTo(originX, originY + moveInwards);
context.lineTo(originX + dims, originY);
context.lineTo(originX + dims - moveInwards, originY + dims);
context.lineTo(originX, originY + dims);
context.closePath();
break;
}
case enumSubShape.windmill: {
context.beginPath();
const dims = quadrantSize * layerScale;
let originX = insetPadding - quadrantHalfSize;
let originY = -insetPadding + quadrantHalfSize - dims;
const moveInwards = dims * 0.4;
context.moveTo(originX, originY + moveInwards);
context.lineTo(originX + dims, originY);
context.lineTo(originX + dims, originY + dims);
context.lineTo(originX, originY + dims);
context.closePath();
break;
}
case enumSubShape.circle: {
context.beginPath();
context.moveTo(
insetPadding + -quadrantHalfSize,
-insetPadding + quadrantHalfSize
);
context.arc(
insetPadding + -quadrantHalfSize,
-insetPadding + quadrantHalfSize,
quadrantSize * layerScale,
-Math.PI * 0.5,
0
);
context.closePath();
break;
}
default: {
assertAlways(false, "Unkown sub shape: " + subShape);
}
}
context.fill();
context.stroke();
context.rotate(-rotation);
context.translate(-centerQuadrantX, -centerQuadrantY);
}
}
context.restore();
}
/////////////////////////////////////////////////////
function showError(msg) {
const errorDiv = document.getElementById("error");
errorDiv.classList.toggle("hasError", !!msg);
if (msg) {
errorDiv.innerText = msg;
} else {
errorDiv.innerText = "Shape generated";
}
}
// @ts-ignore
window.generate = () => {
showError(null);
// @ts-ignore
const code = document.getElementById("code").value.trim();
let parsed = null;
try {
parsed = fromShortKey(code);
} catch (ex) {
showError(ex);
return;
}
renderShape(parsed);
};
// @ts-ignore
window.debounce = (fn) => {
setTimeout(fn, 0);
};
// @ts-ignore
window.addEventListener("load", () => {
if (window.location.search) {
var key = window.location.search.substr(1);
if (key.indexOf(".") >= 0) {
key = key.replace(/\./gi, ":");
}
document.getElementById("code").value = key;
}
generate();
});
window.exportShape = () => {
const canvas = document.getElementById("result");
const imageURL = canvas.toDataURL("image/png");
const dummyLink = document.createElement("a");
dummyLink.download = "shape.png";
dummyLink.href = imageURL;
dummyLink.dataset.downloadurl = [
"image/png",
dummyLink.download,
dummyLink.href,
].join(":");
document.body.appendChild(dummyLink);
dummyLink.click();
document.body.removeChild(dummyLink);
};
window.viewShape = (key) => {
document.getElementById("code").value = key;
generate();
};
window.shareShape = () => {
const code = document.getElementById("code").value.trim();
const url = "https://viewer.shapez.io?" + code.replace(/:/gi, ".");
alert("You can share this url: " + url);
};
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function getRandomShape() {
let shapes = Object.values(enumSubShapeToShortcode);
shapes.push("-");
return shapes[getRandomInt(shapes.length)];
}
function getRandomColor() {
return Object.values(enumColorToShortcode)[
getRandomInt(Object.keys(enumColorToShortcode).length)
];
}
window.randomShape = () => {
let layers = getRandomInt(maxLayer);
let code = "";
for (var i = 0; i <= layers; i++) {
let layertext = "";
for (var y = 0; y <= 3; y++) {
let randomShape = getRandomShape();
let randomColor = getRandomColor();
if (randomShape === "-") {
randomColor = "-";
console.log("in");
}
layertext = layertext + randomShape + randomColor;
}
//empty layer not allowed
if (layertext === "--------") {
i--;
} else {
code = code + layertext + ":";
}
}
code = code.replace(/:+$/, "");
document.getElementById("code").value = code;
generate();
};