Skip to content

Commit

Permalink
"native" to "builtin" (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Jul 30, 2020
1 parent 1a26057 commit ed451e4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/codecs/decoders.ts
@@ -1,19 +1,19 @@
import { nativeDecode, sniffMimeType, canDecodeImage } from '../lib/util';
import { builtinDecode, sniffMimeType, canDecodeImage } from '../lib/util';
import Processor from './processor';
import webpDataUrl from 'url-loader!./tiny.webp';

const nativeWebPSupported = canDecodeImage(webpDataUrl);
const webPSupported = canDecodeImage(webpDataUrl);

export async function decodeImage(blob: Blob, processor: Processor): Promise<ImageData> {
const mimeType = await sniffMimeType(blob);

try {
if (mimeType === 'image/webp' && !(await nativeWebPSupported)) {
if (mimeType === 'image/webp' && !(await webPSupported)) {
return await processor.webpDecode(blob);
}

// Otherwise, just throw it at the browser's decoder.
return await nativeDecode(blob);
return await builtinDecode(blob);
} catch (err) {
throw Error("Couldn't decode image");
}
Expand Down
6 changes: 3 additions & 3 deletions src/codecs/resize/processor-sync.ts
@@ -1,4 +1,4 @@
import { nativeResize, NativeResizeMethod, drawableToImageData } from '../../lib/util';
import { builtinResize, BuiltinResizeMethod, drawableToImageData } from '../../lib/util';
import { BrowserResizeOptions, VectorResizeOptions } from './processor-meta';
import { getContainOffsets } from './util';

Expand All @@ -12,9 +12,9 @@ export function browserResize(data: ImageData, opts: BrowserResizeOptions): Imag
({ sx, sy, sw, sh } = getContainOffsets(sw, sh, opts.width, opts.height));
}

return nativeResize(
return builtinResize(
data, sx, sy, sw, sh, opts.width, opts.height,
opts.method.slice('browser-'.length) as NativeResizeMethod,
opts.method.slice('browser-'.length) as BuiltinResizeMethod,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/select/index.tsx
Expand Up @@ -12,8 +12,8 @@ export default class Select extends Component<Props, State> {

return (
<div class={style.select}>
<select class={`${style.nativeSelect} ${large ? style.large : ''}`} {...otherProps}/>
<svg class={style.arrow} viewBox="0 0 10 5"><path d="M0 0l5 5 5-5z"/></svg>
<select class={`${style.builtinSelect} ${large ? style.large : ''}`} {...otherProps} />
<svg class={style.arrow} viewBox="0 0 10 5"><path d="M0 0l5 5 5-5z" /></svg>
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/select/style.scss
Expand Up @@ -2,7 +2,7 @@
position: relative;
}

.native-select {
.builtin-select {
background: #2f2f2f;
border-radius: 4px;
font: inherit;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/util.ts
Expand Up @@ -163,21 +163,21 @@ export function drawableToImageData(
return ctx.getImageData(0, 0, width, height);
}

export async function nativeDecode(blob: Blob): Promise<ImageData> {
export async function builtinDecode(blob: Blob): Promise<ImageData> {
// Prefer createImageBitmap as it's the off-thread option for Firefox.
const drawable = 'createImageBitmap' in self ?
await createImageBitmap(blob) : await blobToImg(blob);

return drawableToImageData(drawable);
}

export type NativeResizeMethod = 'pixelated' | 'low' | 'medium' | 'high';
export type BuiltinResizeMethod = 'pixelated' | 'low' | 'medium' | 'high';

export function nativeResize(
export function builtinResize(
data: ImageData,
sx: number, sy: number, sw: number, sh: number,
dw: number, dh: number,
method: NativeResizeMethod,
method: BuiltinResizeMethod,
): ImageData {
const canvasSource = document.createElement('canvas');
canvasSource.width = data.width;
Expand Down
2 changes: 1 addition & 1 deletion src/sw/util.ts
Expand Up @@ -116,7 +116,7 @@ export async function cacheAdditionalProcessors(cacheName: string, buildAssets:
return createImageBitmap(blob).then(() => true, () => false);
})();

// No point caching the WebP decoder if it's supported natively:
// No point caching the WebP decoder if the browser supports it:
if (supportsWebP) {
toCache = toCache.filter(asset => !/webp[\-_]dec/.test(asset));
}
Expand Down

0 comments on commit ed451e4

Please sign in to comment.