-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.js
125 lines (125 loc) · 4.03 KB
/
export.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
function OpenExportPanel() {
exportPanel.classList.remove("hidden");
UpdateExportInputs();
setTimeout(() => {
if (!exportPanel.classList.contains("hidden")) {
exportFilename.focus();
exportFilename.select();
}
}, 200);
ToggleTabindexRecursive(exportPanel, true);
}
function CloseExportPanel() {
exportPanel.classList.add("hidden");
ToggleTabindexRecursive(exportPanel, false);
}
function UpdateExportInputs() {
SetCheckboxEnabled(exportTile, useGrid, false);
SetCheckboxEnabled(exportCropAlpha, !exportTile.checked, false);
}
function ExportImage() {
// Alpha cropping
let sizeX = imageSizeX;
let sizeY = imageSizeY;
let startX = 0;
let startY = 0;
if (exportCropAlpha.checked) {
let minX = imageSizeX;
let maxX = 0;
let minY = imageSizeY;
let maxY = 0;
for (let x = 0; x < imageSizeX; x++) {
for (let y = 0; y < imageSizeY; y++) {
if (imageData[x + imageSizeX * y].a > 0.01) {
minX = Math.min(x, minX);
maxX = Math.max(x, maxX);
minY = Math.min(y, minY);
maxY = Math.max(y, maxY);
}
}
}
if (minX <= maxX && minY <= maxY) {
startX = minX;
startY = minY;
sizeX = maxX - minX + 1;
sizeY = maxY - minY + 1;
}
}
let upscaleFactor = parseInt(exportUpscaleInput.value);
if (isNaN(upscaleFactor))
upscaleFactor = 1;
if (upscaleFactor < 1)
upscaleFactor = 1;
// Grid tiles
if (exportTile.checked) {
let i = 0;
for (let x = 0; x <= imageSizeX - gridSizeX; x += gridSizeX) {
for (let y = 0; y <= imageSizeY - gridSizeY; y += gridSizeY) {
if (!IsRectEmpty(x, gridSizeX, y, gridSizeY))
ExportRect(x, gridSizeX, y, gridSizeY, i, upscaleFactor);
i++;
}
}
}
else {
ExportRect(startX, sizeX, startY, sizeY, -1, upscaleFactor);
}
CloseExportPanel();
}
function ExportRect(startX, sizeX, startY, sizeY, number = -1, upscaleFactor = 1) {
let canvas = document.createElement("canvas");
canvas.width = sizeX * upscaleFactor;
canvas.height = sizeY * upscaleFactor;
let ctx = canvas.getContext("2d");
for (let x = 0; x < sizeX; x++) {
for (let y = 0; y < sizeY; y++) {
ctx.fillStyle = imageData[(x + startX) + imageSizeX * (y + startY)].GetHex();
ctx.fillRect(x * upscaleFactor, y * upscaleFactor, upscaleFactor, upscaleFactor);
}
}
let mime = "";
if (exportFormatSelect.value == "0")
mime = "image/png";
if (exportFormatSelect.value == "1")
mime = "image/jpeg";
if (exportFormatSelect.value == "2")
mime = "image/webp";
if (exportFormatSelect.value == "3")
mime = "image/gif";
let url = canvas.toDataURL(mime);
var a = document.createElement('a');
if (number < 0)
a.download = exportFilename.value;
else
a.download = exportFilename.value + "_" + number.toString();
a.href = url;
a.click();
a.remove();
canvas.remove();
}
function IsRectEmpty(startX, sizeX, startY, sizeY) {
for (let x = startX; x < startX + sizeX; x++) {
for (let y = startY; y < startY + sizeY; y++) {
if (imageData[x + y * imageSizeX].a > 0.005)
return false;
}
}
return true;
}
function SetInputEnabled(input, enabled, defaultValue) {
input.readOnly = !enabled;
input.classList.toggle("disabled", !enabled);
input.parentElement?.classList.toggle("disabled", !enabled);
if (!enabled) {
input.value = defaultValue;
}
}
function SetCheckboxEnabled(input, enabled, defaultValue) {
input.disabled = !enabled;
input.classList.toggle("disabled", !enabled);
input.parentElement?.classList.toggle("disabled", !enabled);
if (!enabled) {
input.checked = defaultValue;
}
}
//# sourceMappingURL=export.js.map