canvas+css写轮眼动画效果

我这里思路就是先用canvas画轮廓眼球,勾玉等再用css animation实现动画

完整代码

<!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>canvas写轮眼</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            position: relative;
            background-image: url("bg.jpg");
            background-repeat: no-repeat;
            background-size: 100% 100%; 
            z-index: -100;
        }
        .container{
            width: 600px;
            height: 200px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: absolute;
            top: 20%;
            left: 34%;
        }
        .leftBox,
        .rightBox {
            display: inline-block;
            position: relative;
            width: 200px;
            height: 100px;
        }

        #canvas1,
        #canvas1_right {
            position: absolute;
            z-index: -1;
        }

        /* 中间黑眼球 */
        .small_arc,
        .small_arc_right {
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 90px;
            height: 90px;
            border-radius: 50%;
            background-color: rgb(0, 0, 0);
            border: 1px solid #000;
            z-index: 0;
            animation: colors 3s linear 0s 1 normal forwards;
        }

        @keyframes colors {
            0% {
                background-color: rgb(0, 0, 0);
            }

            100% {
                background-color: rgb(255, 4, 4);
            }
        }

        /* 眼睛小白点 */
        .small_arc::after,
        .small_arc_right::after {
            content: "";
            position: absolute;
            left: -48px;
            right: 0;
            top: -27px;
            bottom: 0;
            margin: auto;
            width: 13px;
            height: 13px;
            border-radius: 50%;
            background-color: rgb(255, 255, 255);
            border: 1px solid #000;
            z-index: 4;
        }

        #yu,
        #yu_right {
            z-index: 3;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            transform: rotate(240deg);
            animation: yu 3s linear 0s 1 forwards;
        }

        #yu1,
        #yu1_right {
            z-index: 3;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            transform: rotate(240deg);
            animation: yu1 3s linear 0s 1 forwards;
        }

        #yu2,
        #yu2_right {
            z-index: 3;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            transform: rotate(240deg);
            animation: yu2 3s linear 0s 1 forwards;
        }

        @keyframes yu {
            0% {
                width: 0;
                height: 0;
                transform: rotate(0);
            }

            100% {
                width: 200px;
                height: 100px;
                transform: rotate(3turn);
            }
        }

        @keyframes yu1 {
            0% {
                width: 0;
                height: 0;
                transform: rotate(120deg);
            }

            100% {
                width: 200px;
                height: 100px;
                transform: rotate(1200deg);
            }
        }

        @keyframes yu2 {
            0% {
                width: 0;
                height: 0;
                transform: rotate(240deg);
            }

            100% {
                width: 200px;
                height: 100px;
                transform: rotate(1320deg);
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 左眼 -->
        <div class="leftBox">
            <!-- 眼睛轮廓 -->
            <canvas id="canvas1"></canvas>
            <!-- 中间眼球 -->
            <div class="small_arc"></div>
            <!-- 三勾玉 -->
            <canvas id="yu"></canvas>
            <canvas id="yu1"></canvas>
            <canvas id="yu2"></canvas>
        </div>

        <!-- 右眼 -->
        <div class="rightBox">
            <!-- 眼睛轮廓 -->
            <canvas id="canvas1_right"></canvas>
            <!-- 中间眼球 -->
            <div class="small_arc_right"></div>
            <!-- 三勾玉 -->
            <canvas id="yu_right"></canvas>
            <canvas id="yu1_right"></canvas>
            <canvas id="yu2_right"></canvas>
        </div>
    </div>
    <script>
        window.onload = function () {
            drawLeft();
            drawRight();
        }
        function drawLeft() {
            let canvas1 = document.getElementById("canvas1");
            let yu = document.getElementById("yu");
            let yu1 = document.getElementById("yu1");
            let yu2 = document.getElementById("yu2");
            let ctx1 = canvas1.getContext("2d");
            let ctx2 = yu.getContext("2d");
            let ctx3 = yu1.getContext("2d");
            let ctx4 = yu2.getContext("2d");

            let ctxList = [ctx2, ctx3, ctx4];
            let obj = {
                width: 200,
                height: 100,
                num: 28,
            }
            canvas1.width = obj.width;
            canvas1.height = obj.height;
            yu.width = obj.width;
            yu.height = obj.height;
            yu1.width = obj.width;
            yu1.height = obj.height;
            yu2.width = obj.width;
            yu2.height = obj.height;

            // 开始绘制眼睛轮廓
            ctx1.beginPath();
            ctx1.moveTo(0, 0);
            ctx1.lineTo(90, 0);
            ctx1.quadraticCurveTo(180, 0, 200, 100);
            ctx1.lineTo(90, 100);
            ctx1.quadraticCurveTo(20, 100, 0, 0);
            ctx1.lineWidth = 1;
            ctx1.lineJoin = "round";
            ctx1.fillStyle = "white";
            ctx1.fill();
            ctx1.stroke();

            // 重复绘制3个勾玉
            for (let i = 0; i < ctxList.length; i++) {
                let item = ctxList[i];
                // 绘制万花筒
                item.beginPath();
                item.arc(100, 50, obj.num, 0, 2 * 3.14, false);
                item.fillStyle = "black";
                item.stroke();
                // 绘制勾玉
                item.beginPath();
                item.arc(100, 23, 7, 0, 2 * 3.14, false);
                item.fillStyle = "black";
                item.fill();
                item.stroke();
                // 绘制小角
                item.beginPath();
                item.moveTo(92, 23);
                item.quadraticCurveTo(100, 5, 92 + 14, 23 - 15);
                item.quadraticCurveTo(102, 11, 102, 18);
                item.fill();
                item.stroke();
            }
        }
        function drawRight() {
            let canvas1 = document.getElementById("canvas1_right");
            let yu = document.getElementById("yu_right");
            let yu1 = document.getElementById("yu1_right");
            let yu2 = document.getElementById("yu2_right");
            let ctx1 = canvas1.getContext("2d");
            let ctx2 = yu.getContext("2d");
            let ctx3 = yu1.getContext("2d");
            let ctx4 = yu2.getContext("2d");

            let ctxList = [ctx2, ctx3, ctx4];
            let obj = {
                width: 200,
                height: 100,
                num: 28,
            }
            canvas1.width = obj.width;
            canvas1.height = obj.height;
            yu.width = obj.width;
            yu.height = obj.height;
            yu1.width = obj.width;
            yu1.height = obj.height;
            yu2.width = obj.width;
            yu2.height = obj.height;

            // 开始绘制眼睛轮廓
            ctx1.beginPath();
            ctx1.moveTo(200, 0);
            ctx1.lineTo(110, 0);
            ctx1.quadraticCurveTo(20, 0, 0, 100);
            ctx1.lineTo(110, 100);
            ctx1.quadraticCurveTo(180, 100, 200, 0);
            ctx1.lineWidth = 1;
            ctx1.lineJoin = "round";
            ctx1.fillStyle = "white";
            ctx1.fill();
            ctx1.stroke();

            // 重复绘制3个勾玉
            for (let i = 0; i < ctxList.length; i++) {
                let item = ctxList[i];
                // 绘制万花筒
                item.beginPath();
                item.arc(100, 50, obj.num, 0, 2 * 3.14, false);
                item.fillStyle = "black";
                item.stroke();
                // 绘制勾玉
                item.beginPath();
                item.arc(100, 23, 7, 0, 2 * 3.14, false);
                item.fillStyle = "black";
                item.fill();
                item.stroke();
                // 绘制小角
                item.beginPath();
                item.moveTo(92, 23);
                item.quadraticCurveTo(100, 5, 92 + 14, 23 - 15);
                item.quadraticCurveTo(102, 11, 102, 18);
                item.fill();
                item.stroke();
            }
        }
    </script>
</body>

</html>

 背景图 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阁下呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值