Using the Image Hot-Spot Feature in a Next.js Project

19 replies
Last updated: Jul 27, 2022
Hello,
i’m trying to find out how to use the image hot-spot feature in my next-js project.
How can i display the image on my nextjs page the same way as the Sanity studio already displays the image underneath?

Is it just set with css (object-position:) or how can i make use of the hot-spot cropping?

Thank!
AI Update

The hotspot feature in Sanity Studio doesn't automatically apply to your Next.js frontend - you need to implement it yourself, either using @sanity/image-url or manually with CSS object-position.

When you query your image data from Sanity, the hotspot information is included in the image object. The @sanity/image-url builder will automatically respect the hotspot when you apply transformations:

import imageUrlBuilder from '@sanity/image-url'

const builder = imageUrlBuilder(sanityClient)

function urlFor(source) {
  return builder.image(source)
}

// The builder automatically uses the hotspot data
const imageUrl = urlFor(image)
  .width(400)
  .height(300)
  .url()

Manual CSS Implementation

If the image URL builder doesn't fully respect the hotspot in your setup (which can happen with certain frameworks), you can manually apply it using CSS object-position. The hotspot data contains x and y values between 0-1 representing the focal point as a percentage:

// Helper function to convert hotspot to CSS object-position
function getPositionFromHotspot(hotspot) {
  if (!hotspot || !hotspot.x || !hotspot.y) return "center";
  
  return `${hotspot.x * 100}% ${hotspot.y * 100}%`;
}

// Usage with Next.js Image component
<Image
  src={urlForImage(image).url()}
  alt={image.alt}
  fill
  style={{
    objectFit: 'cover',
    objectPosition: getPositionFromHotspot(image?.hotspot)
  }}
/>

Enabling Hotspot in Your Schema

Make sure you've enabled hotspot in your schema definition:

{
  name: 'image',
  title: 'Image',
  type: 'image',
  options: {
    hotspot: true
  }
}

As of Studio v3.86.0, you can even configure custom aspect ratio previews to help editors see how their hotspot will work at different sizes.

The key is that yes, it's essentially object-position under the hood, but the values come from the hotspot data you've set in Sanity Studio rather than being hardcoded in your CSS.

Is it such a stupid question, that nobody wants to answer or does this nobody know, didn't find any useful resources online 😅
Hey
user C
! Not a stupid question at all. Most likely no one was able to look at this until now. To clarify, are you trying to display all of those crop options or a single one you set in the Studio?
Thanks for your response, i‘m trying to recreate something similar like the preview in sanity studio. So for example the editor sets the focus point onto the person like in the example in the screenshot, and when the image gets responsive scaled the focus point gets moved into view, or stay in view
Most of that is going to need to be handled inside of the component in your frontend. However, you can use the Image URL Builder to fetch the images as those sizes directly.
Yes i‘m using this image url builder, so far it works very good with NextImage but it seems its not respecting the focus point, so i‘m not quite sure, how to use it in the frontend.
But i will double check to be sure, i‘m not overriding anything with css
When you build out the URL it won't automatically apply those, you need to pass your focal point and crops in. If you've set them on the image in the Studio, they'll be available in your asset's metadata.
Ahh.. now i get the point! I know what you mean! That was the missing part i didn't understand i think. Thanks i will try that
I still struggle with the image url builder, tbh 😅
Yeah its completely logical if you think about, but i don't know why, i didn't think about to pass the meta data from sanity into the urlbuilder, for no reason my brain thought the sanity response and url builder is the same, like passing the crop settings in to find out what are the crop settings 😅
Sorry me again, i still don’t get it what values i have to use for the urlbuilder, i checked the preview in Sanity Studio (which is using the correct crop settings from sanity lol) the url parameters are like this: rect=1716,50,3244,3244&amp;w=35&amp;h=35&amp;fit=crop&amp;dpr=2 | But when i inspect the data i get from sanity, my values are in the 0.xxx area (as you can see in the screenshot). how is this rect values calculated for example if sanity delivers completly different values.
Would you like to try next-sanity-image? I love their functionality and so far it worked fine.I will have a look later this week but we are a bit shortstaffed bc of holidays, so things might take a bit longer…
Yes i thought about next-sanity-image too, but i got upstream dependency conflicts.
user C
could you send us the groq data you receive?We think we might know what it could be, but need to check
Sure, here is how i query the data, the type interface and the result. Or do you need it in a file (not screenshots)? Thanks
We think we know what’s wrong: you’re incorrectly calling the
rect
method, which is what you’d do to manually specify a crop in pixels and overrides the crop/hotspot settings in the studio.
Instead, you should be passing the entire image object (including crop and hotspot) when calling
urlFor
. It will do the rest, when you set a width and height.
These are the relevant parts of
the docs .

When constructing the URL:

In order for hotspot/crop processing to be applied, the
image
record must be supplied, as well as both width and height.
The
rect
method should only be used when you want to override the default image crop, specifying a new crop in pixels:


Specify the crop in pixels. Overrides any crop/hotspot in the image record.
Oh Yes 🥳 thanks now it works! I only passed width into the url parameter without height. such a simple change with such a big improvment. Thank you for your help!
pleasure… your problem was you did too much work 😉 so it was pretty easy to solve
Yes definitely, the plugin is awesome, didn't think that the plugin handles everything automatically 😅

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?