canvas--随机粒子


  • 微信扫码关注公众号 :前端前端大前端,追求更精致的阅读体验 ,一起来学习啊
  • 关注后发送关键资料,免费获取一整套前端系统学习资料和老男孩python系列课程
    在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>canvas--随机粒子</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            width: 100%;
            height: 100%;
            background-color:#000;
        }
    </style>
</head>

<body>

    <canvas id='canvas'></canvas>

    <script>

        const canvas = document.getElementById("canvas");


        const ctx = canvas.getContext("2d");

        const WIDTH = document.documentElement.clientWidth;
        const HEIGHT = document.documentElement.clientHeight;

        canvas.width = WIDTH;
        canvas.height = HEIGHT;


        // 创建粒子对象 

        function Particle(x, y) {
            this.symbol = Symbol();//粒子标识 确保每个粒子不一样
            this.x = x;//x坐标
            this.y = y;//y坐标
            this.r = (Math.random() * 20) + 1;//半径
            let cr = Math.random() * 256;
            let cg = Math.random() * 256;
            let cb = Math.random() * 256;
            let alpha = (Math.floor(Math.random() * 10) + 1) / 10; //0.05-0.5 透明度--为了好看
            this.color = `rgba(${cr},${cg},${cb},${alpha})`;//颜色 
        }

        //Particle原型上添加绘制方法 draw

        Particle.prototype.draw = function () {

            //开始新路径
            ctx.beginPath();
            //画⚪ x,y坐标 r半径 起始弧度 结束弧度
            ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
            //闭合路径
            ctx.closePath();
            //填充样式
            ctx.fillStyle = this.color;
            //模糊程度
            ctx.shadowBlur = this.r * 2;
            //填充
            ctx.fill();

        }



        // 通过改变粒子纵坐标位置 模拟粒子升降
        Particle.prototype.move = function (step=1.5) {
            this.y -= step;//运动速度
            //看不见了就再来一次
            if (this.y <= -10) {
                this.y = HEIGHT + 10;
            }
            //重绘
            this.draw();
        }
        let arr = [];

        function init(len = 100) {

            for (let i = 0; i < len; i++) {
                //填充粒子数组
                arr[i] = new Particle(Math.random() * WIDTH, Math.random() * HEIGHT);
                //批量绘制
                arr[i].draw();
            }
            animate();
        }


        // 粒子一起动起来
        function animate() {

            //每次运动前清空画布
            ctx.clearRect(0, 0, WIDTH, HEIGHT);

            arr.forEach(item => {

                item.move();

            })

            requestAnimationFrame(animate);
        }


        init();



    </script>
</body>

</html>

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的3D粒子爱心代码HTML源码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>3D粒子爱心效果</title> <style> body { margin: 0; padding: 0; overflow: hidden; background-color: #000; } canvas { display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var W = canvas.width = window.innerWidth; var H = canvas.height = window.innerHeight; var particleCount = 200; var particles = []; function Particle() { this.x = Math.random() * W; this.y = Math.random() * H; this.vx = Math.random() * 20 - 10; this.vy = Math.random() * 20 - 10; this.gravity = 0.3; this.alpha = Math.random(); this.color = 'rgb(' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ')'; } Particle.prototype.draw = function() { ctx.globalAlpha = this.alpha; ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x, this.y, 5, 0, Math.PI * 2, false); ctx.fill(); }; function init() { for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function update() { ctx.clearRect(0, 0, W, H); for (var i = 0; i < particleCount; i++) { var p = particles[i]; p.vy += p.gravity; p.x += p.vx; p.y += p.vy; if (p.x > W || p.x < 0 || p.y > H || p.y < 0) { particles[i] = new Particle(); } p.draw(); } } init(); setInterval(update, 1000 / 60); </script> </body> </html> ``` 这个代码使用了HTML5的Canvas标签和JavaScript来实现3D粒子爱心效果。它通过在Canvas上绘制大量的带有随机颜色和透明度的小圆点来模拟出一个3D的爱心效果。通过改变圆点的位置和速度,可以让它们在屏幕上自由运动,从而创造出动态的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值