Skip to content

calculateSmartCrop ​

Compute a crop rectangle for a target aspect / focus.

Signature ​

typescript
interface SmartCropOptions {
  width: number
  height: number
  focus?: 'center' | 'top' | 'bottom' | 'left' | 'right'
}

interface CropResult {
  x: number
  y: number
  width: number
  height: number
}

function calculateSmartCrop(
  canvas: HTMLCanvasElement,
  options: SmartCropOptions
): CropResult

Via compress ​

typescript
await compress(file, {
  smartCrop: {
    enabled: true,
    width: 800,
    height: 600,
    focus: 'center',
  },
})

Direct ​

typescript
import { calculateSmartCrop } from '@pantanal/pixu'

const crop = calculateSmartCrop(canvas, { width: 1080, height: 1080, focus: 'top' })
ctx.drawImage(
  canvas,
  crop.x, crop.y, crop.width, crop.height,
  0, 0, crop.width, crop.height
)

Released under the MIT License.