chore: (작업 중) 앱 공유 이미지 JPEG 를 toBlob 대신 toDataURL 로 인코딩 — 측정용 TMPTRACE 로그와 domToCanvas debug 가 남아 있어 머지 전에 지워야 함
This commit is contained in:
1 parent
c59c2b0f56
commit
9e3e750b6c
1 file changed
+18
-3
@@ -149,9 +149,22 @@ function badge(ctx: CanvasRenderingContext2D, x: number, y: number, n: number) {
|
||||
|
||||
const JPEG_QUALITY = 0.85
|
||||
|
||||
/** 합성한 캔버스를 이미지 파일로. 앱 공유는 처음부터 JPEG 로 만든다 (폰에서 PNG 변환이 10초 넘게 걸리고, 앱이 다시 JPEG 로 줄이던 일도 없어진다) */
|
||||
/**
|
||||
* 합성한 캔버스를 이미지 파일로. 앱 공유는 JPEG 로 만든다.
|
||||
* canvas.toBlob 은 브라우저가 한가한 틈에만 인코딩해서, 화면 애니메이션이 계속 돌면 대기 시간이 지나서야 처리된다
|
||||
* (폰에서 PNG·JPEG 모두 13초). 앱은 바로 인코딩하는 toDataURL 로 만들어 Blob 으로 옮긴다.
|
||||
*/
|
||||
function encode(canvas: HTMLCanvasElement, format: 'png' | 'jpeg'): Promise<Blob> {
|
||||
return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('toBlob failed'))), format === 'jpeg' ? 'image/jpeg' : 'image/png', format === 'jpeg' ? JPEG_QUALITY : undefined))
|
||||
if (format === 'jpeg') {
|
||||
const E0 = performance.now() // TMPTRACE
|
||||
const url = canvas.toDataURL('image/jpeg', JPEG_QUALITY)
|
||||
const bin = atob(url.slice(url.indexOf(',') + 1))
|
||||
const bytes = new Uint8Array(bin.length)
|
||||
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i)
|
||||
console.info('[share-trace] encode toDataURL', Math.round(performance.now() - E0), bytes.length) // TMPTRACE
|
||||
return Promise.resolve(new Blob([bytes], { type: 'image/jpeg' }))
|
||||
}
|
||||
return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('toBlob failed'))), 'image/png'))
|
||||
}
|
||||
|
||||
/** 게시물 슬라이드(최대 2장)를 고른 크기의 이미지 한 장으로 만든다. 기본은 PNG, 앱은 JPEG */
|
||||
@@ -169,8 +182,10 @@ export async function shareImage(slides: HTMLElement[], opts: { size: ShareSize;
|
||||
const css = await fontCss(cards.map((c) => c.textContent ?? '').join(''))
|
||||
const aspect = cards[0].offsetHeight / cards[0].offsetWidth
|
||||
const boxes = z.strip ? place(cards.length, aspect, SIDE, z.h - FOOT) : place(cards.length, aspect, z.subY + 60, z.pillY - z.pillH / 2 - 50)
|
||||
const shots = await Promise.all(cards.map((c, i) => domToCanvas(c, { scale: boxes[i].w / c.offsetWidth, filter: keep, font: css ? { cssText: css } : false, fetchFn: viaProxy })))
|
||||
const D0 = performance.now() // TMPTRACE
|
||||
const shots = await Promise.all(cards.map((c, i) => domToCanvas(c, { scale: boxes[i].w / c.offsetWidth, filter: keep, font: css ? { cssText: css } : false, fetchFn: viaProxy, debug: true /* TMPTRACE */ })))
|
||||
|
||||
console.info('[share-trace] domToCanvas', Math.round(performance.now() - D0), cards.length) // TMPTRACE
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = W
|
||||
canvas.height = z.h
|
||||
|
||||
Reference in new issue
Block a user