|
| 1 | +import { BaseImageResource, Ticker, UPDATE_PRIORITY, settings } from 'pixi.js'; |
| 2 | +import { parseGIF, decompressFrames } from 'gifuct-js'; |
| 3 | + |
| 4 | +export interface GifResourceOptions { |
| 5 | + autoLoad?: boolean; |
| 6 | + autoPlay?: boolean; |
| 7 | + loop?: boolean; |
| 8 | + animationSpeed?: number; |
| 9 | + fps?: number; |
| 10 | +} |
| 11 | + |
| 12 | +interface PrecomputedFrame { |
| 13 | + start: number; |
| 14 | + end: number; |
| 15 | + imageData: ImageData; |
| 16 | +} |
| 17 | + |
| 18 | +const findFrame = (frames: PrecomputedFrame[], time: number) => { |
| 19 | + let low = 0; |
| 20 | + let high = frames.length - 1; |
| 21 | + while (low <= high) { |
| 22 | + const mid = (low + high) >> 1; |
| 23 | + const f = frames[mid]; |
| 24 | + if (time >= f.start && time < f.end) return f; |
| 25 | + if (time < f.start) high = mid - 1; |
| 26 | + else low = mid + 1; |
| 27 | + } |
| 28 | + return undefined; |
| 29 | +}; |
| 30 | + |
| 31 | +export class GifResource extends BaseImageResource { |
| 32 | + public static override test(_src: unknown, ext?: string): boolean { |
| 33 | + return ( |
| 34 | + ext === 'gif' || |
| 35 | + (typeof _src === 'string' && _src.trim().toLowerCase().endsWith('.gif')) || |
| 36 | + (_src instanceof HTMLImageElement && _src.src.toLowerCase().endsWith('.gif')) |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public declare source: HTMLCanvasElement; |
| 41 | + |
| 42 | + public autoPlay: boolean; |
| 43 | + public loop: boolean; |
| 44 | + public animationSpeed: number; |
| 45 | + public readonly fps: number; |
| 46 | + public readonly url: string = ''; |
| 47 | + |
| 48 | + private _frames: PrecomputedFrame[] = []; |
| 49 | + private _currentTime = 0; |
| 50 | + private _playing = false; |
| 51 | + private _loadPromise: Promise<this> | null = null; |
| 52 | + |
| 53 | + public constructor(src: unknown, options: GifResourceOptions = {}) { |
| 54 | + super(document.createElement('canvas')); |
| 55 | + if (typeof src === 'string') this.url = src; |
| 56 | + else if (src instanceof HTMLImageElement) this.url = src.src; |
| 57 | + this.autoPlay = options.autoPlay ?? true; |
| 58 | + this.loop = options.loop ?? true; |
| 59 | + this.animationSpeed = options.animationSpeed ?? 1; |
| 60 | + this.fps = options.fps ?? 30; |
| 61 | + |
| 62 | + if (options.autoLoad !== false) this.load(); |
| 63 | + } |
| 64 | + |
| 65 | + public override async load(): Promise<this> { |
| 66 | + if (this._loadPromise) return this._loadPromise; |
| 67 | + |
| 68 | + this._loadPromise = (async () => { |
| 69 | + const res = await settings.ADAPTER.fetch(this.url); |
| 70 | + const buffer = await res.arrayBuffer(); |
| 71 | + |
| 72 | + if (!buffer?.byteLength) throw new Error('Invalid GIF buffer'); |
| 73 | + |
| 74 | + const gif = parseGIF(buffer); |
| 75 | + const gifFrames = decompressFrames(gif, true); |
| 76 | + if (!gifFrames.length) throw new Error('Invalid GIF file'); |
| 77 | + |
| 78 | + const canvas = document.createElement('canvas'); |
| 79 | + const ctx = canvas.getContext('2d', { willReadFrequently: true })!; |
| 80 | + const patchCanvas = document.createElement('canvas'); |
| 81 | + const patchCtx = patchCanvas.getContext('2d')!; |
| 82 | + |
| 83 | + canvas.width = gif.lsd.width; |
| 84 | + canvas.height = gif.lsd.height; |
| 85 | + |
| 86 | + let time = 0; |
| 87 | + let prevFrame: ImageData | null = null; |
| 88 | + const defaultDelay = 1000 / this.fps; |
| 89 | + |
| 90 | + for (const frame of gifFrames) { |
| 91 | + const { dims, delay = defaultDelay, disposalType = 2, patch } = frame; |
| 92 | + const { width, height, left, top } = dims; |
| 93 | + |
| 94 | + patchCanvas.width = width; |
| 95 | + patchCanvas.height = height; |
| 96 | + const patchData = new ImageData(new Uint8ClampedArray(patch), width, height); |
| 97 | + patchCtx.putImageData(patchData, 0, 0); |
| 98 | + |
| 99 | + ctx.drawImage(patchCanvas, left, top); |
| 100 | + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 101 | + |
| 102 | + this._frames.push({ start: time, end: (time += delay), imageData }); |
| 103 | + |
| 104 | + if (disposalType === 2) ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 105 | + else if (disposalType === 3 && prevFrame) ctx.putImageData(prevFrame, 0, 0); |
| 106 | + |
| 107 | + prevFrame = imageData; |
| 108 | + } |
| 109 | + |
| 110 | + this.source.width = canvas.width; |
| 111 | + this.source.height = canvas.height; |
| 112 | + super.update(); |
| 113 | + |
| 114 | + if (this.autoPlay) this.play(); |
| 115 | + |
| 116 | + return this; |
| 117 | + })(); |
| 118 | + |
| 119 | + return this._loadPromise; |
| 120 | + } |
| 121 | + |
| 122 | + public play(): void { |
| 123 | + if (this._playing) return; |
| 124 | + this._playing = true; |
| 125 | + Ticker.shared.add(this._update, this, UPDATE_PRIORITY.HIGH); |
| 126 | + } |
| 127 | + |
| 128 | + public stop(): void { |
| 129 | + if (!this._playing) return; |
| 130 | + this._playing = false; |
| 131 | + Ticker.shared.remove(this._update, this); |
| 132 | + } |
| 133 | + |
| 134 | + public override dispose(): void { |
| 135 | + this.stop(); |
| 136 | + super.dispose(); |
| 137 | + this._frames = []; |
| 138 | + this._loadPromise = null; |
| 139 | + } |
| 140 | + |
| 141 | + private _update(): void { |
| 142 | + if (!this._playing || !this._frames.length) return; |
| 143 | + |
| 144 | + this._currentTime += Ticker.shared.deltaMS * this.animationSpeed; |
| 145 | + const frame = findFrame(this._frames, this._currentTime); |
| 146 | + |
| 147 | + if (frame) { |
| 148 | + this.source.getContext('2d')!.putImageData(frame.imageData, 0, 0); |
| 149 | + super.update(); |
| 150 | + } |
| 151 | + |
| 152 | + const end = this._frames[this._frames.length - 1].end; |
| 153 | + if (this._currentTime > end) { |
| 154 | + if (this.loop) this._currentTime %= end; |
| 155 | + else this.stop(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments