Skip to content

Commit 36583ac

Browse files
axe312gerKyleAMathews
authored andcommitted
[Ready 4 Review] SQIP - Vectorized primitive image previews (#4205)
* first working version with Contentful assets only * add readme * respect width, height, aspect ratio, cropping, resize focus and background * add support for gatsby-transformer-sharp * integrate in gatsbygram * avoid useless regeneration cus contentDigest changes * proper way to get absolute path to ImageSharp nodes * queue preview generation and cache results on disk * upgrade to latest node-sqip to get rid of the GoLang dependency * replace custom svg data uri function with package * prepare images via sharp plugin and allow sharp transformations * load cached svg properly from disk * fix queue resolving to early * set contentful images to 400px * implement new sharp transformation awareness feature * WIP - extract generation and write first pseudo test * fix styling for gatsbygram post detail * finalize unit tests for actual sqip implementation * use 256px input image width to match sqip/primitive default * add using-sqip example page * some cleanup * clean up example and enhance polaroid effect * fix using-sqip dependency * remove base64 since it was not implemented and is bad for compression * remove sqip from gatsbygram example * simplify tests * Small change to trigger build
1 parent 9d6dfb3 commit 36583ac

31 files changed

+1540
-83
lines changed

examples/using-sqip/.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"browser": true
4+
},
5+
"globals": {
6+
"graphql": false
7+
}
8+
}

examples/using-sqip/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Project dependencies
2+
.cache
3+
node_modules
4+
yarn-error.log
5+
6+
# Build directory
7+
/public
8+
.DS_Store

examples/using-sqip/.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"trailingComma": "es5"
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
siteMetadata: {
3+
title: `Gatsby SQIP Example`,
4+
},
5+
plugins: [
6+
{
7+
resolve: `gatsby-source-filesystem`,
8+
options: {
9+
name: `images`,
10+
path: `${__dirname}/src/images/`,
11+
},
12+
},
13+
{
14+
resolve: `gatsby-source-filesystem`,
15+
options: {
16+
name: `background`,
17+
path: `${__dirname}/src/background/`,
18+
},
19+
},
20+
`gatsby-image`,
21+
`gatsby-plugin-sharp`,
22+
`gatsby-transformer-sharp`,
23+
`gatsby-transformer-sqip`,
24+
],
25+
}

