This commit is contained in:
2019-01-08 23:49:27 -08:00
parent d479c2caef
commit 7481d23389
2 changed files with 22 additions and 15 deletions

View File

@@ -14,7 +14,8 @@ export interface State {
} }
interface SrcSetInfo { interface SrcSetInfo {
srcSet: string; jpeg: string;
webp: string;
bestSrc: string; bestSrc: string;
} }
@@ -44,17 +45,21 @@ export class Picture extends React.PureComponent<Props, State> {
const srcSet = this._srcset(); const srcSet = this._srcset();
return ( return (
<img <picture>
onClick={this.props.onClick} <source srcSet={srcSet.webp} type="image/webp" />
srcSet={srcSet.srcSet} <source srcSet={srcSet.jpeg} type="image/jpeg" />
src={srcSet.bestSrc} <img
width={this.props.width + "px"} onClick={this.props.onClick}
/> src={srcSet.bestSrc}
width={this.props.width + "px"}
/>
</picture>
); );
} }
private _srcset = (): SrcSetInfo => { private _srcset = (): SrcSetInfo => {
const srcs: string[] = []; const jpegSrcSet: string[] = [];
const webpSrcSet: string[] = [];
let bestSize = 1600; let bestSize = 1600;
let bestRatio = Infinity; let bestRatio = Infinity;
@@ -67,7 +72,10 @@ export class Picture extends React.PureComponent<Props, State> {
const scale = width / this.props.width; const scale = width / this.props.width;
if (scale >= 1) { if (scale >= 1) {
srcs.push(`img/${size}/${this.props.image.src} ${scale}x`); const jpeg = `img/${size}/${this.props.image.src}`;
const webp = jpeg.replace("jpg", "webp");
jpegSrcSet.push(`${jpeg} ${scale}x`);
webpSrcSet.push(`${webp} ${scale}x`);
if (scale < bestRatio) { if (scale < bestRatio) {
bestSize = size; bestSize = size;
bestRatio = scale; bestRatio = scale;
@@ -75,10 +83,9 @@ export class Picture extends React.PureComponent<Props, State> {
} }
}); });
srcs.push(`img/${bestSize}/${this.props.image.src} 1x`);
return { return {
srcSet: srcs.join(","), jpeg: jpegSrcSet.join(","),
webp: webpSrcSet.join(","),
bestSrc: `img/${bestSize}/${this.props.image.src}` bestSrc: `img/${bestSize}/${this.props.image.src}`
}; };
}; };

View File

@@ -1,9 +1,9 @@
export const SIZES = [1600, 1200, 800, 600, 400, 200]; export const SIZES = [1600, 1200, 800, 600, 400, 200];
export function dataUrl() { export function dataUrl() {
return (window.location.host === "ski.aarongutierrez.com") return window.location.host === "ski.aarongutierrez.com"
? "https://s3-us-west-2.amazonaws.com/ski.aarongutierrez.com/img/data.json" ? "https://s3-us-west-2.amazonaws.com/ski.aarongutierrez.com/img/data.json"
: "img/data.json"; : "img/data.json";
} }
export interface Data { export interface Data {