Skip to content

Commit

Permalink
搞定
Browse files Browse the repository at this point in the history
  • Loading branch information
yisar committed May 12, 2024
1 parent 2b8d1c2 commit 117e9a0
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 76 deletions.
23 changes: 17 additions & 6 deletions fre/src/danmaku/danmaku.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@ import { push } from '../use-route'
import { addDanmaku, getDanmakus, getUser } from '../util/api'
import './danmaku.css'

function getTimeStr(time) {
let h = Math.floor(time / 3600) as any
let m = Math.floor((time % 3600) / 60) as any
let s = Math.floor(time % 60) as any
h = h >= 10 ? h : '0' + h
m = m >= 10 ? m : '0' + m
s = s >= 10 ? s : '0' + s
return h === '00' ? m + ':' + s : h + ':' + m + ':' + s
}

export default function Danmaku({ post, p }) {
const [danmaku, setDanmaku] = useState('')
const [danmakus, setDanmakus] = useState([])
useEffect(() => {

getDanmakus(post.id, 0).then(res => {
setDanmakus((res as any).danmakus || [])
})

}, [])

function submit() {
if (danmaku.length < 1) {
return
}
const video = document.querySelector('e-player').shadowRoot.querySelector('video')
addDanmaku({
pid: post.id,
p,
pos: 0,
color: '#fffff',
pos: Math.floor(video.currentTime),
color: '#ffffff',
content: danmaku,
} as any).then((res: any) => {
alert(res.msg)
Expand All @@ -45,11 +54,13 @@ export default function Danmaku({ post, p }) {
{danmakus && danmakus.map(item => {
//@ts-ignore
const time = dayjs(item.time).format('MM-DD-YYYY')
//@ts-ignore
const pos = getTimeStr(item.pos)
return <div className="danmaku-item">
<p><a href={`https://www.clicli.cc/danmaku/delete/${item.id}?token=${window.localStorage.getItem('token')}`} target="_blank"><del>#{item.id}</del></a></p>
<p>{pos}</p>
{/* <p><a href={`https://www.clicli.cc/danmaku/delete/${item.id}?token=${window.localStorage.getItem('token')}`} target="_blank"><del>#{item.id}</del></a></p> */}
<p className="danmaku-block">{item.content}</p>
<p>{time}</p>

</div>
})}
</div>
Expand Down
3 changes: 1 addition & 2 deletions fre/src/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import RankList from '../rank/rank'
import Recommend from './recommend'
import UGCList from './ugc'
import PostList from './posts'
import Live from '../play/live'



Expand All @@ -27,7 +26,7 @@ export default function App(props) {
<div class="postplayer"><i class='icon-font icon-close' onclick={() => {
push('/')
}}></i>
{props.gv.indexOf('gv') > -1 ? <Post gv={props.gv}></Post> : <Live uu={props.gv}></Live>}
<Post gv={props.gv}></Post>
</div>
<div className="mask"></div></div> : <div></div>}
</div>
Expand Down
111 changes: 111 additions & 0 deletions fre/src/play/danmaku.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
export class Danmaku {
constructor(canvas, video, data) {
this.canvas = canvas
this.video = video
this.context = canvas.getContext('2d')
this.canvas.width = video.clientWidth
this.canvas.height = video.clientHeight
let defaultOpts = {
color: '#fff',
fontSize: 20,
speed: 2,
data: []
}
Object.assign(this, defaultOpts, { data })

this.paused = true
this.danmaku = this.data.map(danmu => new Danmu(danmu, this))

this.render()
}

render() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
this.draw()
if (!this.paused) {
requestAnimationFrame(this.render.bind(this))
}
}

draw() {
let cTime = this.video.currentTime
this.danmaku.forEach(danmu => {
if (!danmu.flag && danmu.pos <= cTime) {
if (!danmu.once) {
danmu.init()
danmu.once = true
}
danmu.x -= danmu.speed
danmu.render()
if (danmu.x <= danmu.width * -1) {
danmu.flag = true
}
}
})
}

add(danmu) {
this.danmaku.push(new Danmu(danmu, this))
}

play() {
this.paused = false
this.render()
}

pause() {
this.paused = true
}

reset() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
let cTime = this.video.currentTime
this.danmaku.forEach(danmu => {
danmu.flag = false
if (cTime < danmu.pos) {
danmu.paused = false
} else {
danmu.flag = true
}
})
}
}

class Danmu {
constructor(danmu, vm) {
this.danmu = danmu
this.content = danmu.content
this.pos = danmu.pos
this.vm = vm
}

init() {
this.color = this.danmu.color || this.vm.color
this.fontSize = this.danmu.fontSize || this.vm.fontSize
this.speed = this.danmu.speed || this.vm.speed

let span = document.createElement('span')
span.innerText = this.content
span.style.font = this.fontSize + 'px "微软雅黑"'
span.style.position = 'absolute'
document.body.appendChild(span)
this.width = span.clientWidth
document.body.removeChild(span)

this.x = this.vm.canvas.width
let r = this.vm.canvas.height * Math.random()
this.y = r - (r % this.fontSize)

if (this.y < this.fontSize) this.y = this.fontSize
if (this.y > this.vm.canvas.height - this.fontSize)
this.y = this.vm.canvas.height - this.fontSize
}

render() {
this.vm.context.font = this.fontSize + 'px "微软雅黑"'
this.vm.context.fillStyle = this.color
this.vm.context.fillText(this.content, this.x, this.y)
}
}

export default Danmaku
61 changes: 0 additions & 61 deletions fre/src/play/live.tsx

This file was deleted.

5 changes: 5 additions & 0 deletions fre/src/play/play.css
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,9 @@ article a {
align-items: center;
justify-content: center;
cursor: pointer;
}

canvas {
position: absolute;
z-index: 999;
}
30 changes: 23 additions & 7 deletions fre/src/play/play.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEffect, useState, useRef } from 'fre'
import { getPlayUrl, getPostDetail, getPv, getUser } from '../util/api'
import { useEffect, useState, useRef, Fragment } from 'fre'
import { getDanmakus, getPlayUrl, getPostDetail, getPv, getUser } from '../util/api'
import { getAv } from '../util/avatar'
import snarkdown from 'snarkdown'
import './play.css'
import Avatar from '../component/avatar/avatar'
import { push } from '../use-route'
import Comment from '../comment/comment'
import Danmaku from '../danmaku/danmaku'
import Danmu from './danmaku'

export default function Post({ gv }) {
const [id, fp] = getAv(gv)
Expand All @@ -15,6 +16,7 @@ export default function Post({ gv }) {
const [play, setPlay] = useState("")
const [show, setShow] = useState(0)
const [idx, setId] = useState(fp - 1)
const [danmakus, setDanmakus] = useState([])

useEffect(() => {
const p1 = getPostDetail(id)
Expand All @@ -30,6 +32,20 @@ export default function Post({ gv }) {

}, [])

useEffect(() => {
const canvas = document.querySelector('canvas')
const video = document.querySelector('e-player').shadowRoot.querySelector('video')

getDanmakus(id, idx).then(res => {
setDanmakus((res as any).danmakus || [])
console.log((res as any).danmakus)
let dm = new Danmu(canvas, video, (res as any).danmakus)
dm.play()
})


}, [])


const changeid = (i) => {
setPlay(videos[i][1])
Expand All @@ -39,7 +55,7 @@ export default function Post({ gv }) {
return (
<div class="wrap player">
<div className="ep-wrap">
<Eplayer url={play} live={post.sort === '推流'}></Eplayer>
<Eplayer url={play}></Eplayer>
</div>
<div className="p">
<div className="info">
Expand Down Expand Up @@ -100,14 +116,14 @@ export function Eplayer(props) {
if (t.current) {
t.current.setAttribute('type', type)
t.current.setAttribute('src', res.result.url)
if (props.live) {
t.current.shadowRoot.querySelector('.progress').style.display = 'none'
}
}
})
}, [props.url])

return (
<e-player ref={t} class='ep' />
<>
<canvas id="danmaku"></canvas>
<e-player ref={t} class='ep' />
</>
)
}

0 comments on commit 117e9a0

Please sign in to comment.