examples/using-sqip/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "using-sqip",
3+
"description": "Gatsby example site using the sqip transformer plugin",
4+
"version": "1.0.0",
5+
"author": "Benedikt Rötsch <[email protected]>",
6+
"dependencies": {
7+
"gatsby": "^1.9.247",
8+
"gatsby-image": "^1.0.48",
9+
"gatsby-plugin-sharp": "^1.6.43",
10+
"gatsby-source-filesystem": "^1.5.34",
11+
"gatsby-transformer-sharp": "^1.6.23",
12+
"gatsby-transformer-sqip": "*"
13+
},
14+
"keywords": ["gatsby"],
15+
"license": "MIT",
16+
"scripts": {
17+
"build": "gatsby build",
18+
"develop": "gatsby develop",
19+
"format": "prettier --write 'src/**/*.js'",
20+
"test": "echo \"Error: no test specified\" && exit 1"
21+
},
22+
"devDependencies": {
23+
"prettier": "^1.12.0"
24+
}
25+
}
613 KB
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
4+
import Image from 'gatsby-image'
5+
6+
// Styling based on https://codepen.io/havardob/pen/ZOOmMe
7+
8+
const data = {
9+
'alisa-anton-166247-unsplash-2000px': {
10+
style: { left: `6vw`, bottom: `6vw` },
11+
author: `Alisa Anton`,
12+
url: `https://unsplash.com/photos/ukxAK0c2FqM`,
13+
},
14+
'anthony-esau-173126-unsplash-2000px': {
15+
style: { left: `30vw`, bottom: `12vw` },
16+
author: `Anthony Esau`,
17+
url: `https://unsplash.com/photos/N2zk9yXjmLA`,
18+
},
19+
'beth-solano-313648-unsplash-2000px': {
20+
style: { left: `39vw`, top: `3vw` },
21+
author: `Beth Solando`,
22+
url: `https://unsplash.com/photos/VGkn9ENxLXM`,
23+
},
24+
'desmond-simon-412494-unsplash-2000px': {
25+
style: { right: `5vw`, bottom: `5vw` },
26+
author: `Desmond Simon`,
27+
url: `https://unsplash.com/photos/HhOo98Iygps`,
28+
},
29+
'igor-ovsyannykov-307432-unsplash-2000px': {
30+
style: { right: `27vw`, bottom: `3vw` },
31+
author: `Igor Ovsyannykov`,
32+
url: `https://unsplash.com/photos/uzd2UEDdQJ8`,
33+
},
34+
'quino-al-101314-unsplash-2000px': {
35+
style: { right: `28vw`, bottom: `27vw` },
36+
author: `Quino Al`,
37+
url: `https://unsplash.com/photos/vBxlL1xpSdc`,
38+
},
39+
'samuel-zeller-16570-unsplash-2000px': {
40+
style: { right: `16vw`, top: `2vw` },
41+
author: `Samuel Zeller`,
42+
url: `https://unsplash.com/photos/CwkiN6_qpDI`,
43+
},
44+
'tyler-lastovich-205631-unsplash-2000px': {
45+
style: { right: `3vw`, top: `14vw` },
46+
author: `Tyler Lastovich`,
47+
url: `https://unsplash.com/photos/DMJUIGRO_1M`,
48+
},
49+
}
50+
51+
function generateStyle(imageData) {
52+
const rotation = Math.floor(Math.random() * 26) - 13
53+
return {
54+
boxSizing: `content-box`,
55+
56+
display: `block`,
57+
position: `absolute`,
58+
59+
width: `18vw`,
60+
61+
padding: `0.8vw 0.4vw 0`,
62+
background: `linear-gradient(120deg, #fff, #eee 60%)`,
63+
64+
color: `inherit`,
65+
66+
boxShadow: `2px 2px 7px 0px rgba(0,0,0,0.4), rgba(0, 0, 0, 0.1) 1px 1px 3px 0px inset`,
67+
transform: `rotate(${rotation}deg)`,
68+
69+
...imageData.style,
70+
}
71+
}
72+
73+
const Polaroid = ({ image }) => {
74+
const imageData = data[image.name]
75+
76+
return (
77+
<a
78+
href={imageData.url}
79+
title={`by ${imageData.author}`}
80+
style={generateStyle(imageData)}
81+
>
82+
<div style={{ position: `relative` }}>
83+
<Image
84+
sizes={{
85+
...image.childImageSharp.sizes,
86+
base64: image.childImageSharp.sqip.dataURI,
87+
}}
88+
/>
89+
<div
90+
style={{
91+
position: `absolute`,
92+
top: 0,
93+
left: 0,
94+
background: `linear-gradient(120deg, rgba(255, 255, 255, 0.5), transparent 60%, rgba(0, 0, 0, 0.4) 99%)`,
95+
boxShadow: `inset 4px 5px 10px 0 rgba(0,0,0,0.05)`,
96+
width: `100%`,
97+
height: `100%`
98+
}}
99+
/>
100+
</div>
101+
<div
102+
style={{
103+
lineHeight: `4vw`,
104+
textAlign: `center`,
105+
fontSize: `1.6vw`,
106+
}}
107+
>{`📷 ${imageData.author}`}</div>
108+
</a>
109+
)
110+
}
111+
112+
Polaroid.propTypes = {
113+
image: PropTypes.object,
114+
}
115+
116+
export default Polaroid
263 KB
Loading
256 KB
Loading
74.7 KB
Loading

0 commit comments

Comments
 (0)