UNPKG

@ynmstudio/utils

Version:
76 lines (71 loc) 2.8 kB
import { isPlatformBrowser } from '@angular/common'; import * as i0 from '@angular/core'; import { PLATFORM_ID, HostBinding, Input, Inject, Directive } from '@angular/core'; class DrawBlurhash { get width() { return this.WIDTH; } get height() { return this.HEIGHT; } constructor(platformId, canvas) { this.platformId = platformId; this.canvas = canvas; this.WIDTH = 32; this.HEIGHT = 32; } ngAfterViewInit() { this.drawCanvas(); } async drawCanvas() { if (!isPlatformBrowser(this.platformId)) return; try { if (!this.blurhash || !this.canvas) return; if (!this.decode) { const { decode } = await import('blurhash-wasm'); this.decode = decode; } const pixels = this.decode(this.blurhash, this.WIDTH, this.HEIGHT); if (!pixels) return; const asClamped = new Uint8ClampedArray(pixels); const imageData = new ImageData(asClamped, this.WIDTH, this.HEIGHT); const ctx = this.canvas?.nativeElement.getContext('2d'); if (imageData) ctx?.putImageData(imageData, 0, 0); } catch (error) { console.error(error); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.3", ngImport: i0, type: DrawBlurhash, deps: [{ token: PLATFORM_ID }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.1.3", type: DrawBlurhash, isStandalone: true, selector: "[drawBlurhash]", inputs: { blurhash: "blurhash" }, host: { properties: { "attr.width": "this.width", "attr.height": "this.height" } }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.3", ngImport: i0, type: DrawBlurhash, decorators: [{ type: Directive, args: [{ selector: '[drawBlurhash]', standalone: true }] }], ctorParameters: () => [{ type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID] }] }, { type: i0.ElementRef }], propDecorators: { blurhash: [{ type: Input }], width: [{ type: HostBinding, args: ['attr.width'] }], height: [{ type: HostBinding, args: ['attr.height'] }] } }); /* * Public API Surface of draw-blurhash */ /** * Generated bundle index. Do not edit. */ export { DrawBlurhash }; //# sourceMappingURL=ynmstudio-utils-blurhash.mjs.map