手持弹幕 字幕滚动小程序 全屏显示

标题## 手持弹幕 字幕滚动小程序

小程序能做到真正的全屏显示

很早之前看到大家都在使用字幕滚动小程序,自己也做了一款。功能还比较简单,不过已经上线了。在这里插入图片描述小程序目前有功能为:
1、字幕全屏滚动;
2、改变文字大小颜色;
3、移动速度;
4、背景颜色。

字幕文本字体改变功能还没有实现,抽时间在做做。后续会增加一些模板样式,让字体、背景样式选择更多。

让字幕滚动的关键代码:
       data: {
    animationData: {}
  },

  onReady: function() {
    const query = wx.createSelectorQuery();
    query.select('.container').boundingClientRect();
    query.select('.scroll-content').boundingClientRect();
    query.exec((res) => {
      const containerWidth = res[0].width;    //字幕文本宽度
      const contentWidth = res[1].width;       //为放文字容器宽度
      this.createAnimation(containerWidth, contentWidth);
    });
  },

  createAnimation: function(containerWidth, contentWidth) {
    const animation = wx.createAnimation({
      duration: 10000, // 动画持续时间
      timingFunction: 'linear',
      delay: 0,
      transformOrigin: '0 0 0'
    });

    const resetAnimation = wx.createAnimation({
      duration: 0, // 重置动画持续时间为 0,瞬间完成
      timingFunction: 'linear',
      delay: 0,
      transformOrigin: '0 0 0'
    });

    const animate = () => {
      // 重置位置
      resetAnimation.translateX(containerWidth).step();
      this.setData({
        animationData: resetAnimation.export()
      });

      // 确保重置动画完成后再开始滚动动画
      setTimeout(() => {
        // 滚动动画
        animation.translateX(-contentWidth).step();
        this.setData({
          animationData: animation.export()
        });

        // 在滚动动画完成后再调用animate函数
        setTimeout(animate, 10000); // 每10秒重新开始动画
      }, 50); // 50毫秒后开始滚动动画
    };

    animate(); // 初次调用animate函数,启动动画循环
   
  }
});

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ae60782607874702b10c6b8168b8a23b.jpeg#pic_center

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/1acc08e423e543ffb2c2a82032e23e89.jpeg#pic_center

在这里插入图片描述

以下是一个简单的微信小程序全屏弹幕墙代码示例: 1. 在 wxml 文件中添加 canvas 元素: ``` <canvas canvas-id="danmu" style="width: 100%; height: 100%;"></canvas> ``` 2. 在 js 文件中获取 canvas 对象并设置绘图上下文: ``` const ctx = wx.createCanvasContext('danmu'); ``` 3. 在页面加载时监听屏幕旋转事件并重新设置 canvas 大小: ``` wx.onWindowResize(() => { const res = wx.getSystemInfoSync(); canvasWidth = res.windowWidth; canvasHeight = res.windowHeight; canvasScale = res.pixelRatio; ctx.scale(canvasScale, canvasScale); ctx.canvas.width = canvasWidth * canvasScale; ctx.canvas.height = canvasHeight * canvasScale; }); ``` 4. 定义一个弹幕类 Danmu,包含弹幕的文字、颜色、位置、速度等属性,以及绘制弹幕的方法 draw: ``` class Danmu { constructor(text, color, speed) { this.text = text; this.color = color; this.x = canvasWidth; this.y = Math.random() * canvasHeight; this.speed = speed; } draw(ctx) { ctx.fillStyle = this.color; ctx.font = 'bold 20px sans-serif'; ctx.fillText(this.text, this.x, this.y); } move() { this.x -= this.speed; } } ``` 5. 在页面 onLoad 方法中初始化弹幕数组和计时器: ``` onLoad: function () { const danmus = []; setInterval(() => { danmus.push(new Danmu('Hello, world!', 'red', 5)); }, 1000); setInterval(() => { ctx.clearRect(0, 0, canvasWidth, canvasHeight); danmus.forEach((danmu) => { danmu.draw(ctx); danmu.move(); }); }, 1000 / 60); }, ``` 这样就实现了一个简单的微信小程序全屏弹幕墙。你可以根据需要调整弹幕的大小、颜色、速度等参数,以及添加更多的交互和动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值