UNPKG

@thi.ng/pixel

Version:

Typedarray integer & float pixel buffers w/ customizable formats, blitting, drawing, convolution

34 lines (33 loc) 971 B
import { canvas2d } from "@thi.ng/canvas"; import { isNumber } from "@thi.ng/checks/is-number"; const imageCanvas = (img, width, height = width, parent, opts) => { const ctx = isNumber(width) && isNumber(height) ? canvas2d(width, height, parent, opts) : canvas2d(img.width, img.height, parent, opts); ctx.ctx.drawImage(img, 0, 0, ctx.canvas.width, ctx.canvas.height); return ctx; }; const imageFromURL = async (src, cors = "anonymous") => { const img = new Image(); img.crossOrigin = cors; img.src = src; await img.decode(); return img; }; const imagePromise = imageFromURL; const imageFromFile = (file) => new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = (e) => { const img = new Image(); img.src = e.target.result; img.onload = () => resolve(img); img.onerror = (e2) => reject(e2); }; reader.readAsDataURL(file); }); export { imageCanvas, imageFromFile, imageFromURL, imagePromise };