canvas实现聚光灯效果(js)

效果展示:

源码展示:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>canvas实现聚光灯效果(js)</title>
</head>
<body>
<canvas id="canvas" width="533" height="300" ></canvas>

<script>
    class Lamp {
        /**
         *
         * @param canvas_id canvas元素id
         * @param img       图片路径
         * @param cover_color 遮盖颜色,与cxt.fillStyle一致
         * @param radius    显示圆环大小
         */
        constructor(canvas_id, img, cover_color, radius) {
            //基本属性
            this.img = document.createElement("img");
            this.img.src = img;
            this.ctx = document.getElementById(canvas_id).getContext("2d");
            this.canvas = this.ctx.canvas;

            this.hideC1 = document.createElement("canvas").getContext("2d");

            this.overColor = cover_color;
            this.radius = radius;
            this.x = 0;
            this.y = 0;

            this.img.onload = () => this.init();
        }

        init() {
            //初始化画布
            this.ctx.fillStyle = this.overColor;
            this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
            this.overImgData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);

            //初始化C1 C2并绑定监听事件
            this.initHideC1();
            this.canvas.addEventListener('mousemove', (event) => this.mouseMove(event));
            this.canvas.addEventListener('mouseout', (event) => this.mouseOut(event));
        }
        //初始化c1
        initHideC1() {
            let t = this.hideC1.canvas;
            t.width = this.canvas.width;
            t.height = this.canvas.height;
            this.hideC1.drawImage(this.img, 0, 0, this.img.width, this.img.height);
            return this;
        }

        //鼠标在画布上移动
        mouseMove(e) {
            let t = this.canvas.getBoundingClientRect();
            this.x = e.clientX - t.left - this.radius;
            this.y = e.clientY - t.top - this.radius;
            this.light();
            return this;
        }

        //鼠标移除
        mouseOut() {
            this.ctx.putImageData(this.overImgData, 0, 0);
            return this;
        }


        //添加聚光灯效果
        light() {
            this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
            this.ctx.putImageData(this.overImgData, 0, 0);

            this.ctx.beginPath();
            this.ctx.arc(this.x + this.radius, this.y + this.radius, this.radius, 0, Math.PI * 2);
            this.ctx.fillStyle = this.ctx.createPattern(this.hideC1.canvas, 'no-repeat');
            this.ctx.fill();
        }
    }
    new Lamp('canvas', "a.jpg", 'rgba(45,0,0,0.7)', 100);
</script>
<hr>
<pre style="color:red">
 感:  最近贡献一下我在教学中的小案例可以能给你一些帮助 ,希望继续关注我的博客

                                                                               --王
</pre>


</body>
</html>

 

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HTML5中的Canvas元素和JavaScript来实现写字动画效果。具体实现步骤如下: 1. 创建一个Canvas元素,并设置它的宽度和高度。 2. 使用JavaScript获取Canvas的上下文对象。 3. 设置字体、颜色、阴影等样式属性。 4. 使用Canvas的beginPath()方法开始绘制路径。 5. 使用Canvas的moveTo()和lineTo()方法绘制字母路径。 6. 使用Canvas的stroke()方法绘制路径。 7. 使用JavaScript的定时器函数setInterval()不断更新字母的位置,从而实现动画效果。 下面是一个简单的示例代码: HTML代码: ```html <canvas id="myCanvas" width="500" height="200"></canvas> ``` JavaScript代码: ```javascript // 获取Canvas元素的上下文对象 var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); // 设置字体属性 ctx.font = "80px Arial"; ctx.fillStyle = "red"; ctx.shadowColor = "black"; ctx.shadowBlur = 10; // 定义字母路径 ctx.beginPath(); ctx.moveTo(50, 100); ctx.lineTo(100, 50); ctx.lineTo(150, 100); ctx.lineTo(200, 50); ctx.lineTo(250, 100); ctx.lineTo(250, 150); ctx.stroke(); // 定义定时器函数来更新字母位置 var x = 50; var y = 100; var dx = 5; var dy = -5; setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x+50, y-50); ctx.lineTo(x+100, y); ctx.lineTo(x+150, y-50); ctx.lineTo(x+200, y); ctx.lineTo(x+200, y+50); ctx.stroke(); x += dx; y += dy; if (x > canvas.width-200 || x < 0) { dx = -dx; } if (y > canvas.height-50 || y < 0) { dy = -dy; } }, 10); ``` 这段代码实现了一个不断移动的字母路径,可以根据需要修改字母的形状和动画效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值