How to Set a Hotspot Crop for an Image Hosted in Sanity
The crop and hotspot editor only shows up when the image field has options: { hotspot: true } in its schema. Studio only stores the crop and hotspot as data on the image field, so your front end has to apply them when it builds the image URL. The @sanity/image-url builder does this for you.
1. Enable hotspot on the image field
The hotspot option is false by default (Image type). Add it to the field. You can also add extra fields, like alt text or a caption, to the image:
import {defineField} from 'sanity'
defineField({
name: 'mainImage',
title: 'Main image',
type: 'image',
options: {
hotspot: true,
},
fields: [
defineField({name: 'alt', type: 'string', title: 'Alternative text'}),
defineField({name: 'attribution', type: 'string', title: 'Attribution'}),
],
})hotspot can also be an object with previews. Editors then see how the crop looks at specific aspect ratios:
options: {
hotspot: {
previews: [
{title: '2:1', aspectRatio: 2 / 1},
{title: '4:5', aspectRatio: 4 / 5},
],
},
}2. Get the change into Studio
- Local dev server: schema changes show up in Studio automatically.
- Hosted Studio (
*.sanity.studio): run your deploy script (npm run deploy, which runssanity deploy) from the Studio directory. This rebuilds and redeploys the Studio (Deploy a Studio). If the Studio is self-hosted or embedded in your app, redeploy it the way you normally deploy that app.
3. Set the crop and hotspot
Upload or pick an image in the field, then click the crop icon at the top right of the field (walkthrough). Drag the crop rectangle to the outer edges of what can be shown. Shrink the circle (the hotspot) around the part that must always stay in view.
The values are saved on the image field in the document, not on the asset. All values are fractions of the image size:
{
"_type": "image",
"asset": {"_type": "reference", "_ref": "image-..."},
"crop": {"top": 0.028, "bottom": 0.15, "left": 0.019, "right": 0.009},
"hotspot": {"x": 0.81, "y": 0.28, "height": 0.32, "width": 0.28}
}4. Apply them on the front end
The image is stored uncropped, so the original asset URL (asset->url) ignores the crop. Build the URL with @sanity/image-url instead (Presenting images):
npm install @sanity/image-urlimport {createImageUrlBuilder, type SanityImageSource} from '@sanity/image-url'
import {client} from './client'
const builder = createImageUrlBuilder(client)
export function urlFor(source: SanityImageSource) {
return builder.image(source)
}
// usage
const src = urlFor(post.mainImage).width(800).height(450).url()The builder first applies the crop. If the requested size needs more cropping, it keeps the hotspot in frame. Pass it the whole image field (post.mainImage), not just the asset, or it won't have the crop and hotspot data. Make sure your GROQ query returns that whole field, for example *[_type == "post"][0]{ mainImage } (course lesson).
If you get import errors, check your @sanity/image-url version: older versions used different imports and exports.
You can also crop by hand with URL parameters like ?rect=x,y,width,height and w/h, but then you have to convert the stored fractions to pixels yourself. The builder handles that.
Show original thread10 replies
Was this answer helpful?
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.