Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
32 lines
803 B
Plaintext
32 lines
803 B
Plaintext
import { PixelCrop } from '..'
|
|
import { canvasPreview } from './canvasPreview'
|
|
|
|
let previewUrl = ''
|
|
|
|
function toBlob(canvas: HTMLCanvasElement): Promise<Blob | null> {
|
|
return new Promise(resolve => {
|
|
canvas.toBlob(resolve)
|
|
})
|
|
}
|
|
|
|
// Returns an image source you should set to state and pass
|
|
// `{previewSrc && <img alt="Crop preview" src={previewSrc} />}`
|
|
export async function imgPreview(image: HTMLImageElement, crop: PixelCrop, scale = 1, rotate = 0) {
|
|
const canvas = document.createElement('canvas')
|
|
canvasPreview(image, canvas, crop, scale, rotate)
|
|
|
|
const blob = await toBlob(canvas)
|
|
|
|
if (!blob) {
|
|
console.error('Failed to create blob')
|
|
return ''
|
|
}
|
|
|
|
if (previewUrl) {
|
|
URL.revokeObjectURL(previewUrl)
|
|
}
|
|
|
|
previewUrl = URL.createObjectURL(blob)
|
|
return previewUrl
|
|
}
|