吉安感知网项目-前端
shuishen
5 days ago 6e88705bd5b443a259b24c17c8a299765d059d96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
  <div :style="styleName" class="basic-video">
    <div class="basic-video__border">
      <span :style="borderStyleName"></span>
      <span :style="borderStyleName"></span>
      <span :style="borderStyleName"></span>
      <span :style="borderStyleName"></span>
    </div>
    <img :style="imgStyleName" class="basic-video__img" :src="background" />
    <video class="basic-video__main" ref="main" autoplay muted></video>
  </div>
</template>
 
<script>
import RecordVideo from './plugin';
 
export default {
  name: 'basic-video',
  props: {
    background: {
      type: String,
    },
    width: {
      type: [String, Number],
      default: 500,
    },
  },
  computed: {
    styleName() {
      return {
        width: `${this.width}px`,
      };
    },
    imgStyleName() {
      return {
        width: `${this.width / 2}px`,
      };
    },
    borderStyleName() {
      return {
        width: `${this.width / 15}px`,
        height: `${this.width / 15}px`,
        borderWidth: `${5}px`,
      };
    },
  },
  data() {
    return {
      videoObj: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.videoObj = new RecordVideo(this.$refs.main);
      const videoPromise = this.videoObj.init();
      videoPromise.then(() => {
        this.videoObj.mediaRecorder.addEventListener('stop', this.getData, false);
      });
    },
    startRecord() {
      this.videoObj.startRecord();
    },
    stopRecord() {
      this.videoObj.stopRecord();
    },
    getData() {
      const blob = new Blob(this.videoObj.chunks, {
        type: 'video/mp4',
      });
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.addEventListener('loadend', () => {
        var video_base64 = reader.result;
        this.$emit('data-change', video_base64);
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.basic-video {
  margin: 0 auto;
  position: relative;
  overflow: hidden;
 
  &__border {
    span {
      position: absolute;
      width: 30px;
      height: 30px;
      border-width: 4px;
      color: #0073eb;
      border-style: solid;
 
      &:nth-child(1) {
        left: 15px;
        top: 15px;
        border-right: 0;
        border-bottom: 0;
      }
 
      &:nth-child(2) {
        right: 15px;
        top: 15px;
        border-left: 0;
        border-bottom: 0;
      }
 
      &:nth-child(3) {
        bottom: 15px;
        left: 15px;
        border-right: 0;
        border-top: 0;
      }
 
      &:nth-child(4) {
        bottom: 15px;
        right: 15px;
        border-left: 0;
        border-top: 0;
      }
    }
  }
 
  &__img {
    width: 100px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
 
  &__main {
    width: 100%;
  }
}
</style>