Vue Cropper.js Wrapper Component

5.0.0 · active · verified Sun Apr 19

Vue-cropperjs is a Vue.js wrapper component for the popular Cropper.js library, enabling image cropping, rotating, and zooming functionalities within Vue 3 applications. The current stable version is 5.0.0, which targets Vue 3.x.x. Previous versions (e.g., 4.2.0) supported Vue 2.x.x, indicating a clear version split for different Vue major versions. The project appears to have an active maintenance cadence, aligning with major Vue releases and dependency updates. Key differentiators include its direct integration with Vue's reactivity system and component model, simplifying the use of Cropper.js without direct DOM manipulation. It primarily acts as a direct passthrough for most Cropper.js options and methods, offering a thin, idiomatic Vue layer over the core JavaScript library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `vue-cropperjs` into a Vue 3 application, including importing the component and its CSS, displaying an image for cropping, and using a ref to access the underlying Cropper.js instance to perform actions like getting the cropped image and rotating it.

import { createApp, ref, onMounted } from 'vue';
import VueCropper from 'vue-cropperjs';
import 'cropperjs/dist/cropper.css';

const App = {
  components: { VueCropper },
  setup() {
    const cropperRef = ref(null);
    const imgSrc = ref('https://picsum.photos/800/600');
    const croppedImage = ref(null);

    const cropImage = () => {
      if (cropperRef.value) {
        croppedImage.value = cropperRef.value.getCroppedCanvas().toDataURL();
      }
    };

    const rotate = () => {
      if (cropperRef.value) {
        cropperRef.value.rotate(90);
      }
    };

    return {
      cropperRef,
      imgSrc,
      croppedImage,
      cropImage,
      rotate
    };
  },
  template: `
    <div>
      <h1>Image Cropper</h1>
      <div style="width: 600px; height: 400px; margin-bottom: 20px;">
        <vue-cropper
          ref="cropperRef"
          :src="imgSrc"
          alt="Source Image"
          :aspectRatio="16/9"
          :viewMode="1"
          @crop="cropImage"
        />
      </div>
      <button @click="cropImage">Get Cropped Image</button>
      <button @click="rotate">Rotate 90°</button>
      <div v-if="croppedImage" style="margin-top: 20px;">
        <h2>Cropped Image:</h2>
        <img :src="croppedImage" alt="Cropped Image" style="max-width: 100%; border: 1px solid #eee;"/>
      </div>
    </div>
  `
};

createApp(App).mount('#app');

view raw JSON →