Adding browser's webp encoder (#72)

* Adding WebP (without feature detect in place)

* Adding WebP check

* Remove unused import
This commit is contained in:
Jake Archibald
2018-07-02 15:15:18 +01:00
parent a09ec269b8
commit 68729979e3
5 changed files with 94 additions and 20 deletions

View File

@@ -0,0 +1,23 @@
import { canvasEncode } from '../../lib/util';
export interface EncodeOptions { quality: number; }
export interface EncoderState { type: typeof type; options: EncodeOptions; }
export const type = 'browser-webp';
export const label = 'Browser WebP';
export const mimeType = 'image/webp';
export const extension = 'webp';
export const defaultOptions: EncodeOptions = { quality: 0.5 };
export async function featureTest() {
const data = new ImageData(1, 1);
const blob = await encode(data, defaultOptions);
// According to the spec, the blob should be null if the format isn't supported…
if (!blob) return false;
// …but Safari falls back to PNG, so we need to check the mime type.
return blob.type === mimeType;
}
export function encode(data: ImageData, { quality }: EncodeOptions) {
return canvasEncode(data, mimeType, quality);
}