Skip to content

Commit

Permalink
Add limited support for objectPosition property
Browse files Browse the repository at this point in the history
  • Loading branch information
damianstasik committed Dec 7, 2023
1 parent 29fe2e4 commit 940de75
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/builder/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export function getPreserveAspectRatio(
objectFit: string,
objectPosition?: string
) {
if (objectFit !== 'contain' && objectFit !== 'cover') {
return 'none'
}

let align = 'xMidYMid'

if (objectPosition) {
const parts = objectPosition.split(' ')

if (parts.length === 2) {
const [x, y] = parts

if (x === 'left') {
align = 'xMin'
} else if (x === 'right') {
align = 'xMax'
} else {
align = 'XMid'
}

if (y === 'top') {
align += 'YMin'
} else if (y === 'bottom') {
align += 'YMax'
} else {
align += 'YMid'
}
}
}

return align + (objectFit === 'cover' ? ' slice' : '')
}
11 changes: 5 additions & 6 deletions src/builder/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { buildXMLString } from '../utils.js'
import border, { getBorderClipPath } from './border.js'
import { genClipPath } from './clip-path.js'
import buildMaskImage from './mask-image.js'
import { getPreserveAspectRatio } from './image.js'

export default async function rect(
{
Expand Down Expand Up @@ -200,12 +201,10 @@ export default async function rect(
((style.borderBottomWidth as number) || 0) +
((style.paddingBottom as number) || 0)

const preserveAspectRatio =
style.objectFit === 'contain'
? 'xMidYMid'
: style.objectFit === 'cover'
? 'xMidYMid slice'
: 'none'
const preserveAspectRatio = getPreserveAspectRatio(
String(style.objectFit),
String(style.objectPosition)
)

shape += buildXMLString('image', {
x: left + offsetLeft,
Expand Down
49 changes: 49 additions & 0 deletions test/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,55 @@ describe('Image', () => {

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should convert objectFit', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
}}
>
<img
width='100%'
height='100%'
src='https://via.placeholder.com/150'
style={{
objectFit: 'cover',
}}
/>
</div>,
{ width: 100, height: 100, fonts }
)

expect(svg).toContain('preserveAspectRatio="xMidYMid slice"')
})

it('should convert objectPosition', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
}}
>
<img
width='100%'
height='100%'
src='https://via.placeholder.com/150'
style={{
objectFit: 'cover',
objectPosition: 'right top',
}}
/>
</div>,
{ width: 100, height: 100, fonts }
)

expect(svg).toContain('preserveAspectRatio="xMaxYMin slice"')
})
})

describe('background-image: url()', () => {
Expand Down

0 comments on commit 940de75

Please sign in to comment.