VUE3 Getting Started Example: Making a Simple Image Cropper
Vue.js is a popular JavaScript front-end framework. The latest version - Vue3 has been launched. The new version of Vue has improved performance, size and development experience, and is welcomed by more and more developers. . This article will introduce how to use Vue3 to make a simple image cropper.
First, we need to create a Vue project and install the required plugins. You can use Vue CLI to create the project, or you can build it manually. Here we take the use of Vue CLI as an example:
# 安装Vue CLI npm install -g @vue/cli # 创建Vue项目 vue create image-cropper # 进入项目文件夹 cd image-cropper # 安装所需插件 npm install vue-cropperjs --save npm install cropperjs --save
Vue-Cropperjs is a plug-in for cropping images, and Cropperjs is the core library of Vue-Cropperjs and needs to be installed together.
Next, we need to introduce the Vue-Cropperjs plug-in into the Vue project. Modify the src/main.js
file as follows:
import Vue from 'vue' import App from './App.vue' import VueCropper from 'vue-cropperjs' import 'cropperjs/dist/cropper.css' Vue.use(VueCropper) Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app')
In the above code, we introduced the Vue-Cropperjs plug-in and called the Vue.use() method in Vue to register. It should be noted that here we also introduce the style file of Cropperjs to ensure the normal operation of the image cropper.
Next, we need to create an image cropper component in Vue. Create a new CropImage.vue
file in the src/views
directory and add the following code:
<template> <div> <div ref="wrapper"> <img ref="img" :src="src" style="max-width: 100%;" /> </div> <div> <input type="file" @change="onUpload" /> </div> <div> <vue-cropper ref="cropper" :src="src" :auto-crop-area="0.5" :guides="false" :view-mode="1" :drag-mode="dragMode" :crop-box-movable="false" :crop-box-resizable="false" :crop-box-border-radius="50"></vue-cropper> </div> <div> <button @click="onCrop">裁剪</button> </div> </div> </template> <script> export default { name: 'CropImage', data() { return { src: '', cropper: null, dragMode: 'move' } }, methods: { onUpload(e) { const file = e.target.files[0] if (file.type.match(/image.*/)) { const reader = new FileReader() reader.onload = (event) => { this.src = event.target.result } reader.readAsDataURL(file) } }, onCrop() { const canvas = this.$refs.cropper.getCroppedCanvas({ width: 100, height: 100 }) const dataUrl = canvas.toDataURL() console.log(dataUrl) } } } </script>
In the above code, we created a file named CropImage
Vue component, which contains three main elements:
- Image container
- Image upload button
- Image cropper
With the img
tag and a div
tag, we create an initial image container. Users can click the "Upload" button to select an image for cropping. When the user selects the image, we use FileReader to convert the image to base64 encoding and assign it to the src
attribute to preview the image.
The image cropper uses the vue-cropper
tag provided in the Vue-Cropperjs plug-in, which supports multiple attributes to control the performance of the cropper, such as: auto-crop- area
controls the area ratio of automatic cropping, guides
controls whether to display the cropping frame auxiliary lines, view-mode
controls the mode of the cropper, etc. In addition, we also set the movement mode of the crop box to "Move" to ensure that users can better operate the crop box.
The crop button is bound to the onCrop
method, which outputs the base64 encoding of the cropped image to the console. Developers can rewrite this method according to actual needs.
Finally we need to introduce the CropImage.vue
component into the App.vue
file. Modify the src/App.vue
file as follows:
<template> <div id="app"> <CropImage /> </div> </template> <script> import CropImage from './views/CropImage.vue' export default { name: 'App', components: { CropImage } } </script> <style> #app { max-width: 640px; margin: 0 auto; padding: 20px; } </style>
In the above code, we introduce the CropImage
component into the App.vue
file, And pass parameters through props
in the component tag to initialize the image cropper.
So far, we have completed the production of a simple image cropper, which can run normally and perform cropping. Of course, this is just an introductory example. Beginners can further understand the use and development skills of Vue3 by modifying and extending the code.
The above is the detailed content of VUE3 Getting Started Example: Making a Simple Image Cropper. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Using ECharts in Vue makes it easy to add data visualization capabilities to your application. Specific steps include: installing ECharts and Vue ECharts packages, introducing ECharts, creating chart components, configuring options, using chart components, making charts responsive to Vue data, adding interactive features, and using advanced usage.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Question: What is the role of export default in Vue? Detailed description: export default defines the default export of the component. When importing, components are automatically imported. Simplify the import process, improve clarity and prevent conflicts. Commonly used for exporting individual components, using both named and default exports, and registering global components.

The Vue.js map function is a built-in higher-order function that creates a new array where each element is the transformed result of each element in the original array. The syntax is map(callbackFn), where callbackFn receives each element in the array as the first argument, optionally the index as the second argument, and returns a value. The map function does not change the original array.
