实现video播放器截图并且在canvas上绘制矩形
只展示核心代码 安装 video-playe 全局引入 有可能代码复制不完全,逻辑不变
<div id="videoItem">
<video-player
class="video-player vjs-custom-skin tine-font-size"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions"
id="video"
:crossOrigin="Anonymous"
:useCORS="true"
></video-player>
</div>
<div class="screenshotValue-item">
<div class="first" @click="btnScreenshot">截图</div>
<div class="firstTwo" @click="videoDialogFalse">取消</div>
</div>
<el-dialog
:visible.sync="showCutDialog"
v-if="showCutDialog"
:close-on-click-modal="false"
width="57%"
class="dialog-content"
height="800px"
title="截图绘制矩形"
id="dialog-content"
v-loading="videoLoading"
>
<div class="item-class-left">
<div style="position: relative;;"
id="icanvasImage"
>
<img id="customPositionImg" ref="table" :src="listValue" height="400" width="700" crossOrigin="Anonymous"
/>
<canvas
ref="icanvas"
:width="canvasWidth"
:height="canvasHeight"
id="myCanvas"
class="item-color-font-size"
style="position: absolute;left:0;"
@mousedown="handlerMouseDown($event)"
@mousemove="handlerMouseMove($event)"
@contextmenu="contextmenu($event)"
@mouseup="handlerMouseUp($event)"
></canvas>
</div>
</div>
Anonymous:"Anonymous",
showCutDialog:false,
listValue:null,
canvasWidth: 700,
canvasHeight: 400,
isMouseDown: false,
playerRef:null,
ctx:null,
tempImageCanvasList:[],
imageCanvas: new Image(),
this.coordinate:{
X:null,
Y:null,
}
playerOptions: {
playbackRates: [0.7, 1.0, 1.5, 2.0],
autoplay: true,
muted: false,
loop: false,
preload: 'auto',
language: 'zh-CN',
aspectRatio: '16:9',
fluid: true,
sources: [{
type: 'video/mp4' || 'video/ogg' || 'video/webm' || 'video/3gp',
src: ""
}],
num:null,
crossOrigin:"Anonymous",
poster: '',
notSupportedMessage: '此视频暂无法播放,请稍后再试',
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true
}
}
mounted
this.nextTick(_=>{
this.playerRef = this.$refs.videoPlayer
})
methods
btnScreenshot(){
this.playerRef.player.pause();
this.$nextTick(() => {
this.tempImageCanvasList = [];
this.showCutDialog = true;
this.getCanvasImage();
});
}
getCanvasImage(){
this.$nextTick((_) => {
if (this.playerRef) {
const canvasEl = document.getElementById('myCanvas')
const videoEl = this.playerRef.$refs.video;
this.ctx = canvasEl.getContext("2d");
const img = new Image();
let that = this
let videoList = document.querySelector('video')
console.log(videoList);
videoList.useCORS= true
videoEl.setAttribute("crossOrigin",'Anonymous')
that.ctx.drawImage(videoList, 0, 0, 700, 400);
let tempImage = canvasEl.toDataURL('image/jpeg')
console.log(tempImage);
this.imageCanvas.src = tempImage;
this.listValue = tempImage
this.tempImageCanvasList.push(tempImage);
}
})
},
handlerMouseDown(e){
console.log('handlerMouseDown',e);
this.isMouseDown = true;
console.log(e.offsetX, e.offsetY;)
this.coordinate.X = e.offsetX;
this.coordinate.Y = e.offsetY;
},
handlerMouseUp(e) {
this.isMouseDown = false;
console.log(e.offsetX, e.offsetY;)
},
handlerMouseMove(e) {
if (this.isMouseDown) {
const {
X, Y,
} = this.coordinate;
const canvasEl = this.$refs.icanvas;
this.ctx = canvasEl.getContext('2d');
this.ctx.clearRect(0,0,canvasEl.width,canvasEl.height);
this.ctx.beginPath();
this.ctx.strokeStyle = 'red';
this.ctx.lineWidth = 4;
this.ctx.strokeRect(X,Y,e.offsetX-X,e.offsetY -Y);
}
},
|