Creating a Vue component for uploading and cropping an image using the Composition API involves handling the image upload, allowing the user to select a crop area via mouse events, and displaying the cropped image as a preview. This task can be complex due to the manual handling of image data and mouse events for the cropping functionality. Below is a simplified example to get you started. This example won't cover all cases or error handling but will provide a basic framework.
First, ensure you have a Vue 3 project setup. If you're starting from scratch, you can quickly set up a project using Vue CLI.
- Install Vue CLI (if you haven't already):
npm install -g @vue/cli
- Create a new Vue project (if needed):
vue create my-vue-app
- Navigate to your project folder:
cd my-vue-app
- Add the following component:
Create a new file named ImageCropper.vue in your src/components directory and add the following content:
<template>
<div>
<input type="file" @change="onFileChange" />
<canvas v-if="image" ref="canvas"></canvas>
<button v-if="croppedImage" @click="clear">Reset</button>
<div v-if="croppedImage">
<p>Cropped Image:</p>
<img :src="croppedImage" alt="Cropped Image" />
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue';
export default {
setup() {
const image = ref(null);
const canvas = ref(null);
const croppedImage = ref(null);
const ctx = ref(null);
let startX, startY, isDown = false;
onMounted(() => {
if (canvas.value) {
ctx.value = canvas.value.getContext('2d');
canvas.value.addEventListener('mousedown', startCrop);
canvas.value.addEventListener('mousemove', crop);
canvas.value.addEventListener('mouseup', endCrop);
}
});
const onFileChange = (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
canvas.value.width = img.width;
canvas.value.height = img.height;
ctx.value.drawImage(img, 0, 0);
image.value = img;
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
};
const startCrop = (e) => {
isDown = true;
startX = e.offsetX;
startY = e.offsetY;
};
const crop = (e) => {
if (isDown) {
const width = e.offsetX - startX;
const height = e.offsetY - startY;
ctx.value.clearRect(0, 0, canvas.value.width, canvas.value.height);
ctx.value.drawImage(image.value, 0, 0);
ctx.value.strokeRect(startX, startY, width, height);
}
};
const endCrop = (e) => {
isDown = false;
const width = e.offsetX - startX;
const height = e.offsetY - startY;
cropImage(startX, startY, width, height);
};
const cropImage = (x, y, width, height) => {
const croppedCanvas = document.createElement('canvas');
croppedCanvas.width = width;
croppedCanvas.height = height;
const croppedCtx = croppedCanvas.getContext('2d');
croppedCtx.drawImage(
image.value,
x, y, width, height, // Source rectangle
0, 0, width, height // Destination rectangle
);
croppedImage.value = croppedCanvas.toDataURL();
};
const clear = () => {
image.value = null;
croppedImage.value = null;
ctx.value.clearRect(0, 0, canvas.value.width, canvas.value.height);
};
return { image, canvas, croppedImage, onFileChange, clear };
},
};
</script>
<style scoped>
canvas {
border: 1px solid #000;
}
</style>
This component provides a basic image upload and cropping functionality using the mouse. You can upload an image, which will then be drawn onto a canvas. You can then use the mouse to select a cropping area. Releasing the mouse will crop the image to the selected area, and the cropped image will be displayed below.
Please note, this is a simplified example and might need adjustments based on your specific requirements, such as handling different image orientations, adding touch event support for mobile devices, or improving the user experience with visual feedback during cropping.