-
Notifications
You must be signed in to change notification settings - Fork 207
/
card-renderer.js
298 lines (265 loc) · 7.39 KB
/
card-renderer.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
import metaversefile from 'metaversefile';
import {generateStats, types} from './procgen/procgen.js';
import {screenshotObjectApp} from './object-screenshotter.js';
import {screenshotAvatarUrl} from './avatar-screenshotter.js';
import {generateGlyph} from './glyph-generator.js';
import {splitLinesToWidth} from './util.js';
const cardsSvgUrl = `./images/cards-01.svg`;
const _loadSvg = async () => {
const res = await fetch(cardsSvgUrl);
const cardSvgSource = await res.text();
return cardSvgSource;
};
let svgLoadPromise = null;
const _waitForSvgLoad = () => {
if (svgLoadPromise === null) {
svgLoadPromise = _loadSvg();
}
return svgLoadPromise;
};
const _loadFonts = () => Promise.all([
'FuturaLT-Condensed',
'GillSans-CondensedBold',
'FuturaStd-Heavy',
'PlazaITC-Normal',
'MS-Gothic',
'GillSans',
'GillSans-ExtraBoldDisplay',
'FuturaLT-CondensedBold',
'SanvitoPro-Regular',
].map(fontFamily => document.fonts.load(`16px "${fontFamily}"`)))
.catch(err => {
console.warn(err);
});
let fontsLoadPromise = null;
const _waitForFontsLoad = () => {
if (fontsLoadPromise === null) {
fontsLoadPromise = _loadFonts();
}
return fontsLoadPromise;
};
const _getCanvasBlob = canvas => new Promise((resolve, reject) => {
canvas.toBlob(blob => {
resolve(blob);
});
});
const _getBlobDataUrl = async blob => new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.onerror = reject;
fileReader.readAsDataURL(blob);
});
const _getCanvasDataUrl = async canvas => {
const blob = await _getCanvasBlob(canvas);
const url = await _getBlobDataUrl(blob);
return url;
};
/* const _previewImage = (image, width, height) => {
image.style.cssText = `\
position: fixed;
top: 0;
left: 0;
width: ${width}px;
z-index: 100;
`;
// console.log('got image', image);
document.body.appendChild(image);
}; */
export const generateObjectUrlCard = async ({
start_url,
width = 300,
// height = width,
flipY = false,
signal = null,
}) => {
const app = await metaversefile.createAppAsync({
start_url,
});
if (signal?.aborted) throw new Error();
const result = await generateObjectCard({
app,
width,
// height,
flipY,
});
if (signal?.aborted) throw new Error();
return result;
};
export const generateObjectCard = async ({
app,
width = 300,
// height = width,
flipY = false,
}) => {
const stats = generateStats(app.contentId);
const {
name,
description,
contentId,
appType,
} = app;
const url = contentId;
const type = appType;
let objectImage = await screenshotObjectApp({
app,
});
objectImage = await _getCanvasDataUrl(objectImage);
let minterAvatarPreview = await screenshotAvatarUrl({
start_url: `./avatars/scilly_drophunter_v31.7_fuji.vrm`,
});
minterAvatarPreview = await _getCanvasDataUrl(minterAvatarPreview);
let glyphImage = generateGlyph(url);
glyphImage = await _getCanvasDataUrl(glyphImage);
const minterUsername = 'Scillia';
const cardImg = await generateCard({
stats,
width,
name,
description,
url,
type,
objectImage,
minterUsername,
minterAvatarPreview,
glyphImage,
flipY,
});
// _previewImage(cardImg, width, height);
return cardImg;
};
export const generateCard = async ({
stats: spec,
width: cardWidth,
name,
description,
url,
type,
objectImage,
minterUsername,
minterAvatarPreview,
glyphImage,
flipY,
} = {}) => {
description = description || 'A great mystery.';
const cardSvgSource = await _waitForSvgLoad();
await _waitForFontsLoad();
const cardHeight = cardWidth / 2.5 * 3.5;
const svg = document.createElement('svg');
svg.setAttribute('xmlns', `http://www.w3.org/2000/svg`);
svg.setAttribute('width', cardWidth);
svg.setAttribute('height', cardHeight);
svg.innerHTML = cardSvgSource;
{
const el = svg;
// name
{
const nameEl = el.querySelector('#name');
nameEl.innerHTML = name;
}
// type
{
const typeEl = el.querySelector('#type');
typeEl.innerHTML = type.toUpperCase();
}
// illustrator name
{
const illustratorNameEl = el.querySelector('#illustrator-name');
illustratorNameEl.innerHTML = minterUsername;
}
// type icon
for (let i = 0; i < types.length; i++) {
const type = types[i];
const typeEl = el.querySelector('#type-' + type);
typeEl.style.display = type === spec.stats.type ? 'block' : 'none';
}
// stat values
[
'level',
'hp',
'mp',
'atk',
'def',
'mag',
'spr',
'dex',
'lck',
].forEach(statName => {
const statEl = el.querySelector('#' + statName + '-value');
statEl.innerHTML = escape(spec.stats[statName] + '');
});
// main image
{
const mainImageEl = el.querySelector('#main-image');
mainImageEl.setAttribute('xlink:href', objectImage);
}
// illustrator image
{
const illustartorImageEl = el.querySelector('#illustrator-image');
illustartorImageEl.setAttribute('xlink:href', minterAvatarPreview);
}
// url
{
const urlEl = el.querySelector('#url');
urlEl.innerHTML = url;
}
// glyph image
{
const glyphImageEl = el.querySelector('#glyph-image');
glyphImageEl.setAttribute('image-rendering', 'pixelated');
glyphImageEl.setAttribute('xlink:href', glyphImage);
}
{
const descriptionEl = el.querySelector('#description');
document.body.appendChild(svg);
const bbox = descriptionEl.getBBox();
const {width, height} = bbox;
document.body.removeChild(svg);
const font = '12px SanvitoPro-Regular';
let description2 = splitLinesToWidth(description, font, width);
if (description2.length > 2) {
description2 = description2.slice(0, 2);
description2[description2.length - 1] += '…';
}
descriptionEl.innerHTML = description2
.map((l, i) => `<tspan x="0" y="${i * height * 1}">${l}</tspan>`)
.join('');
}
{
const linearGradientName = 'linear-gradient-120';
const stopEls = el.querySelectorAll(`#${linearGradientName} > stop`);
stopEls[1].style.cssText = `stop-color:${spec.art.colors[0]}80`;
stopEls[3].style.cssText = `stop-color:${spec.art.colors[1]}`;
}
}
/* const blob = new Blob([svg.outerHTML], {
type: 'image/svg+xml',
});
const objectUrl = URL.createObjectURL(blob);
return objectUrl; */
const image = await new Promise((accept, reject) => {
const image = document.createElement('img');
image.onload = () => {
accept(image);
cleanup();
};
image.onerror = err => {
reject(err);
cleanup();
};
image.crossOrigin = 'Anonymous';
const blob = new Blob([svg.outerHTML], {
type: 'image/svg+xml',
});
const url = URL.createObjectURL(blob);
image.src = url;
function cleanup() {
URL.revokeObjectURL(url);
}
});
const imageBitmap = await createImageBitmap(image, {
imageOrientation: flipY ? 'flipY' : 'none',
});
return imageBitmap;
};