vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能
預(yù)覽鏈接點(diǎn)擊預(yù)覽
效果圖如下所示,大家感覺不錯(cuò),請參考實(shí)現(xiàn)代碼。
需求
- [x] 預(yù)覽:根據(jù)選擇圖像大小自適應(yīng)填充左側(cè)裁剪區(qū)域
- [x] 裁剪:移動(dòng)裁剪框右側(cè)預(yù)覽區(qū)域可實(shí)時(shí)預(yù)覽
- [x] 上傳&清空:點(diǎn)擊確認(rèn)上傳裁剪圖片,點(diǎn)擊取消按鈕清空圖像
- [ ] 裁剪框可調(diào)節(jié)大小
實(shí)現(xiàn)步驟
methods:funName() - 對應(yīng)源碼中methods中的funName方法
data:dataName - 對應(yīng)源碼中data中的dataName數(shù)據(jù)
1. 圖片選擇與讀取
- 選擇圖片 :(methods:selectPic) 使用 input[type="file"] 彈出選擇圖片框,js 主動(dòng)觸發(fā)點(diǎn)擊事件;
- 讀取圖片 : (methods:readImage) 創(chuàng)建圖片對象,使用createObjectURL顯示圖片。 objectURL = URL.createObjectURL(blob) ;
2. 在canvas中展示圖片
需要掌握的 canvas 相關(guān)知識(shí):
- 清空畫布 ctx.clearRect(x,y,width,height) ;
- 填充矩形 ctx.fillRect(x,y,width,height) ;
- 繪制圓弧 ctx.arc(x,y,r,startAngle,endAngle,counterclockwise) ; 繪制矩形 ctx.rect(x,y,width,height);
- 繪制圖像drawImage
# 語法 ctx.drawImage(image, dx, dy); ctx.drawImage(image, dx, dy, dWidth, dHeight); ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); # 參數(shù) image # 繪制的元素(可以為HTMLImageElement,HTMLVideoElement,或者 HTMLCanvasElement。) dx,dy # 目標(biāo)畫布(destination canvas)左上角的坐標(biāo) dWidth,dHeight # 目標(biāo)畫布(destination canvas)上繪制圖像寬高 sx,sy # 源畫布(source canvase)左上角的坐標(biāo) sWidth,sHeight # 源畫布(source canvase)選擇的圖像寬高
5.剪裁圖片 ctx.clip() ;
具體步驟:
- 計(jì)算canvas寬高 :(methods:calcCropperSize) 根據(jù)圖片大小,計(jì)算canvas寬高(data:cropperCanvasSize),以致圖片能夠在裁剪區(qū)域自適應(yīng)展示,并確定裁剪的左上角位置(data:cropperLocation)。
- 繪制左側(cè)裁剪區(qū)域圖像 :(methods:renderCropperImg)
裁剪區(qū)域vue data示意圖:
- 繪制右側(cè)預(yù)覽圖片 :(methods:renderPreviewImg)
3. 移動(dòng)裁剪框
知識(shí)點(diǎn): onmousedown、onmousemove、onmouseup
具體實(shí)現(xiàn):
methods:drag()
記錄鼠標(biāo)坐標(biāo),鼠標(biāo)移動(dòng)根據(jù)偏移量計(jì)算圓心位置。
canvas.onmousedown = e => { let [lastX, lastY] = [e.offsetX, e.offsetY]; self.movement = true; canvas.onmousemove = e => { self.circleCenter = { X: self.cropperCanvasSize.width > 2 * self.slectRadius ? self.circleCenter.X + (e.offsetX - lastX) : self.cropperCanvasSize.width / 2, Y: self.cropperCanvasSize.height > 2 * self.slectRadius ? self.circleCenter.Y + (e.offsetY - lastY) : self.cropperCanvasSize.height / 2 }; self.renderCropperImg(); [lastX, lastY] = [e.offsetX, e.offsetY]; }; canvas.onmouseup = e => { self.movement = false; canvas.onmousemove = null; canvas.onmouseup = null; }; };
4. 上傳圖片至服務(wù)器
知識(shí)點(diǎn):
具體實(shí)現(xiàn):
methods:upload() this.$refs.preview.toBlob((blob)=> { const url = URL.createObjectURL(blob); const formData = new FormData(); formData.append(this.uploadProps.name, blob, `${Date.now()}.png`); if(this.data){ Object.keys(this.uploadProps.data).forEach(key => { formData.append(key, this.uploadProps.data[key]); }); } const request = new XMLHttpRequest(); request.open("POST", this.uploadProps.action, true); request.send(formData); request.onreadystatechange = () => { if (request.readyState === 4 && request.status === 200) { // ... } }; });
總結(jié)
以上所述是小編給大家介紹的vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue+Websocket簡單實(shí)現(xiàn)聊天功能
這篇文章主要為大家詳細(xì)介紹了Vue+Websocket簡單實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08elementUI的table表格改變數(shù)據(jù)不更新問題解決
最近在做vue的項(xiàng)目時(shí)發(fā)現(xiàn)了一個(gè)問題,今天就來解決一下,本文主要介紹了elementUI的table表格改變數(shù)據(jù)不更新問題解決,感興趣的可以了解一下2022-02-02Vue局部組件數(shù)據(jù)共享Vue.observable()的使用
隨著組件的細(xì)化,就會(huì)遇到多組件狀態(tài)共享的情況,今天我們介紹的是 vue.js 2.6 新增加的 Observable API ,通過使用這個(gè) api 我們可以應(yīng)對一些簡單的跨組件數(shù)據(jù)狀態(tài)共享的情況,感興趣的可以了解一下2021-06-06手把手教你如何使用Vite構(gòu)建vue項(xiàng)目
這篇文章主要介紹了如何使用Vite構(gòu)建vue項(xiàng)目的相關(guān)資料,本文主要介紹了Vite構(gòu)建Vue項(xiàng)目的詳細(xì)步驟,包括檢查node.js和pnpm的安裝,構(gòu)建Vite+Vue項(xiàng)目,利用HBuilderX導(dǎo)入項(xiàng)目,需要的朋友可以參考下2024-10-10Vue動(dòng)態(tài)改變css樣式的3種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue動(dòng)態(tài)改變css樣式的3種方法,在Vue.js中我們經(jīng)常需要根據(jù)特定的條件或事件來動(dòng)態(tài)地修改CSS樣式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11