### Setup for Real-time Preview Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html Import and register the Cropper and Preview components. This setup is necessary for implementing a live preview of the cropping result. ```javascript import { Cropper, Preview } from 'vue-advanced-cropper'; export default { components: { Cropper, Preview }, data() { return { img: 'https://images.unsplash.com/photo-1590291409749-452efbe0d76c?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80', result: { coordinates: null, image: null } }; }, methods: { onChange({ coordinates, image }) { this.result = { coordinates, image }; }, }, }; ``` -------------------------------- ### Install with npm Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/getting-started.html Install the library using npm. For Vue 2, use 'npm install -S vue-advanced-cropper@vue-2'. ```bash npm install -S vue-advanced-cropper ``` -------------------------------- ### Minimal Working Example (Template) Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/getting-started.html The template section for the minimal working example, binding the image source and handling the change event. ```html ``` -------------------------------- ### Minimum Vue Advanced Cropper Example Source: https://advanced-cropper.github.io/vue-advanced-cropper This is the minimum working example to integrate the Vue Advanced Cropper into your website. It requires importing the component and its styles, and provides a basic setup for image cropping. ```vue ``` -------------------------------- ### Install with yarn Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/getting-started.html Install the library using yarn. For Vue 2, use 'yarn add vue-advanced-cropper@vue-2'. ```bash yarn add vue-advanced-cropper ``` -------------------------------- ### Minimal Working Example (Vue Component) Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/getting-started.html A basic Vue component demonstrating the integration of the Cropper. Register the component locally or globally. ```javascript import { Cropper } from 'vue-advanced-cropper'; import 'vue-advanced-cropper/dist/style.css'; export default { components: { Cropper, }, data() { return { img: 'https://images.unsplash.com/photo-1600984575359-310ae7b6bdf2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=700&q=80', } }, methods: { change({ coordinates, canvas }) { console.log(coordinates, canvas) } }, } ``` -------------------------------- ### Custom Stencil Component Example Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/concepts.html This example demonstrates how to create a custom stencil component. It includes handlers for resize and move events, and uses default components like StencilPreview, BoundingBox, and DraggableArea. ```javascript import { StencilPreview, BoundingBox, DraggableArea } from 'vue-advanced-cropper'; export default { components: { StencilPreview, BoundingBox, DraggableArea, }, props: [ // Image object 'image', // Actual coordinates of the cropped fragment 'coordinates', // Stencil size desired by cropper 'stencilCoordinates', // Aspect ratios aspectRatio', 'minAspectRatio', 'maxAspectRatio', // Transitions: 'transitions' ], computed: { style() { const { height, width, left, top } = this.stencilCoordinates; const style = { position: 'absolute', width: `${width}px`, height: `${height}px`, transform: `translate(${left}px, ${top}px)`, }; if (this.transitions && this.transitions.enabled) { style.transition = `${this.transitions.time}ms ${this.transitions.timingFunction}`; } return style; }, }, methods: { onMove(moveEvent) { this.$emit('move', moveEvent); }, onMoveEnd() { this.$emit('moveEnd'); }, onResize(resizeEvent) { this.$emit('resize', resizeEvent); }, onResizeEnd() { this.$emit('resizeEnd'); }, aspectRatios() { return { minimum: this.aspectRatio || this.minAspectRatio, maximum: this.aspectRatio || this.maxAspectRatio, }; }, }, }; ``` ```html ``` -------------------------------- ### Import Cropper Component Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/getting-started.html Import the Cropper component and its default styles after installation. ```javascript import { Cropper } from 'vue-advanced-cropper'; import 'vue-advanced-cropper/dist/style.css'; ``` -------------------------------- ### Default Stencil Component Example Source: https://advanced-cropper.github.io/vue-advanced-cropper This example demonstrates a default stencil component that includes handlers for resizing and a draggable area for moving. It utilizes StencilPreview, BoundingBox, and DraggableArea components. ```vue ``` -------------------------------- ### Integrate Handler and Preview in a Circle Stencil Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/advanced-stencil.html This example demonstrates how to add a handler image and a `StencilPreview` component to a circular stencil. It includes necessary imports and styling for the preview and handler elements. ```javascript ``` -------------------------------- ### Fixed Stencil Example Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/advanced-recipes.html Use this configuration to create a fixed stencil, which is particularly useful for mobile devices where the cropping area should not be changed by the user. ```html ``` -------------------------------- ### Cropper Component Setup for Programmatic Manipulation Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/advanced-recipes.html Set up the cropper component in your template, assigning a ref to it (e.g., `ref="cropper"`) to enable programmatic control via methods like `zoom` and `move`. ```html ``` -------------------------------- ### Load Image from Disc Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html This example shows how to load an image from a file input using Blob and FileReader. It includes a helper function to detect the image MIME type. ```javascript import { Cropper } from 'vue-advanced-cropper' // This function is used to detect the actual image type, function getMimeType(file, fallback = null) { const byteArray = (new Uint8Array(file)).subarray(0, 4); let header = ''; for (let i = 0; i < byteArray.length; i++) { header += byteArray[i].toString(16); } sswitch (header) { case "89504e47": return "image/png"; case "47494638": return "image/gif"; case "ffd8ffe0": case "ffd8ffe1": case "ffd8ffe2": case "ffd8ffe3": case "ffd8ffe8": return "image/jpeg"; default: return fallback; } } export default { components: { Cropper, }, data() { return { image: { src: null, type: null } }; }, methods: { crop() { const { canvas } = this.$refs.cropper.getResult(); canvas.toBlob((blob) => { // Do something with blob: upload to a server, download and etc. }, this.image.type); }, reset() { this.image = { src: null, type: null } }, loadImage(event) { // Reference to the DOM input element const { files } = event.target; // Ensure that you have a file before attempting to read it if (files && files[0]) { // 1. Revoke the object URL, to allow the garbage collector to destroy the uploaded before file if (this.image.src) { URL.revokeObjectURL(this.image.src) } // 2. Create the blob link to the file to optimize performance: const blob = URL.createObjectURL(files[0]); // 3. The steps below are designated to determine a file mime type to use it during the // getting of a cropped image from the canvas. You can replace it them by the following string, // but the type will be derived from the extension and it can lead to an incorrect result: // // this.image = { // src: blob; // type: files[0].type // } // Create a new FileReader to read this image binary data const reader = new FileReader(); // Define a callback function to run, when FileReader finishes its job reader.onload = (e) => { // Note: arrow function used here, so that "this.image" refers to the image of Vue component this.image = { // Set the image source (it will look like blob:http://example.com/2c5270a5-18b5-406e-a4fb-07427f5e7b94) src: blob, // Determine the image type to preserve it during the extracting the image from canvas: type: getMimeType(e.target.result, files[0].type), }; }; // Start the reader job - read file as a data url (base64 format) reader.readAsArrayBuffer(files[0]); } }, }, destroyed() { // Revoke the object URL, to allow the garbage collector to destroy the uploaded before file if (this.image.src) { URL.revokeObjectURL(this.image.src) } } }; ``` -------------------------------- ### Function for sizeRestrictionsAlgorithm Source: https://advanced-cropper.github.io/vue-advanced-cropper/components/cropper.html Customizes size restrictions for the cropper's stencil. This example interprets min/max width and height as percentages of the image size, allowing for responsive sizing. ```javascript ({ minWidth, minHeight, maxWidth, maxHeight, imageSize }) => { return { maxWidth: imageSize.width * (maxWidth || 0) / 100, maxHeight: imageSize.height * (maxHeight || 0) / 100, minWidth: imageSize.width * (minWidth || 100) / 100, minHeight: imageSize.height * (minHeight || 100) / 100, } } ``` -------------------------------- ### Function for defaultVisibleArea Source: https://advanced-cropper.github.io/vue-advanced-cropper/components/cropper.html Dynamically calculates the visible area based on current coordinates, boundaries, and image size. This example expands the visible area by 100 pixels on all sides. ```javascript ({ coordinates, boundaries, imageSize }) => { return { left: coordinates.left - 50, top: coordinates.top - 50, width: coordinates.width + 100, height: coordinates.height + 100, } } ``` -------------------------------- ### Getting Image Data from Cropper Source: https://advanced-cropper.github.io/vue-advanced-cropper/components/preview.html It is recommended to use the `image` property directly from the cropper's results object to populate the preview component. ```javascript const { image } = this.$refs.cropper.getResult() ``` -------------------------------- ### Emitting ResizeEvent Source: https://advanced-cropper.github.io/vue-advanced-cropper/events/resize-event.html Emit a ResizeEvent to programmatically resize a stencil. This example shows how to specify pixel shifts for each direction and preserve the aspect ratio. ```javascript // For example, inside a method of your stencil this.$emit('move', new ResizeEvent( { left: leftShift, right: rightShift, top: topShift, bottom: bottomShift, }, { preserveAspectRatio: true } )) ``` -------------------------------- ### Registering Custom Stencil Components Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html Import and register custom stencil components in your Vue application if they are not globally registered. This example shows how to import CircleStencil and register it. ```javascript import { CircleStencil, Cropper } from 'vue-advanced-cropper'; export default { components: { Cropper, CircleStencil }, data() { return { img: 'https://images.unsplash.com/photo-1485178575877-1a13bf489dfe?ixlib=rb-1.2.1&auto=format&fit=crop&w=991&q=80', }; }, }; ``` -------------------------------- ### Upload Cropped Image to Server Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html This snippet demonstrates how to get the cropped image as a blob and upload it to a server using the fetch API. It's suitable for backends that accept multipart/form-data. Ensure your backend is configured to handle image uploads. ```javascript import { Cropper } from 'vue-advanced-cropper'; export default { components: { Cropper, }, data() { return { image: 'https://images.unsplash.com/photo-1591273531346-ba9262aa2da6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=751&q=80', }; }, methods: { reset() { this.image = null; }, uploadImage() { const { canvas } = this.$refs.cropper.getResult(); if (canvas) { const form = new FormData(); canvas.toBlob(blob => { form.append('file', blob); // You can use axios, superagent and other libraries instead here fetch('http://example.com/upload/', { method: 'POST', body: form, }); // Perhaps you should add the setting appropriate file format here }, 'image/jpeg'); } }, }, }; ``` -------------------------------- ### Pass Props to Stencil Component Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html Use the 'stencil-props' prop to pass configuration options like aspect ratio, movability, and resizability to the active stencil component. This example sets a fixed aspect ratio and disables movement and resizing. ```html ``` -------------------------------- ### Import Classic Theme CSS Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/theming.html Load the classic theme, which is the default theme with comprehensive styling. Import this CSS file to use the classic theme. ```css import 'vue-advanced-cropper/dist/theme.classic.css'; ``` -------------------------------- ### Customize Line Appearance Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/customize-appearance.html Modify the appearance of lines by applying custom CSS to the `.line` class, for example, changing border style and color. ```css .line { border-style: dashed; border-color: red; } ``` -------------------------------- ### Import Default Styles Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/theming.html Load the basic styles required for the cropper components to display correctly. This is the first step to enable any theme. ```javascript import 'vue-advanced-cropper/dist/style.css'; ``` -------------------------------- ### Props Source: https://advanced-cropper.github.io/vue-advanced-cropper/components/preview.html Configuration options for the Preview component. ```APIDOC ## Props ### `image` * **Type:** `Object` * **Default:** `null` * **Details:** The object containing data about the cropped image. It should have at least a `src` field (string). Other optional fields include `width` (number), `height` (number), and `transforms` (object with `rotate` and `flip` properties). ### `coordinates` * **Type:** `Object` * **Default:** `{}` * **Details:** An object representing the actual coordinates of the cropped fragment. ### `width` * **Type:** `Number` * **Details:** The desired width of the preview. If not provided, the preview will automatically determine its width based on its root element. ### `height` * **Type:** `Number` * **Details:** The desired height of the preview. If not provided, the preview will automatically determine its height based on its root element. ``` -------------------------------- ### Recommended Fixed Cropper Implementation Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/types.html The recommended implementation for a fixed cropper, combining fixed stencil size and static properties. ```html ``` -------------------------------- ### Implement Real-time Preview Source: https://advanced-cropper.github.io/vue-advanced-cropper/guides/recipes.html Use the Preview component alongside the Cropper, setting debounce to false for immediate updates. The preview component displays the cropped area in real-time. ```html
``` -------------------------------- ### Import Cropper Styles with CDN Source: https://advanced-cropper.github.io/vue-advanced-cropper/introduction/migration.html When using a CDN, include the stylesheet link to import the cropper styles. This is necessary for the cropper to display correctly. ```html