-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: supports decode animated webp * test: unit tests for decodeAnimation * doc: perf readme
- Loading branch information
Showing
22 changed files
with
365 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ on: | |
|
||
jobs: | ||
release: | ||
if: false | ||
runs-on: ubuntu-latest | ||
environment: release | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<script setup lang="ts"> | ||
import { ref, onMounted } from 'vue' | ||
import { decoderVersion, decodeAnimation } from '../src/' | ||
const canvasRef = ref<HTMLCanvasElement | null>(null) | ||
let isUploaded = false | ||
let webpData: Uint8Array | null = null | ||
let timeoutId = 0 | ||
const onChange = (event) => { | ||
const files = (event.target as HTMLInputElement).files | ||
if (!files?.length) { | ||
return | ||
} | ||
isUploaded = true | ||
const file = files[0] | ||
const fr = new FileReader() | ||
fr.onload = () => { | ||
if (!fr.result) { | ||
return | ||
} | ||
webpData = fr.result as Uint8Array | ||
} | ||
fr.readAsArrayBuffer(file) | ||
} | ||
const drawWebp = async () => { | ||
if (!isUploaded) { | ||
alert('Please upload one webp image') | ||
return | ||
} | ||
window.clearTimeout(timeoutId) | ||
const canvas = canvasRef.value | ||
if (!canvas || !webpData) { | ||
return | ||
} | ||
const frames = await decodeAnimation(webpData, true) | ||
if (!frames?.length) { | ||
return | ||
} | ||
const { width, height } = frames[0] | ||
canvas.style.width = `${width}px` | ||
canvas.style.height = `${height}px` | ||
canvas.width = width | ||
canvas.height = height | ||
const ctx = canvas.getContext('2d')! | ||
const loopFrame = (i: number) => { | ||
const frame = frames[i] | ||
ctx.putImageData(new ImageData(new Uint8ClampedArray(frame.data), frame.width), 0, 0) | ||
timeoutId = window.setTimeout(() => { | ||
loopFrame((i + 1) % frames.length) | ||
}, frame.duration) | ||
} | ||
loopFrame(0) | ||
} | ||
onMounted(async () => { | ||
console.log('decoder version is', await decoderVersion()) | ||
}) | ||
onMounted(() => { | ||
webpData = null | ||
window.clearTimeout(timeoutId) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<input type="file" @change="onChange" accept=".webp" /> | ||
<button @click="drawWebp">Decode</button> | ||
<canvas ref="canvasRef" /> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
canvas { | ||
display: block; | ||
} | ||
button { | ||
margin-top: 12px; | ||
display: block; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
import type { WebPConfig, Nullable, WebPAnimationFrame, WebPDecodedImageData } from './types' | ||
import type { | ||
WebPConfig, | ||
Nullable, | ||
WebPAnimationFrame, | ||
WebPDecodedImageData, | ||
DecodedWebPAnimationFrame, | ||
} from './types' | ||
export declare const encoderVersion: () => Promise<string> | ||
export declare const encodeRGB: ( | ||
rgb: Uint8Array, | ||
width: number, | ||
height: number, | ||
quality?: number | ||
rgb: Uint8Array, | ||
width: number, | ||
height: number, | ||
quality?: number | ||
) => Promise<Nullable<Uint8Array>> | ||
export declare const encodeRGBA: ( | ||
rgba: Uint8Array, | ||
width: number, | ||
height: number, | ||
quality?: number | ||
rgba: Uint8Array, | ||
width: number, | ||
height: number, | ||
quality?: number | ||
) => Promise<Nullable<Uint8Array>> | ||
export declare const encode: ( | ||
data: Uint8Array, | ||
width: number, | ||
height: number, | ||
hasAlpha: boolean, | ||
config: Partial<WebPConfig> | ||
data: Uint8Array, | ||
width: number, | ||
height: number, | ||
hasAlpha: boolean, | ||
config: Partial<WebPConfig> | ||
) => Promise<Nullable<Uint8Array>> | ||
export declare const encodeAnimation: ( | ||
width: number, | ||
height: number, | ||
hasAlpha: boolean, | ||
frames: WebPAnimationFrame[] | ||
width: number, | ||
height: number, | ||
hasAlpha: boolean, | ||
frames: WebPAnimationFrame[] | ||
) => Promise<Nullable<Uint8Array>> | ||
export declare const decoderVersion: () => Promise<string> | ||
export declare const decodeRGB: (data: Uint8Array) => Promise<Nullable<WebPDecodedImageData>> | ||
export declare const decodeRGBA: (data: Uint8Array) => Promise<Nullable<WebPDecodedImageData>> | ||
export declare const decodeAnimation: ( | ||
data: Uint8Array, | ||
hasAlpha: boolean | ||
) => Promise<Nullable<DecodedWebPAnimationFrame[]>> |
Oops, something went wrong.