Skip to content
LIBRARYForms

ImageCropper ​

RC.12.1

Included in RC.12.1 for Vue + Vite, Nuxt and NativeScript.

Web preview
Open example ↗
vue
<MImageCropper v-model="crop" :src="image" label="Cover" :aspectRatio="1" @export="saveExport" />
PropTypeDefaultContract
src / modelValuestring / ImageCrop | nullrequired / nullDecoded image and controlled crop {x,y,width,height,rotation}; coordinates are pixels after quarter rotation.
aspectRatio / outputnumber / CropExportOptions1 / PNG, quality .92Aspect 0.1–10; output mimeType PNG/JPEG, quality 0–1 and optional width. Output dimensions 1–4096px.
animated / disabledbooleantrue / falseJolt settling and editing/export lock.
label / idstringrequired / optionalAccessible component name and optional stable ID.

Drag to pan, pinch to zoom, or use the named direction buttons, zoom slider and 90° rotation button. Escape/pointer cancellation restores the uncommitted crop on Web. The model remains canonical; the app must accept emitted updates. A null model fits the source to the requested aspect. Clear modelValue when changing to an incompatible image/aspect. Web exports a PNG/JPEG data URI through Canvas; remote images must permit CORS. iOS/Android use native image drawing and export a temporary file. The export object includes uri, width, height, mimeType and async dispose(); the consumer owns the result and must call dispose() after using a native temporary file. At most 40 decoded megapixels. This is raster cropping, without filters, arbitrary angles or SVG editing. Loading/export failures emit error; stale loads and exports are released on source changes/deactivation. Native accessibility uses explicit buttons as gesture alternatives.

update:modelValue → ImageCrop; export → CropExport; load → {width,height}; error → {stage,message}. Exposed export() returns Promise<CropExport | undefined>.

Motion uses Jolt and respects reduced motion. Use :animated="false" for static presentation.

The rule-of-thirds guides stay faint at rest and brighten while you drag or pinch, then fade back after you let go. The direction buttons, zoom slider, rotate and reset light them briefly too, and with animated the photo springs to the new crop. The guides are decorative and hidden from assistive technology.

vue
<script setup lang="ts">
import { computed, ref, onBeforeUnmount } from 'vue'
import { useCatalogLocale } from '../locale'
const props = defineProps<{ lang?: 'en' | 'pt' }>()
const { locale } = useCatalogLocale(() => props.lang)
const t = (en: string, pt: string) => ((props.lang ?? locale.value) === 'pt' ? pt : en)
const animated = ref(true)

import { mountains, coast } from '../atlas/photos'
import type { ImageCrop, CropExport } from '@getcortexlabs/mavis-core'
const crop = ref<ImageCrop | null>(null),
  alternate = ref(false),
  result = ref<CropExport>(),
  error = ref('')
function exported(image: CropExport) {
  void result.value?.dispose()
  result.value = image
}
function changeImage() {
  crop.value = null
  alternate.value = !alternate.value
  error.value = ''
}
onBeforeUnmount(() => {
  void result.value?.dispose()
})
</script>
<template>
  <MColumn gap="lg">
    <MText text="THE FIELD GUIDE / 04" variant="overline" tone="primary" />
    <MText
      :text="t('Find the frame that feels right.', 'Encontre o enquadramento certo.')"
      variant="title"
    />
    <MStack direction="column" :responsive="{ at: 900, direction: 'row' }" gap="lg" align="start">
      <MBox :grow="1"
        ><MImageCropper
          id="studio-cropper"
          v-model="crop"
          :src="alternate ? coast : mountains"
          :animated="animated"
          :label="t('The cover photograph', 'A fotografia de capa')"
          :output="{ mimeType: 'image/png', quality: 1, width: 480 }"
          :zoomLabel="t('Zoom', 'Zoom')"
          :rotateLabel="t('Rotate clockwise', 'Girar à direita')"
          :resetLabel="t('Reset', 'Redefinir')"
          :exportLabel="t('Export image', 'Exportar imagem')"
          :moveLabel="t('Move image', 'Mover imagem')"
          :leftLabel="t('Move left', 'Mover à esquerda')"
          :rightLabel="t('Move right', 'Mover à direita')"
          :upLabel="t('Move up', 'Mover acima')"
          :downLabel="t('Move down', 'Mover abaixo')"
          :loadingLabel="t('Loading image', 'Carregando imagem')"
          @export="exported"
          @error="error = $event.message"
      /></MBox>
      <MCard padding="md" :width="220"
        ><MColumn gap="md"
          ><MText :text="t('Ready for the cover', 'Pronta para a capa')" variant="heading" /><MImage
            v-if="result"
            :src="result.uri"
            :height="180"
            :alt="t('Exported crop', 'Recorte exportado')" /><MText
            id="crop-result"
            :text="
              result
                ? `${result.width} × ${result.height}`
                : t('Your export appears here.', 'Seu recorte aparece aqui.')
            "
            variant="caption" /><MText v-if="error" :text="error" /></MColumn
      ></MCard>
    </MStack>
    <MButton
      :text="t('Change photograph', 'Trocar fotografia')"
      variant="secondary"
      @click="changeImage"
    />
    <MText
      id="crop-value"
      :text="crop ? `${Math.round(crop.x)},${Math.round(crop.y)},${crop.rotation}` : 'original'"
      variant="caption"
      tone="muted"
    />

    <MSwitch v-model="animated" :label="t('Animate changes', 'Animar mudanças')" />
  </MColumn>
</template>

Desktop ​

On the Mac and on Windows the frame moves over the picture with a drag or the arrow keys, and the slider and + and - zoom; the result is the rectangle in modelValue. The hosts cannot encode or rotate a picture yet, so there are no export or rotate buttons, export() reports an error with stage export, and on the Mac a picture saved at a resolution other than 72 dpi is measured in points. See Desktop components.

Components for NativeScript and Web. Starting with Vue.