网站背景跟随鼠标移动的粒子线条

//在公用页内引入这行js即可实现

<script src="//cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js"></script>

//如果要改变颜色(RGB),透明度,层级,线条数量,直接在<script>内写即可

<script type="text/javascript" color="0,0,255" opacity='0.7' zIndex="-2" count="99" src="//cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js"></script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的js代码实现跟随鼠标粒子效果: HTML: ```html <canvas id="canvas"></canvas> ``` CSS: ```css #canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } ``` JS: ```javascript var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var particles = []; var mouse = { x: undefined, y: undefined } window.addEventListener("mousemove", function(event) { mouse.x = event.x; mouse.y = event.y; }); function Particle(x, y, radius, color) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.radians = Math.random() * Math.PI * 2; this.velocity = 0.05; this.distanceFromCenter = Math.random() * 50 + 100; this.lastMouse = {x: x, y: y}; this.draw = function(lastPoint) { ctx.beginPath(); ctx.strokeStyle = this.color; ctx.lineWidth = this.radius; ctx.moveTo(lastPoint.x, lastPoint.y); ctx.lineTo(this.x, this.y); ctx.stroke(); ctx.closePath(); } this.update = function() { // 记录上一帧的鼠标位置 this.lastMouse.x += (mouse.x - this.lastMouse.x) * 0.05; this.lastMouse.y += (mouse.y - this.lastMouse.y) * 0.05; // 运动轨迹 this.radians += this.velocity; this.x = this.lastMouse.x + Math.cos(this.radians) * this.distanceFromCenter; this.y = this.lastMouse.y + Math.sin(this.radians) * this.distanceFromCenter; this.draw(this.lastMouse); } } function init() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; for (var i = 0; i < 50; i++) { var radius = Math.random() * 3 + 1; var color = "rgba(255, 255, 255, 0.5)"; particles.push(new Particle(canvas.width / 2, canvas.height / 2, radius, color)); } } function animate() { requestAnimationFrame(animate); ctx.fillStyle = "rgba(0, 0, 0, 0.1)"; ctx.fillRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].update(); } } init(); animate(); ``` 这段代码创建了一个画布,并且在鼠标移动跟随鼠标移动粒子效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhenxing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值