演示地址
https://my729.github.io/picture-crop-demo/dist/#/
前言
使用 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()}            
新聞熱點(diǎn)
疑難解答
圖片精選