用canvas画跟随鼠标事件

//鼠标移动 展现光片

在这里插入图片描述

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title></title>
	<style>
		body {
			margin: 0;
			overflow: hidden;
		}

		#canvas {
			background: #000;
		}
	</style>
</head>

<body>
	<canvas id="canvas"></canvas>
	<script>
		var canvas = document.getElementById('canvas');
		var context = canvas.getContext('2d');
		var circleList = [];

		canvas.width = window.innerWidth;
		canvas.height = window.innerHeight;

		canvas.addEventListener('mousemove', function (e) {
			// 将对象push到数组中,画出来的彩色小点可以看作每一个对象中记录着信息 然后存在数组中
			circleList.push(new Circle(e.clientX, e.clientY));
		})

		//取x到y之间随机数:Math.round(Math.random()*(y-x)+x)	包括y
		function random(min, max) {
			return Math.round(Math.random() * (max - min) + min);
		}

		function Circle(x, y) {
			this.x = x;
			this.y = y;

			this.vx = (Math.random() - 0.5) * 3;	//随机出来一个正数,或者负数。乘3是为了让速度变得大一点
			this.vy = (Math.random() - 0.5) * 3;

			this.color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) + ')';

			this.a = 1;	// 初始透明度

			this.draw();
		}
		Circle.prototype = {
			draw() {
				context.beginPath();
				context.fillStyle = this.color;
				context.globalCompositeOperation = 'lighter';
				context.globalAlpha = this.a;	//全局透明度
				context.arc(this.x, this.y, 30, 0, Math.PI * 2, false);
				context.fill();
				this.update();
			},
			update() {
				// 根据速度更新每一个小圆的位置
				this.x += this.vx;
				this.y += this.vy;
				this.a *= 0.98;
			}
		}

		function render() {
			//把原来的内容区域清除掉
			context.clearRect(0, 0, canvas.width, canvas.height);
			circleList.forEach(function (ele, i) {
				ele.draw();

				if (ele.a < 0.05) {
					circleList.splice(i, 1);
				}
			});

			requestAnimationFrame(render);	//动画,会根据浏览器的刷新频率更新动画
		}
		render();
	</script>
</body>

</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是基于Canvas实现文字跟随鼠标在钦慕上放大缩小的代码: ```javascript // 获取布和笔 const canvas = document.querySelector('canvas'); const ctx = canvas.getContext('2d'); // 设置布大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 设置文字样式 ctx.font = 'bold 50px Arial'; ctx.fillStyle = 'red'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; // 定义文字和初始大小 let text = 'Hello World!'; let size = 50; // 监听鼠标移动事件 canvas.addEventListener('mousemove', function(event) { // 获取鼠标位置 const mouseX = event.clientX; const mouseY = event.clientY; // 清除布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 设置字体大小 ctx.font = `bold ${size}px Arial`; // 计算文字宽度和高度 const textWidth = ctx.measureText(text).width; const textHeight = size; // 绘制文字 ctx.fillText(text, mouseX, mouseY); // 判断是否需要改变字体大小 if (size >= 100 || size <= 30) { // 反转字体大小变化方向 sizeDir = -sizeDir; } // 改变字体大小 size += sizeDir; }); ``` 这段代码,我们首先获取了布和笔,然后设置了文字的样式和初始大小。接着监听了鼠标移动事件,在事件处理函数清除了布,然后根据鼠标位置绘制了文字。同时还计算了文字的宽度和高度,用于后续判断是否需要改变字体大小。最后判断是否需要改变字体大小,并修改字体大小和变化方向。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值