canvas实现字符视频

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <style>
    .body {
      display: flex;
    }
    video {
      height: 600px;
      width: 300px;
    }
    #cvs1, #cvs2 {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <div class="body">
    <video src="./11.mp4" controls oncanplay="init()"></video>
    <canvas id="cvs1"></canvas>
    <canvas id="cvs2" onclick="video.play()"></canvas>
  </div>


  <script>
    const init = () => {
      const video = document.querySelector('video')
      const cvs1 = document.querySelector('#cvs1')
      const cvs2 = document.querySelector('#cvs2')
      const ctx1 = cvs1.getContext('2d')
      const ctx2 = cvs2.getContext('2d')
      cvs1.width = cvs2.width = video.offsetWidth
      cvs1.height = cvs2.height = video.offsetHeight
      const play = () => {
        requestAnimationFrame(play)
        ctx1.drawImage(video, 0, 0, cvs1.width, cvs1.height)
        const data = ctx1.getImageData(0, 0, cvs1.width, cvs1.height).data
        ctx2.clearRect(0, 0, cvs2.width, cvs2.height)
        const bl = 64
        for (let i = 0; i < data.length; i += 4 * bl) {
          const x = parseInt((i + 1) % (cvs2.width * 4) / 4)
          const y = parseInt(i / (cvs2.width * 4))
          const g = parseInt((data[i] + data[i+1] + data[i+2]) / 1.5)
          ctx2.fillStyle = `rgba(${g}, ${g}, ${g}, ${data[i+3]})`
          ctx2.fillText('+', x, y)
        }
      }
      play()
    }
  </script>
</body>

</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值