| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <template>
- <div class="progress-box-bar" ref="progressbar">
- <canvas draggable="false" id="procanvas" height="60" @contextmenu="contextMenuClick" @mousedown="mousedownFunc" @mouseup="mouseupFunc" @mousemove="mousemoveFunc" @mouseout="mouseoutFunc"></canvas>
- <div class="bar-container">
- <div class="bar1"></div>
- <div class="bar2"></div>
- <div class="bar3"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name:"ProgressBar",
- props:["timeCell","startTime","videoData"],
- data(){
- return{
- canvas:"",
- ctx:"",
- canvasW:"",
- canvasH:"",
- nowTime:"",
- hours_per_ruler:24, //时间轴显示12小时
- half_time:5*60*60*1000,
- graduation_step:20, //刻度间最小宽度,单位px
- start_timestamp:new Date().getTime()- 5*60*60*1000, //开始时间、最左侧时间
- minutes_per_step:[1, 2, 5, 10, 15, 20, 30, 60, 120, 180, 240, 360, 720, 1440],
- distance_between_gtitle:80,
- g_isMousedown:false, //拖动mousedown标记
- g_isMousemove:false, //拖动mousemove标记
- g_mousedownCursor:null, //拖动mousedown的位置
- returnTime: null, //mouseup返回时间
- downTime:[], //下载时间
- downLeft:0,
- showDown:false,
- tiaoTop:40,
- tiaoHeight:20,
- }
- },
- watch:{
- timeCell(){
- if(this.timeCell.length>0){
- this.nowTime = this.changeTime(new Date(this.timeCell[0].beginTime).getTime());
- this.start_timestamp = new Date(this.timeCell[0].beginTime).getTime()- this.half_time;
- this.clearCanvas();
- this.initCanvas();
- }
- },
- startTime(){
- if(!this.g_isMousedown){
- this.nowTime = this.changeTime(this.startTime+this.half_time);
- this.start_timestamp = this.startTime;
- this.clearCanvas();
- this.initCanvas();
- }
- }
- },
- mounted() {
- this.canvas = document.getElementById("procanvas");
- this.ctx = this.canvas.getContext('2d');
- this.canvas.width = this.$refs.progressbar.offsetWidth;
- this.canvasW = this.canvas.width;
- this.canvasH = this.canvas.height;
- this.nowTime = this.changeTime(new Date().getTime());
- this.initCanvas();
- },
- methods:{
- // 初始化canvas
- initCanvas:function(){
- this.drawCellBg();
- //this.add_cells(this.timeCell);
- this.add_graduations(this.start_timestamp);
- //this.draw_down(this.downTime);
- //this.drawTimeTriangle();
- },
- // // 绘制三角 时间
- drawTimeTriangle(){
- // this.ctx.beginPath();
- // let x = this.canvasW/2;
- // let x1 = this.canvasW/2-7;
- // let x2 = this.canvasW/2+7;
- // this.ctx.moveTo(x1,25);
- // this.ctx.lineTo(x2,25);
- // this.ctx.lineTo(x,36);
- // this.ctx.fillStyle = "#318ed4";
- // this.ctx.closePath();
- // this.ctx.fill();
- // this.ctx.fillStyle = "#fff";
- // this.ctx.font = "14px Arial";
- // this.ctx.fillText(this.nowTime,x-60,20);
- },
- // 画背景色
- drawCellBg:function(){
- this.ctx.fillStyle = "#eaeaea";
- this.ctx.fillRect(0, this.tiaoTop, this.canvasW, this.tiaoHeight);
- },
- // 添加刻度
- add_graduations:function(start_timestamp){
- var _this = this;
- var px_per_min = _this.canvasW / (_this.hours_per_ruler * 60); // px/min
- var px_per_ms = _this.canvasW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
- var px_per_step = _this.graduation_step; // px/格 默认最小值20px
- var min_per_step = px_per_step / px_per_min; // min/格
- for(let i = 0; i < _this.minutes_per_step.length;i++){
- if(min_per_step <= _this.minutes_per_step[i]){ //让每格时间在minutes_per_step规定的范围内
- min_per_step = _this.minutes_per_step[i];
- px_per_step = px_per_min * min_per_step;
- break
- }
- }
- var medium_step = 30;
- for (let i = 0; i < _this.minutes_per_step.length; i++) {
- if (_this.distance_between_gtitle / px_per_min <= _this.minutes_per_step[i]) {
- medium_step = _this.minutes_per_step[i];
- break;
- }
- }
- var num_steps = _this.canvasW / px_per_step; //总格数
- var graduation_left;
- var graduation_time;
- var ms_offset = _this.ms_to_next_step(start_timestamp,min_per_step*60*1000);//开始的偏移时间 ms
- var px_offset = ms_offset * px_per_ms; //开始的偏移距离 px
- var ms_per_step = px_per_step / px_per_ms; // ms/step
- for(var i = 0; i < num_steps; i++){
- graduation_left = px_offset + i * px_per_step; // 距离=开始的偏移距离+格数*px/格
- graduation_time = start_timestamp + ms_offset + i * ms_per_step; //时间=左侧开始时间+偏移时间+格数*ms/格
- var date = new Date(graduation_time);
- if (graduation_time / (60 * 1000) % medium_step == 0) {
- var middle_date = _this.graduation_title(date);
- _this.ctx.fillStyle = "#318ed4";
- _this.ctx.font = "14px Arial";
- _this.ctx.fillText(middle_date,graduation_left-17,35);
- _this.drawLine(graduation_left,50,graduation_left,60,"#318ed4",1);
- }else{
- _this.drawLine(graduation_left,55,graduation_left,60,"rgba(49,142,212,0.7)",1);
- }
- }
- },
- // 画刻度线
- drawLine:function(beginX,beginY,endX,endY,color,width){
- this.ctx.beginPath();
- this.ctx.moveTo(beginX,beginY);
- this.ctx.lineTo(endX,endY);
- this.ctx.strokeStyle = color;
- this.ctx.lineWidth = width;
- this.ctx.stroke();
- },
- // 添加录像块
- // add_cells:function(cells){
- // var _this = this;
- // cells.forEach(cell => {
- // _this.draw_cell(cell)
- // });
- // },
- // 绘制录像块
- // draw_cell:function(cell){
- // var _this = this;
- // var px_per_ms = _this.canvasW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
- // var beginX = (cell.beginTime - _this.start_timestamp) * px_per_ms;
- // var cell_width = ( cell.endTime - cell.beginTime) * px_per_ms;
- // _this.ctx.fillStyle = "#2d86cd";
- // _this.ctx.fillRect(beginX,this.tiaoTop,cell_width,this.tiaoHeight);
- // },
- // 绘制下载录像块
- // draw_down:function(cell){
- // var _this = this;
- // var px_per_ms = _this.canvasW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
- // var beginX = cell[0]>cell[1]?(cell[1]- _this.start_timestamp)* px_per_ms:(cell[0]- _this.start_timestamp)* px_per_ms;
- // var cell_width = cell[0]>cell[1]?(cell[0]-cell[1])* px_per_ms:(cell[1]-cell[0])* px_per_ms;
- // _this.ctx.fillStyle = "#ff0000";
- // _this.ctx.fillRect(beginX,this.tiaoTop,cell_width,this.tiaoHeight);
- // },
- // 返回时间轴上刻度时间
- graduation_title:function(datetime){
- return (datetime.getHours()<10?'0'+datetime.getHours():datetime.getHours()) + ':' + ('0' + datetime.getMinutes().toString()).substr(-2);
- },
- // 开始时间 px
- ms_to_next_step:function(timestamp, step) {
- var remainder = timestamp % step;
- return remainder ? step - remainder : 0;
- },
- // 清除画布
- clearCanvas:function(){
- this.ctx.clearRect(0,0,this.canvasW,this.canvasH);
- },
- // 获取鼠标x位置
- get_cursor_x_position:function(e){
- var posx = 0;
- if (! e) {
- e = window.event;
- }
- if (e.pageX || e.pageY) {
- posx = e.pageX;
- }else if (e.clientX || e.clientY) {
- posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
- }
- return posx-455;//调整鼠标误差值
- },
- // 返回时间日期格式
- changeTime:function(time) {
- var newTime = new Date(time);
- var year = newTime.getFullYear();
- var month = newTime.getMonth() + 1;
- if(month < 10){
- var month = "0" + month;
- }
- var date = newTime.getDate();
- if (date < 10) {
- var date = "0" + date;
- }
- var hour = newTime.getHours();
- if (hour < 10) {
- var hour = "0" + hour;
- }
- var minute = newTime.getMinutes();
- if (minute < 10) {
- var minute = "0" + minute;
- }
- var second = newTime.getSeconds();
- if (second < 10) {
- var second = "0" + second;
- }
- return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
- },
- // 鼠标按下事件
- mousedownFunc:function(e){
- if(e.button == 2){
- }else{
- this.g_isMousedown = true;
- }
- this.g_mousedownCursor = this.get_cursor_x_position(e);//记住mousedown的位置
- },
- // 鼠标移动事件
- mousemoveFunc:function(e){
- let _this = this;
- let pos_x = _this.get_cursor_x_position(e);
- let px_per_ms = _this.canvasW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
- _this.clearCanvas();
- if(_this.g_isMousedown){
- var diff_x = pos_x - _this.g_mousedownCursor;
- _this.start_timestamp = _this.start_timestamp - Math.round(diff_x / px_per_ms);
- _this.initCanvas();
- _this.g_isMousemove = true;
- _this.g_mousedownCursor = pos_x;
- this.nowTime = this.changeTime(_this.start_timestamp+this.half_time);
- }else{
- var time = _this.start_timestamp + pos_x/px_per_ms;
- _this.initCanvas();
- _this.drawLine(pos_x,30,pos_x,60,"rgb(49,142,212)",1);
- _this.ctx.fillStyle = "rgb(194, 202, 215)";
- _this.ctx.fillText(_this.changeTime(time),pos_x-65,35);
- }
- },
- //鼠标释放弹起事件
- mouseupFunc:function(e){
- if(e.button == 2){
- if(this.downTime.length==2){
- this.downLeft = this.g_mousedownCursor;
- this.showDown = true;
- }
- }else{
- this.g_isMousedown = false;
- this.g_isMousemove = false;
- let time = this.start_timestamp+this.half_time;
- this.$emit("setDragPlayTime",time);
- }
- },
- //鼠标移除隐藏时间事件
- mouseoutFunc:function(e){
- this.clearCanvas();
- this.initCanvas();
- },
- contextMenuClick:function(e){
- e.preventDefault();
- return;
- var _this = this;
- var pos_x = _this.get_cursor_x_position(e);
- var px_per_ms = _this.canvasW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
- var time = _this.start_timestamp + pos_x/px_per_ms;
- var flag = false;
- var index = 0;
- for(let i = 0;i<_this.timeCell.length;i++){
- if(time>=_this.timeCell[i].beginTime && time<=_this.timeCell[i].endTime){
- flag = true;
- index = i;
- }
- }
- if(flag){
- if(_this.downTime.length<2){
- _this.downTime.push(time);
- if(_this.downTime.length==2){
- _this.clearCanvas();
- _this.initCanvas();
- }
- }else if(_this.downTime.length==2){
- // console.log(_this.downTime);
- }
- }
- },
- downCLickBack(){
- if(this.downTime.length == 2){
- this.downTime.sort(function(a,b){
- return a-b
- })
- var userData = this.$store.getters.replayDownVideo.length+1;
- var startTime1 = this.downTime[0]<this.downTime[1]?this.downTime[0]:this.downTime[1];
- var startTime = this.changeTime(startTime1);
- var endTime1 = this.downTime[0]>=this.downTime[1]?this.downTime[0]:this.downTime[1];
- var endTime = this.changeTime(endTime1);
- var fileName = new Date().getTime()+".mp4";
- var server = this.$store.getters.serverConfig;
- var alltime = Math.ceil((endTime1-startTime1)/1000);
- var obj = {
- fileName:fileName,startTime:startTime,endTime:endTime,allTime:alltime,progress:"0%",userDate:userData,downType:0
- };
- var s_index = 0,e_index = 0;
- for(var i = 0;i<this.timeCell.length;i++){
- if(this.downTime[0]>=this.timeCell[i].beginTime&&this.downTime[0]<=this.timeCell[i].endTime){
- s_index = i;
- }
- if(this.downTime[1]>=this.timeCell[i].beginTime&&this.downTime[1]<=this.timeCell[i].endTime){
- e_index = i;
- }
- }
- var fileArr = new Array();
- for(var j=s_index;j<=e_index;j++){
- fileArr.push(this.videoData.videoList[j]);
- }
- var filesJson = JSON.stringify(fileArr);
- this.downTime.splice(0,this.downTime.length);
- var ss = document.getElementById("videoOcx").StartDownloadVodFile(server.ServerIP,server.ServerPort,filesJson,fileName,startTime,endTime,userData);
- this.$store.commit("addDownLoadVideo",obj);
- this.showDown = false;
- }
- },
- cancelDownClick(){
- this.downTime.splice(0,this.downTime.length);
- this.showDown = false;
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .progress-box-bar{
- position: relative;
- width:100%;
- height: 100%;
- }
- .progress-box-bar canvas{
- width: 100%;
- cursor: pointer;
- }
- .progress-box-bar .downVideo{
- position: absolute;
- z-index: 10;
- top: 0;
- left: 300px;
- }
- .progress-box-bar .downVideo ul{
- background-color: #fff;
- }
- .progress-box-bar .downVideo ul li{
- line-height: 20px;
- font-size: 12px;
- cursor: pointer;
- }
- .progress-box-bar .downVideo ul li:hover{
- background-color: #318ed4;
- }
- .bar-container{
- width: 100%;
- height: 20px;
- background-color: #eaeaea;
- position: relative;
- .bar1{
- width: 80%;
- height: 100%;
- position: absolute;
- top:0;
- left: 0;
- background: green;
- z-index:2;
- }
- .bar2{
- width: 93%;
- height: 100%;
- position: absolute;
- top:0;
- left: 0;
- background: rgb(0, 178, 255);
- z-index:1;
- }
- .bar3{
- width: 10%;
- height: 100%;
- position: absolute;
- top:0;
- left: 30%;
- background: red;
- z-index:3;
- }
- }
- </style>
|