国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vue項(xiàng)目中實(shí)現(xiàn)圖片裁剪功能

2024-05-06 15:38:15
字體:
供稿:網(wǎng)友

演示地址

https://my729.github.io/picture-crop-demo/dist/#/

前言

vue版本:3.6.3 https://cli.vuejs.org/zh/ cropperjs: 1.5.1 https://github.com/fengyuanchen/cropperjs elementUI https://element.eleme.io/#/zh-CN

使用 cropperjs插件 和 原生canvas 兩種方式實(shí)現(xiàn)圖片裁剪功能

使用cropperjs插件

安裝cropperjs

yarn install cropperjs

初始化一個(gè)canvas元素,并在上面繪制圖片

<canvas :id="data.src" ref="canvas"></canvas>
// 在canvas上繪制圖片drawImg () { this.$nextTick(() => { // 獲取canvas節(jié)點(diǎn) let canvas = document.getElementById(this.data.src) if (canvas) { // 設(shè)置canvas的寬為canvas的父元素寬度,寬高比3:2 let parentEle = canvas.parentElement canvas.width = parentEle.offsetWidth canvas.height = 2 * parentEle.offsetWidth / 3 let ctx = canvas.getContext('2d') ctx.clearRect(0, 0, canvas.width, canvas.height) let img = new Image() img.crossOrigin = 'Anonymous' img.src = this.data.src img.onload = function () { ctx.drawImage(img, 0, 0, canvas.width, canvas.height) } } })}

如果遇到canvas跨域繪制圖片報(bào)錯(cuò),設(shè)置圖片img.crossOrigin = 'Anonymous',并且服務(wù)器響應(yīng)頭設(shè)置Access-Control-Allow-Origin:*

創(chuàng)建cropperjs

// 引入import Cropper from 'cropperjs'// 顯示裁剪框initCropper () { let cropper = new Cropper(this.$refs.canvas, { checkCrossOrigin: true, viewMode: 2, aspectRatio: 3 / 2 })}

更多方法和屬性,參考官網(wǎng): https://github.com/fengyuanchen/cropperjs

具體實(shí)現(xiàn),可以查看源碼的cropper.vue 或 cropper.one.vue組件:

cropper.vue組件:https://github.com/MY729/picture-crop-demo/blob/master/src/components/cropper.vue
cropper.one.vue組件:https://github.com/MY729/picture-crop-demo/blob/master/src/components/cropper.one.vue

使用canvas實(shí)現(xiàn)圖片裁剪

支持鼠標(biāo)繪制裁剪框,并移動(dòng)裁剪框

思路:

在canvas上繪制圖片為背景 監(jiān)聽鼠標(biāo)點(diǎn)擊、移動(dòng)、松開事件

canvas的isPointInPath()方法: 如果給定的點(diǎn)的坐標(biāo)位于路徑之內(nèi)的話(包括路徑的邊),否則返回 false

具體實(shí)現(xiàn)可查看源碼cropper.canvas.vue組件: https://github.com/MY729/picture-crop-demo/blob/master/src/components/cropper.canvas.vue

