Using Sanity Hotspot with GatsbyImage for Image Cropping
16 replies
Last updated: May 24, 2021
J
Hey everyone! Is it possible to use the Sanity hotspot data with a
My use case is that I’m cropping photos into squares when they’re in a list but also shown full size elsewhere. I’d like to be able to choose the area that gets shown when in the cropped square with a Sanity hotspot. Thanks a lot for any help!
GatsbyImage? Maybe using the hotspot values with css
object-position? I couldn’t find much documentation on what the values actually mean, e.g:
height: 0.43673837232083357 width: 0.33333333333333326 x: 0.4060283687943266 y: 0.4828572548097652
May 23, 2021, 4:38 PM
M
also curious. Right now i am also using GatsbyImage with Sanity but dont know exactly if using
has any advantage over plain GatsbyImage. Especially when having hotspot in mind
gatsby-plugin-sanity-image
May 23, 2021, 5:13 PM
M
• Applies Sanity hotspot data as the
object-positionin case you need it
May 23, 2021, 5:15 PM
M
So it looks like that the plugin is the way to go.
May 23, 2021, 5:17 PM
J
That’s helpful thanks
Will try to play around with it and a GatsbyImage now
user U
! Had a look at the plugin code and it’s just setting the hotspot values straight in as a percentage:css={ hotspot && { objectPosition: [hotspot.x, hotspot.y] .map((value) => (value * 100).toFixed(2) + "%") .join(" "), } }
May 23, 2021, 5:51 PM
M
gimme feedback how this goes. I really would like to stick with plain gatsby ImagePlugin
May 23, 2021, 5:51 PM
M
but bear in mind that sharp also has some fancy options for guessing how you want to crop based on skin tones.
May 23, 2021, 6:09 PM
M
This should work pretty well for portraits… not so for skyscrapers i assume 😉
May 23, 2021, 6:10 PM
J
Yeah it would be really cool to be able to do it automatically but unfortunately I’m working with artwork at the moment
May 23, 2021, 6:17 PM
J
It’s kind of working? As in moving the hotspot around in sanity does move the image crop accordingly - but it’s not in sync with the preview in sanity studio
May 23, 2021, 6:18 PM
J
This is with this GatsbyImage updating on the left:
<GatsbyImage image={getImage(artwork.artworkImage.asset)} alt={""} style={{ width: "100%", height: "100%" }} imgStyle={{ width: `100%`, height: `100%`, objectPosition: `${hotspot.x * 100}% ${hotspot.y * 100}%`, }} />
May 23, 2021, 6:25 PM
J
In Sanity Studio they aren’t using object-position on the previews though, they’re overflowing the preview elements with e.g:
position: absolute; overflow: hidden; height: 100%; width: 138.07%; top: 0px; left: -12.91%;
May 23, 2021, 6:29 PM
M
ah thanks for feedback. Looks nice so far.
May 23, 2021, 6:55 PM
M
will do my tests most likely tomorrow.
May 23, 2021, 6:56 PM
M
mainly thinking about using the hotspot thing for social media sharing images as in og:image
May 23, 2021, 6:57 PM
M
Also did some tests. Same finding as you. But with hotspot i can replace my previous way of doing things. Before i used ArtDirection and delivered different images based on different breakpoints (screen sizes). Hotspot kind of replaces this optically… with the drawback that the image size wont be smaller with smaller screens. But will switch anyway. Maintaining different header image sizes depending on sreen size is too much effort.
May 24, 2021, 10:33 AM
M
One thing i noticed though: The sanity image plugin is also able to make usage of the crop feature… something the gatsby image plugin cant
May 24, 2021, 10:54 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.