Skip to content

Commit

Permalink
Reuse context in resizeBlobImage
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Dec 5, 2023
1 parent 6a74513 commit 5a98dc9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {vec2} from 'linearly'

let canvas: HTMLCanvasElement | null = null
let ctx: CanvasRenderingContext2D | null = null

export async function resizeBlobImage(blob: Blob, size: vec2) {
if (!canvas) {
Expand All @@ -12,8 +13,11 @@ export async function resizeBlobImage(blob: Blob, size: vec2) {
canvas.width = width
canvas.height = height

const ctx = canvas.getContext('2d')!
ctx.drawImage(await createImageBitmap(blob), 0, 0, width, height)
ctx = canvas.getContext('2d')!

const bitmap = await createImageBitmap(blob)
ctx.drawImage(bitmap, 0, 0, width, height)
bitmap.close()

return await new Promise<Blob>((resolve, reject) => {
canvas!.toBlob(b => (b ? resolve(b) : reject()), 'image/jpeg', 0.9)
Expand Down

0 comments on commit 5a98dc9

Please sign in to comment.