UNPKG

expo-image-manipulator

Version:

Provides functions that let you manipulation images on the local file system, eg: resize, crop.

104 lines (102 loc) • 3.87 kB
import { CodedError, SharedObject } from 'expo'; import ImageManipulatorImageRef from './ImageManipulatorImageRef.web'; import { crop, extent, flip, resize, rotate } from './actions/index.web'; import { releaseCanvas } from './utils.web'; export default class ImageManipulatorContext extends SharedObject { loader; isReleased = false; _currentTask; get currentTask() { this.ensureNotReleased(); if (this._currentTask) { return this._currentTask; } this._currentTask = new Promise((resolve)=>resolve(this.loader())); return this._currentTask; } set currentTask(task) { this.ensureNotReleased(); this._currentTask = task; } constructor(loader){ super(); this.loader = loader ?? (()=>document.createElement('canvas')); } resize(size) { return this.addTask((canvas)=>resize(canvas, size)); } rotate(degrees) { return this.addTask((canvas)=>rotate(canvas, degrees)); } flip(flipType) { return this.addTask((canvas)=>flip(canvas, flipType)); } crop(rect) { return this.addTask((canvas)=>crop(canvas, rect)); } extent(options) { return this.addTask((canvas)=>extent(canvas, options)); } reset() { this.ensureNotReleased(); const previousTask = this._currentTask; this.currentTask = new Promise((resolve)=>resolve(this.loader())); this.releaseTask(previousTask); return this; } release() { if (this.isReleased) { return; } this.isReleased = true; this.releaseTask(this._currentTask); this._currentTask = undefined; this.loader = undefined; super.release(); } async renderAsync() { const canvas = await this.currentTask; // We're copying the canvas so ref's `saveAsync` can safely use `toBlob` again with the desired format and quality. // The original canvas cannot be reused as the manipulator context may still draw on it. const clonedCanvas = document.createElement('canvas'); const clonedCanvasCtx = clonedCanvas.getContext('2d'); clonedCanvas.width = canvas.width; clonedCanvas.height = canvas.height; clonedCanvasCtx?.drawImage(canvas, 0, 0); return new Promise((resolve)=>{ // Encode the clone, which remains valid if the context is released or reset. clonedCanvas.toBlob((blob)=>{ const url = blob ? URL.createObjectURL(blob) : clonedCanvas.toDataURL(); const image = new ImageManipulatorImageRef(url, clonedCanvas); resolve(image); }, // Use PNG format so the result is of the best quality. // If you need another format, see `saveAsync` function on the image ref. 'image/png'); }); } addTask(task) { this.currentTask = this.currentTask.then(async (canvas)=>{ try { const result = await task(canvas); if (result !== canvas) { releaseCanvas(canvas); } return result; } catch (error) { releaseCanvas(canvas); throw error; } }); return this; } ensureNotReleased() { if (this.isReleased) { throw new CodedError('ERR_IMAGE_MANIPULATOR_RELEASED', 'This image manipulation context was released by release() or by useImageManipulator when its source changed or its component unmounted. Create a new context with ImageManipulator.manipulate(...).'); } } releaseTask(task) { // Failed tasks clean up their own canvases. task?.then(releaseCanvas, ()=>{}); } } //# sourceMappingURL=ImageManipulatorContext.web.js.map