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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
| <!DOCTYPE html>
<html lang="zh-cn" >
<head >
<meta charset="UTF-8" >
<title > Barrage </title >
<style >
#container {
position : relative;
height : 800px;
width : 1000px;
overflow : hidden;
border : 1px solid black;
}
#container video {
position : absolute;
height : 100%;
width : 100%;
}
#container #wrapper {
position : absolute;
z-index : 100;
border : 1px solid black;
width : 100%;
height : 100%;
}
#barrage-box {
position : absolute;
z-index : 99;
width : 200%;
height : 100%;
transform : translateX(-40%);
}
.barrage {
color : red;
animation : barrage 10s linear;
position : absolute;
font-size : 30px;
font-family : "DejaVu Math TeX Gyre";
user-select : none;
}
@keyframes barrage {
from {
left : 100%;
}
to {
left : -100%;
}
}
</style >
</head >
<body >
<div id="app" >
<barrage-video />
</div >
</body >
<script src="https://lib.baomitu.com/vue/3.0.2/vue.global.js" ></script >
<script >
const { createApp, ref } = Vue
const useVideo = () => {
const video = ref(null)
const barrageList = ref([])
let id = 0
function videoClick() {
video.value.paused ? video.value.play() : video.value.pause()
}
function play() {
video.value.play()
}
function pause() {
video.value.pause()
}
function fullScreen() {
const element = document.querySelector('#container');
console.log(document.fullscreenEnabled)
if (document.fullscreenEnabled && document.fullscreenElement == null) {
element.requestFullscreen()
return
}
document.exitFullscreen()
}
function closeFullScreen() {
document.exitFullscreen()
}
function sendBarrage() {
const top = Math.floor(Math.random() * 30) * 30 + 'px'
const meta = {
text: '弹幕', top, time: +new Date(), className: '', id: ++id
}
barrageList.value.filter(item => {
return meta.time - item.time < 10
})
barrageList.value.push(meta)
meta.className = 'barrage'
}
function onMouseLeave({ target }) {
target.style.animationPlayState = 'running';
}
function onMouseEnter({ target }) {
target.style.animationPlayState = 'paused';
}
return {
video,
sendBarrage,
closeFullScreen,
fullScreen,
play,
pause,
videoClick,
barrageList,
onMouseEnter,
onMouseLeave
}
}
const Video = {
template: `
<div id="container" >
<video src="http://172.22.0.3/video.mp4" ref="video" autoplay ></video >
<div id="barrage-box" @dblclick="fullScreen" @click="videoClick" >
<div @mouseenter="onMouseEnter"
@mouseleave="onMouseLeave"
v-text="item.text"
v-for="item in barrageList"
:style="{marginTop: item.top}"
:class="item.className"
:key="item.id" />
</div >
</div >
<button @click="play" >PLAY</button >
<button @click="pause" >PAUSE</button >
<button @click="fullScreen" >FullScreen</button >
<button @click="sendBarrage" >SEND</button >`,
setup() {
const video = useVideo()
return {
...video,
}
}
}
const app = createApp({})
app.component('BarrageVideo', Video)
app.mount('#app')
</script >
</html >
|