JavaScript实现弹幕功能

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
    <button>发送</button>
  </body>
  <script>
    let ipt = document.querySelector("input")
    let btn = document.querySelector("button")
  //  按下enter键发送弹幕
    ipt.onkeydown = function(e){
        if(e.keyCode == 13){
            let val = ipt.value;
            let span = document.createElement("span")
            span.innerHTML = val
            ipt.value = "";
            // 绝对定位
            span.style.position = "absolute"
            // 随机出现的位置
            span.style.left = document.documentElement.clientWidth + "px"
            // 随机出现的高度, 不能超出屏幕的高度, 也就是不能超出body的高度, 也就是不能超出documentElement的高度
            span.style.top = Math.floor(Math.random()*document.documentElement.clientHeight) + "px"
            // 插入到body中, 但是在第一个元素之前, 也就是在body中的第一个元素之前
            document.body.prepend(span)

            let timer = setInterval(()=>{
                span.style.left = parseInt(span.style.left) - 10 + "px"
                if(parseInt(span.style.left)<-1*span.offsetWidth){
                    span.remove()
                    clearInterval(timer)
                }
            },50)
        }
    }

    // btn.onclick = function(){
    //     let val = ipt.value;
    //     let span = document.createElement("span")
    //     span.innerHTML = val
    //     ipt.value = "";
    //     span.style.position = "absolute"
    //     span.style.left = document.documentElement.clientWidth + "px"
    //     span.style.top = Math.floor(Math.random()*document.documentElement.clientHeight) + "px"
    //     document.body.prepend(span)

    //     let timer = setInterval(()=>{
    //         span.style.left = parseInt(span.style.left) - 10 + "px"
    //         if(parseInt(span.style.left)<-1*span.offsetWidth){
    //             span.remove()
    //             clearInterval(timer)
    //         }
    //     },50)
       

    // }
  </script>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WebSocket技术可以用来实现实时弹幕功能。以下是一个简单的WebSocket弹幕示例: 1. 前端代码 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Websocket弹幕</title> <style> #barrage { position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none; } .barrage-item { position: absolute; top: 0; white-space: nowrap; font-size: 20px; color: white; text-shadow: 1px 1px 2px black; } </style> </head> <body> <canvas id="barrage"></canvas> <script> const canvas = document.getElementById('barrage'); const ctx = canvas.getContext('2d'); const width = canvas.width = window.innerWidth; const height = canvas.height = window.innerHeight; let barrageList = []; // 连接websocket服务器 const ws = new WebSocket('ws://localhost:8080'); ws.onmessage = (event) => { const data = JSON.parse(event.data); barrageList.push({ text: data.text, x: width, y: Math.random() * height, color: data.color }); }; function draw() { ctx.clearRect(0, 0, width, height); for (let i = 0; i < barrageList.length; i++) { const item = barrageList[i]; ctx.fillStyle = item.color; ctx.fillText(item.text, item.x, item.y); item.x -= 3; } barrageList = barrageList.filter(item => item.x > -ctx.measureText(item.text).width); requestAnimationFrame(draw); } draw(); </script> </body> </html> ``` 2. 后端代码 ```javascript const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { console.log('connected'); ws.on('message', (message) => { console.log(`received: ${message}`); wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(message); } }); }); }); ``` 3. 使用方法 用户在前端页面上输入文本和颜色,点击发送按钮后,将文本和颜色发送给后端WebSocket服务器,服务器接收到消息后将消息广播给所有连接的客户端,客户端接收到消息后将消息添加到弹幕列表中,并在画布上绘制弹幕。 这是一个最基本的弹幕实现,实际中还需要考虑弹幕的速度、字体大小、弹幕屏蔽等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lionliu0519

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值