-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (107 loc) · 3.3 KB
/
Copy pathmain.js
File metadata and controls
129 lines (107 loc) · 3.3 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
const canvas = document.querySelector("#output");
const preview = document.querySelector("#preview");
const fileInput = document.querySelector("#screenshot");
const centeredInput = document.querySelector("#centered");
const squaredInput = document.querySelector("#squared");
const nameInput = document.querySelector("#name");
function drawClipMap(ctx, width, height) {
ctx.beginPath();
const offset = 0
let left, right, top, bottom;
if (height % 2) {
left = [[1, Math.floor(height / 2)]]
right = [[width - 2, Math.floor(height / 2)]]
} else {
left = [
[offset, height / 2],
[offset, height / 2 - 1]
]
right = [
[width - 4, height / 2 - 1],
[width - 4, height / 2]
]
}
if (width % 2) {
top = [[Math.floor(width / 2), offset]]
bottom = [[Math.floor(width / 2), height - 1 - offset]]
} else {
top = [
[width / 2 - 2, offset],
[width / 2 - 2, offset]
]
bottom = [
[width / 2, height - 1],
[width / 2 - 1, height - 1]
]
}
[...left].forEach(element => {
ctx.moveTo(...element)
});
[...top, ...right, ...bottom, ...left].forEach(element => {
ctx.lineTo(...element)
});
ctx.clip();
}
function drawImage(ctx, img) {
const center = centeredInput.checked
const squared = squaredInput.checked
var height = 0.215*img.height
var width = Math.floor(2*height)
height = Math.floor(height)
var left
if (center) {
left = Math.floor(img.width/2 - width/2)
} else {
left = img.width - width
}
left += 2
canvas.height = height - 1;
canvas.width = squared ? height - 3 : width - 4;
drawClipMap(ctx, squared ? height : width, height);
ctx.drawImage(img,
left, // source x
img.height - height, // source y
width, // source width
height, // source heigth
0, 0, // target x, y
squared ? height : width, // target width
height // target height
);
}
function processFile() {
const reader = new FileReader();
const img = new Image();
const ctx = canvas.getContext("2d");
ctx.globalAlpha = 0.0;
img.addEventListener("load",
() => drawImage(ctx, img)
);
reader.addEventListener(
"load",
() => {
// convert image file to base64 string
preview.src = reader.result;
img.src = reader.result;
},
false,
);
const file = fileInput.files[0]
if (file) {
reader.readAsDataURL(file);
nameInput.placeholder = file.name.substr(0, file.name.lastIndexOf('.')) + '_minimap.png'
}
}
function exportImage() {
const file = fileInput.files[0]
const imgUrl = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.href = imgUrl;
link.download = nameInput.value ?? file.name.substr(0, file.name.lastIndexOf('.')) + '_minimap.png'
link.click();
}
const exportButton = document.querySelector("#export");
exportButton.addEventListener("click", exportImage);
fileInput.addEventListener("change", processFile);
centeredInput.addEventListener("click", processFile);
squaredInput.addEventListener("click", processFile);
processFile();