-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
140 lines (119 loc) · 4.2 KB
/
main.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
const preview = document.getElementById("preview");
const previewContext = preview.getContext("2d");
const outputArea = document.getElementById("output");
const textarea = document.getElementById("textarea");
const inputImage = document.getElementById("input-image");
const generateButton = document.getElementById("generate-button");
const mirrorCheckbox = document.getElementById("mirror-checkbox");
const rightCheckbox = document.getElementById("right-checkbox");
const fontsizeSelect = document.getElementById("fontsize-select");
const topColor = document.getElementById("top-color");
const backgroundColor = document.getElementById("bg-color");
const textColor = document.getElementById("txt-color");
const frameColor = document.getElementById("frame-color");
const image = new Image();
drawPreview();
// ====================
function drawPreview() {
const splitText = textarea.value.split("\n");
const fontsize = Math.floor(fontsizeSelect.value);
previewContext.font = `${fontsize}px ${CANVAS_FONT}`;
const maxTextWidth = Math.floor(Math.max(...splitText.map(r => previewContext.measureText(r).width)));
const textLineSpace = Math.floor(fontsize * 0.2);
const leftOffset = rightCheckbox.checked ?
CANVAS_WIDTH - TAG_AREA_WIDTH - TAG_AREA_POSITION_X * 2 - Math.max(0, maxTextWidth - TAG_AREA_WIDTH + TAG_AREA_POSITION_X):
0;
// 全体クリア
previewContext.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// ベース背景描画
previewContext.fillStyle = topColor.value;
previewContext.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// 画像描画
if (image.src) {
previewContext.drawImage(image, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
}
// 名札背景描画
previewContext.fillStyle = backgroundColor.value;
previewContext.fillRect(
TAG_AREA_POSITION_X + leftOffset,
TAG_AREA_POSITION_Y,
maxTextWidth > TAG_AREA_WIDTH - (TAG_AREA_INNER_PADDING * 2) ? maxTextWidth + (TAG_AREA_INNER_PADDING * 2) : TAG_AREA_WIDTH,
TAG_AREA_POSITION_Y +
TAG_AREA_INNER_PADDING +
(splitText.length - 1) * textLineSpace +
splitText.length * fontsize
);
/*
// 名札枠描画
previewContext.strokeStyle = frameColor.value;
previewContext.strokeRect(
TAG_AREA_POSITION_X + leftOffset,
TAG_AREA_POSITION_Y,
TAG_AREA_WIDTH,
TAG_AREA_POSITION_Y +
TAG_AREA_INNER_PADDING +
(splitText.length - 1) * textLineSpace +
splitText.length * fontsize
);
*/
// テキスト描画
previewContext.fillStyle = textColor.value;
previewContext.font = `${fontsize}px ${CANVAS_FONT}`;
for (let i in splitText) {
previewContext.fillText(
splitText[i],
TAG_AREA_POSITION_X + TAG_AREA_INNER_PADDING + leftOffset,
TAG_AREA_POSITION_Y +
TAG_AREA_INNER_PADDING +
fontsize +
i * (fontsize + textLineSpace)
);
}
}
// ======== イベント =======
textarea.addEventListener("input", () => {
drawPreview();
});
inputImage.addEventListener("change", (e) => {
const file = e.target.files[0];
const r = new FileReader();
r.onload = () => {
image.src = r.result;
image.onload = () => {
drawPreview();
};
};
r.readAsDataURL(file);
});
fontsizeSelect.addEventListener("change", () => {
drawPreview();
});
topColor.addEventListener("change", () => {
drawPreview();
});
backgroundColor.addEventListener("change", () => {
drawPreview();
});
textColor.addEventListener("change", () => {
drawPreview();
});
/*
frameColor.addEventListener("change", () => {
drawPreview();
});
*/
generateButton.addEventListener("click", () => {
const imgData = preview.toDataURL();
outputArea.innerHTML = `
<a href="${imgData}" download="file.png">🙋♀️ ダウンロードする</a>
`;
});
// ======= 高度設定イベント =======
mirrorCheckbox.addEventListener("click", () => {
previewContext.scale(-1, 1);
previewContext.translate(-CANVAS_WIDTH, 0);
drawPreview();
});
rightCheckbox.addEventListener("click", () => {
drawPreview();
});