直播平臺原始碼,vue圖片中劃框擷取部分圖片
直播平臺原始碼,vue圖片中劃框擷取部分圖片
<template> <div> <el-dialog title="請在圖片上畫出需要新增故障的位置" :visible.sync="dialogVisible" width="1270px" :before-close="handleClose" :close-on-click-modal="false" :close-on-press-escape="false"> <div> <img id="newImg" ref="newImg" style="user-select:none;" crossOrigin="Anonymous" :src="ftp+info.detectionImgLocation" width="1200" height="700" /> <canvas ref="myCanvas" class="myCanvas" width="1200" height="700"></canvas> </div> <!-- loading層,在上一層進行loading會導致canvas偏移的情況 --> <div v-if="loading" v-loading="loading" class="loading_c" element-loading-text="截圖中...."></div> </el-dialog> </div> </template> <script> //import html2canvas from "html2canvas"; export default { props: ['info'], data() { return { dialogVisible: true, ftp: process.env.VUE_APP_FTP_API, originalImg: { width: 0, height: 0 }, canvas: "", mousedownPostion: { x: 0, y: 0 }, finallyRect: { x: "", y: "", width: 0, height: 0 }, isDrawing: false,//是否開啟繪製 loading: true } }, mounted() { this.$nextTick(() => { this.getImgWidth() }) }, methods: { //關閉彈窗 handleClose() { this.$emit('closeAdd'); }, //畫布監聽 getImgWidth() { const myCanvas = this.$refs.myCanvas const img = new Image() img.src = this.ftp + this.info.detectionImgLocation img.onload = () => { //獲取圖片原始的長寬 this.originalImg.width = img.width this.originalImg.height = img.height this.loading = false } myCanvas.onmousedown = this.mouseDown // myCanvas.onmousemove = this.mouseMove myCanvas.onmouseup = this.mouseUp; myCanvas.onmouseout = this.mouseOut; this.canvas = myCanvas }, //滑鼠按下 mouseDown(e) { this.mousedownPostion.x = e.offsetX this.mousedownPostion.y = e.offsetY this.isDrawing = true this.canvas.onmousemove = this.mouseMove }, //滑鼠移動 mouseMove(e) { if (!this.isDrawing) return false let rectWidth = 0 let rectHeight = 0; rectWidth = Math.abs(this.mousedownPostion.x - e.offsetX) rectHeight = Math.abs(this.mousedownPostion.y - e.offsetY) if (this.mousedownPostion.x - e.offsetX > 0 || this.mousedownPostion.y - e.offsetY > 0) { //從右下往左上畫 this.drawReat(e.offsetX, e.offsetY, rectWidth, rectHeight) } else { this.drawReat(this.mousedownPostion.x, this.mousedownPostion.y, rectWidth, rectHeight) } }, //滑鼠抬起 mouseUp(e) { if (!this.isDrawing) return false; this.isDrawing = false this.loading = true this.canvas.onmousemove = function () { } this.shareHandle() }, //超出canvas mouseOut(e) { if (!this.isDrawing) return false; this.isDrawing = false this.loading = true this.canvas.onmousemove = function () { } this.shareHandle() }, //繪製矩形 drawReat(x, y, width, height) { this.finallyRect = { x: x, y: y, width: width, height: height } const context = this.canvas.getContext("2d") context.clearRect(0, 0, 1200, 700) context.save() //狀態的儲存 context.beginPath() context.strokeStyle = '#fff' context.lineWidth = 2 context.strokeRect(x, y, width, height) context.restore() //狀態的恢復 }, // 截圖 async shareHandle() { if (this.finallyRect.width <= 0 || this.finallyRect.height <= 0) { this.loading = false return false } //html2canvas截圖,速度太慢改為原生截圖 // 使用擷取指定範圍元素 // const opts = { // useCORS: true, // ...this.finallyRect // }; // const canvas = await html2canvas(this.$refs.newImg, opts); // const screenshotImage = canvas.toDataURL("image/jpg"); // // 圖片在這裡進行傳輸 // const scalX = this.originalImg.width / 1200 // const scalY = this.originalImg.height / 700 // this.finallyRect = { // x: this.finallyRect.x * scalX, // y: this.finallyRect.y * scalY, // width: this.finallyRect.width * scalX, // height: this.finallyRect.height * scalY // } // this.loading = false // this.$emit('screenshotImage', { // image: screenshotImage, // ...this.finallyRect, // picInfo: this.info // }); //縮放比例 const scalX = this.originalImg.width / 1200 const scalY = this.originalImg.height / 700 this.finallyRect = { x: this.finallyRect.x * scalX, y: this.finallyRect.y * scalY, width: this.finallyRect.width * scalX, height: this.finallyRect.height * scalY } // 建立一個新的canvas const canvasElement = document.createElement("canvas"); const canvasContext = canvasElement.getContext("2d"); const { x, y, width, height } = this.finallyRect canvasElement.width = width; canvasElement.height = height; // 框選區域繪製到canvas裡 canvasContext.drawImage(document.getElementById('newImg'), x, y, width, height, 0, 0, width, height); // 轉為base64進行顯示 const screenshotImage = canvasElement.toDataURL("image/jpeg"); this.loading = false this.$emit('screenshotImage', { image: screenshotImage, ...this.finallyRect, picInfo: this.info }); }, }, } </script> <style scoped> .doCanvas { position: absolute; left: 0; top: 13px; z-index: 50; } ::v-deep .el-dialog { margin-top: 8vh !important; } ::v-deep .el-dialog__body { position: relative; } .myCanvas { position: absolute; left: 20px; user-select: none; } .loading_c { width: 1200px; height: 700px; top: 30px; position: absolute; } </style>
以上就是 直播平臺原始碼,vue圖片中劃框擷取部分圖片,更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2996822/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- node平臺擷取圖片模組——jimp
- vue自定義指令擷取圖片中心顯示Vue
- 直播平臺原始碼,圖片放大瀏覽功能原始碼
- 直播平臺搭建原始碼,更改圖片透明度原始碼
- app直播原始碼,Vue獲取URL圖片的寬高APP原始碼Vue
- 直播軟體原始碼,Vue獲取URL圖片的寬高原始碼Vue
- 直播平臺搭建原始碼,bootstrap實現圖片輪播效果原始碼boot
- 直播平臺原始碼,el-button自定義圖片顯示原始碼
- 直播平臺原始碼,qt繪畫事件-設定背景圖片原始碼QT事件
- Java 圖片裁剪,擷取Java
- ImageView長圖擷取部分展示View
- 直播平臺原始碼,視訊抽幀作為圖片儲存原始碼
- 直播平臺原始碼,多種方法實現圖片複雜排列原始碼
- 直播平臺原始碼,上傳本地圖片實現個人名片背景圖輪播原始碼地圖
- 直播平臺搭建,實現圖片縮圖功能
- 求擷取圖片等比公式公式
- 一對一直播平臺原始碼,如何實現圖片釋出原始碼
- 如何擷取影片中的一部分,製作成GIF動態圖
- TableView ScrollreView 截圖 擷取全屏 圖片模糊View
- 視訊直播原始碼,css實現圖片對角邊框線原始碼CSS
- 短視訊平臺原始碼,自定義上傳有邊框的背景圖片原始碼
- js擷取影片的封面圖片JS
- 直播平臺軟體開發,完整擷取整個螢幕的截圖方式
- 直播平臺製作,利用python批量讀取儲存圖片Python
- 影片直播原始碼,載入gif圖片原始碼
- 成品直播原始碼,JAVA獲取圖片的寬、高和大小原始碼Java
- 擷取圖片生成頭像外掛
- 直播平臺原始碼,input密碼框顯示與隱藏原始碼密碼
- 直播平臺搭建,vue中實現圖片懶載入的幾種方法Vue
- 直播app原始碼,使用vue-awesome-swiper建立輪播圖幻燈片APP原始碼Vue
- 短視訊平臺原始碼,登入介面插入背景圖片原始碼
- 直播app原始碼,js圖片下載方式集合APP原始碼JS
- 影片直播原始碼,圖片選擇器ImagePicker原始碼
- 直播平臺原始碼,純JS實現左右滑動輪播圖原始碼JS
- 直播平臺搭建原始碼,XBanner設定只顯示輪播圖原始碼
- node上擷取圖片工具 images(node-images)
- 成品直播原始碼,例項原始碼系列-更改圖片透明度原始碼
- app直播原始碼,為文字/圖片新增按壓效果APP原始碼