-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
294 lines (264 loc) · 6.88 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
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
'use strict';
(function() {
var storageKey = "codeprep_bookcover_builder";
var samples = [{
"background": [{
"rect": "0, 0, 300, 290",
"color": "#FFF"
},
{
"rect": "0, 290, 300, 110",
"color": "#48ACF0"
}
],
"text": [{
"color": "#4e504f",
"font": "37px HiraKakuPro-W6",
"pos": "60, 140",
"text": "はじめての"
},
{
"color": "#48ACF0",
"font": "bold 68px HiraKakuProN-W6",
"pos": "45, 210",
"text": "HTML"
},
{
"color": "#FFF",
"font": "20px HiraKakuProN-W3",
"pos": "20, 330",
"text": "実際にコーディングしながら"
},
{
"color": "#FFF",
"font": "20px HiraKakuProN-W3",
"pos": "45, 360",
"text": "HTMLの基礎を学べる!!"
}
]
}, {
"background": [{
"rect": "0, 0, 300, 300",
"color": "#30DACC"
},
{
"rect": "0, 290, 300, 110",
"color": "#176D60"
}
],
"text": [{
"color": "#FFF",
"font": "56px HiraKakuProN-W6",
"pos": "32, 160",
"text": "CSSの色"
},
{
"color": "#FFF",
"font": "28px HiraKakuProN-W3",
"pos": "125, 210",
"text": "を理解する"
},
{
"color": "#FFF",
"font": "24px HiraKakuProN-W3",
"pos": "30, 355",
"text": "CSS色の理論を学ぼう!"
}
]
}, {
"background": [{
"rect": "0, 0, 300, 290",
"color": "#49C6E5"
},
{
"rect": "25, 220, 245, 1",
"color": "#fff"
},
{
"rect": "25, 100, 245, 1",
"color": "#fff"
},
{
"rect": "0, 290, 300, 110",
"color": "#1164B4"
}
],
"text": [{
"color": "#FFF",
"font": "27px HiraKakuProN-W3",
"pos": "25, 140",
"text": "HTMLとCSSで作る"
},
{
"color": "#FFF",
"font": "56px HiraKakuProN-W6",
"pos": "25, 200",
"text": "Web名刺"
},
{
"color": "#FFF",
"font": "20px HiraKakuProN-W3",
"pos": "10, 340",
"text": "自分だけのオリジナルの名刺を"
},
{
"color": "#FFF",
"font": "20px HiraKakuProN-W3",
"pos": "80, 365",
"text": "作ってみよう!"
}
]
}];
var canvas, ctx, subCanvas, subCtx;
function parsePos(str) {
var array = str.split(",");
return {
x: parseInt(array[0].trim(), 10),
y: parseInt(array[1].trim(), 10)
};
}
function parseRect(str) {
var array = str.split(",");
return {
x: parseInt(array[0].trim(), 10),
y: parseInt(array[1].trim(), 10),
w: parseInt(array[2].trim(), 10),
h: parseInt(array[3].trim(), 10)
};
}
function toggleDummyCanvas() {
subCanvas = document.getElementById('sub-canvas');
if (subCanvas.classList.contains("hide")) {
drawAditionalline();
subCanvas.classList.remove("hide");
} else {
clearAditionalline();
subCanvas.classList.add("hide");
}
}
function clearAditionalline() {
var h = subCanvas.height;
var w = subCanvas.width;
subCtx.clearRect(0, 0, w, h);
}
function drawAditionalline() {
var h = subCanvas.height;
var w = subCanvas.width;
subCtx.beginPath();
[25, 50, 75, 225, 250, 275].map(x => {
subCtx.moveTo(x, 0);
subCtx.lineTo(x, h);
});
[45, 75, 105, 185, 215, 245, 300, 315, 330, 360, 375, 390].map(y => {
subCtx.moveTo(0, y);
subCtx.lineTo(w, y);
});
subCtx.strokeStyle = 'rgba(55, 55, 55, 0.5)';
subCtx.strokeWidth = 1;
subCtx.stroke();
// 中心線
subCtx.beginPath();
subCtx.moveTo(w / 2, 0);
subCtx.lineTo(w / 2, h);
subCtx.moveTo(0, 145);
subCtx.lineTo(w, 145);
subCtx.moveTo(0, 345);
subCtx.lineTo(w, 345);
subCtx.strokeStyle = 'rgba(221, 81, 76, 0.8)';
subCtx.stroke();
}
function drawBackground(data) {
var r = parseRect(data.rect);
ctx.fillStyle = data.color;
ctx.fillRect(r.x, r.y, r.w, r.h);
}
function drawText(data) {
var pos = parsePos(data.pos);
ctx.fillStyle = data.color;
ctx.font = data.font;
ctx.fillText(data.text, pos.x, pos.y);
}
function drawImage(data) {
if (!data.rect) {
return;
}
var image = new Image();
// Avoid CORS issue with image
// http://stackoverflow.com/a/20424457/1855830
image.setAttribute('crossOrigin', 'anonymous');
image.src = data.src;
var rect = parseRect(data.rect);
ctx.save();
if(data.opacity >= 0) {
ctx.globalAlpha = data.opacity;
}
if (data.capture) {
var cap = parseRect(data.capture);
ctx.drawImage(image, cap.x, cap.y, cap.w, cap.h, rect.x, rect.y, rect.w, rect.h);
} else {
ctx.drawImage(image, rect.x, rect.y, rect.w, rect.h);
}
ctx.restore();
}
function draw(data) {
var background = data.background || [];
background.forEach(v => drawBackground(v));
var image = data.images || [];
image.forEach(v => drawImage(v));
var text = data.text || [];
text.forEach(v => drawText(v));
}
function downloadImage() {
this.href = canvas.toDataURL();
}
function getJson() {
var text = document.getElementById('text').value;
return JSON.parse(text);
}
function generate() {
ctx.clearRect(0, 0, 300, 400);
try {
var json = getJson();
draw(json);
} catch (e) {
alert("Can not parse JSON");
}
}
function sample() {
var index = parseInt(this.dataset.index);
var text = document.getElementById('text');
text.value = JSON.stringify(samples[index], null, 2);
generate();
}
window.addEventListener("load", function() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
subCanvas = document.getElementById('sub-canvas');
subCtx = subCanvas.getContext('2d');
document.getElementById("download").addEventListener("click", downloadImage);
var generateBtn = document.getElementById("generate");
generateBtn.addEventListener("click", generate);
[0, 1, 2].forEach(function(i) {
var el = document.getElementById("sample" + (i + 1));
el.addEventListener("click", sample);
});
var text = document.getElementById('text');
text.addEventListener("input", function(e) {
localStorage.setItem(storageKey, e.target.value);
ctx.clearRect(0, 0, 300, 400);
try {
var json = getJson();
draw(json);
} catch (e) {
}
});
var storedBookJson = localStorage.getItem(storageKey)
if (storedBookJson) {
text.value = storedBookJson;
generateBtn.click();
} else {
document.getElementById("sample1").click();
}
document.getElementById("additional-line").addEventListener("change", toggleDummyCanvas);
});
})();