【vue 实现代码舞】

14 篇文章 1 订阅
5 篇文章 1 订阅

项目演示
github地址
gitee地址

在这里插入图片描述
在这里插入图片描述

代码
直接替换视频资源就行了

<!--
 * @name: name
 * @description: Description * @lastEditors: Francis
 * @date: Do not edit
 * @lastEditTime: Do not edit
 * @filePath: Do not edit
-->

<template>
    <div class="content container">
        <div class="flex ">
            <div>
                <video ref="videoRef" style="width:324px;height:570;" :muted="true" :autoPlay="true" :preload="true"
                    loop :x5-video-player-fullscreen="true" :x5-playsinline="true" playsInline
                    :webkit-playsinline="true" crossOrigin="anonymous">
                    <source src="../../assets/video/video.mp4">

                </video>
            </div>
            <canvas ref="canvasRef" crossOrigin="anonymous" width="324" height="570"></canvas>
        </div>
    </div>
</template>
<script setup>
    import {
        ref,
    } from 'vue'

    const canvasWidth = 324;
    const canvasHeight = 570;
    const videoRef = ref(null)
    const canvasRef = ref(null)
    const ctxRef = ref(null)
    const frameId = ref(null)

    const init = () => {
        ctxRef.value = canvasRef.value.getContext("2d", {
            willReadFrequently: true,
        })
    }
    const play = () => {
        if (videoRef.value && ctxRef.value) {
            ctxRef.value.drawImage(
                videoRef.value,
                0,
                0,
                canvasWidth,
                canvasHeight
            );
            const imageData = ctxRef.value.getImageData(
                0,
                0,
                canvasWidth,
                canvasHeight
            );
            ctxRef.value.clearRect(0, 0, canvasWidth, canvasHeight);
            const {
                data,
                height,
                width,
            } = imageData;
            for (let y = 0; y < height; y++) {
                for (let x = 0; x < width; x++) {
                    const startIndex = (y * width + x) * 4;
                    if (x % 5 === 0 && y % 5 === 0) {
                        const avgColor =
                            (data[startIndex] + data[startIndex + 1] + data[startIndex + 2]) / 3
                        ctxRef.value.fillStyle = `rgb(${avgColor}, ${avgColor}, ${avgColor})`;
                        ctxRef.value.font = "10px Arial";
                        ctxRef.value.fillText("8", x, y);
                    }
                }
            }

            setTimeout(() => {
                play()
            }, 60)
            // frameId.value= window.requestAnimationFrame(play());
            // window.cancelAnimationFrame(frameId.value)
        }
    };

    setTimeout(() => {
            init()
            play()
            //  cancelAnimationFrame(frameId.value);
        }, 2000

    )
</script>
<style scoped lang="less">
    .container {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        position: relative;
        .tip {
            margin-top: 20px;
            color: #666;
        }
    }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值