cropImg () { let canvas = document.getElementById(this.data.img_url) let ctx = canvas.getContext('2d') let img = new Image() img.onload = function () { ctx.drawImage(img, 0, 0, canvas.width, canvas.height) } img.src = this.data.src let drag = false // 是否拖動(dòng)矩形 let flag = false // 是否繪制矩形 let rectWidth = 0 // 繪制矩形的寬 let rectHeight = 0 // 繪制矩形的高 let clickX = 0 // 矩形開始繪制X坐標(biāo) let clickY = 0 // 矩形開始繪制Y坐標(biāo) let dragX = 0 // 當(dāng)要拖動(dòng)矩形點(diǎn)擊時(shí)X坐標(biāo) let dragY = 0 // 當(dāng)要拖動(dòng)矩形點(diǎn)擊時(shí)Y坐標(biāo) let newRectX = 0 // 拖動(dòng)變化后矩形開始繪制的X坐標(biāo) let newRectY = 0 // 拖動(dòng)變化后矩形開始繪制的Y坐標(biāo) // 鼠標(biāo)按下 canvas.onmousedown = e => { // 每次點(diǎn)擊前如果有繪制好的矩形框,通過路徑繪制出來,用于下面的判斷 ctx.beginPath() ctx.setLineDash([6, 6]) ctx.moveTo(newRectX, newRectY) ctx.lineTo(newRectX + rectWidth, newRectY) ctx.lineTo(newRectX + rectWidth, newRectY + rectHeight) ctx.lineTo(newRectX, newRectY + rectHeight) ctx.lineTo(newRectX, newRectY) ctx.strokeStyle = 'green' ctx.stroke() // 每次點(diǎn)擊,通過判斷鼠標(biāo)點(diǎn)擊的點(diǎn)在矩形框內(nèi)還是外,來決定重新繪制還是移動(dòng)矩形框 if (ctx.isPointInPath(e.offsetX, e.offsetY)) { drag = true dragX = e.offsetX dragY = e.offsetY clickX = newRectX clickY = newRectY } else { ctx.clearRect(0, 0, canvas.width, canvas.height) ctx.drawImage(img, 0, 0, canvas.width, canvas.height) flag = true clickX = e.offsetX clickY = e.offsetY newRectX = e.offsetX newRectY = e.offsetY } } // 鼠標(biāo)抬起 canvas.onmouseup = () => { if (flag) { flag = false this.sureCrop(clickX, clickY, rectWidth, rectHeight) } if (drag) { drag = false this.sureCrop(newRectX, newRectY, rectWidth, rectHeight) } } // 鼠標(biāo)移動(dòng) canvas.onmousemove = (e) => { if (flag) { ctx.clearRect(0, 0, canvas.width, canvas.height) ctx.drawImage(img, 0, 0, canvas.width, canvas.height) rectWidth = e.offsetX - clickX rectHeight = e.offsetY - clickY ctx.beginPath() ctx.strokeStyle = '#FF0000' ctx.strokeRect(clickX, clickY, rectWidth, rectHeight) ctx.closePath() } if (drag) { ctx.clearRect(0, 0, canvas.width, canvas.height) ctx.drawImage(img, 0, 0, canvas.width, canvas.height) ctx.beginPath() newRectX = clickX + e.offsetX - dragX newRectY = clickY + e.offsetY - dragY ctx.strokeStyle = 'yellow' ctx.strokeRect(newRectX, newRectY, rectWidth, rectHeight) ctx.closePath() } }},// 拿到裁剪后的參數(shù),可自行處理圖片sureCrop (x, y, width, height) { let canvas = document.getElementById(this.data.img_url + 'after') // 設(shè)置canvas的寬為canvas的父元素寬度,寬高比3:2 let parentEle = canvas.parentElement canvas.width = parentEle.offsetWidth canvas.height = 2 * parentEle.offsetWidth / 3 let ctx = canvas.getContext('2d') let img = new Image() img.src = this.data.src img.onload = function () { ctx.beginPath() ctx.moveTo(x, y) ctx.lineTo(x + width, y) ctx.lineTo(x + width, y + height) ctx.lineTo(x, y + height) ctx.clip() ctx.drawImage(img, 0, 0, canvas.width, canvas.height) } ctx.stroke()}            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 台州市| 章丘市| 明光市| 新余市| 抚州市| 马边| 合山市| 曲阜市| 肇州县| 桐梓县| 连江县| 封开县| 仁寿县| 老河口市| 易门县| 新兴县| 东平县| 阿荣旗| 壶关县| 布尔津县| 泸定县| 大荔县| 古田县| 隆回县| 梁山县| 炉霍县| 师宗县| 盐源县| 永平县| 德令哈市| 临江市| 喀什市| 府谷县| 尼木县| 石渠县| 宁晋县| 凤山县| 霸州市| 嘉鱼县| 深水埗区| 宜阳县|