web实现酷炫的canvas粒子动画背景

文章目录

  • 前言
  • 一、particle-bg
    • [1. git地址:](#1. git地址:)
    • [2. 安装](#2. 安装)
    • [3. 使用](#3. 使用)
    • [4. 完整demo](#4. 完整demo)
  • 二、tsParticles
  • [1. 源码地址:](#1. 源码地址:)
    • [2. 安装](#2. 安装)
    • [3. 引入](#3. 引入)
    • [4. 使用](#4. 使用)
    • [5. 几个例子](#5. 几个例子)
      • [5.1 ts粒子五彩纸屑烟花](#5.1 ts粒子五彩纸屑烟花)
      • [5.2 多粒子产卵器-用tsParticles制作](#5.2 多粒子产卵器-用tsParticles制作)
      • [5.3 ts粒子鼠标吸引力](#5.3 ts粒子鼠标吸引力)
      • [5.4 粒子烟花](#5.4 粒子烟花)
  • 源码地址
  • 完结

前言

粒子动画,顾名思义,就是页面上存在大量的粒子构建而成的动画。

传统的粒子动画主要由 Canvas、WebGL 实现,我们经常用来用作网站的动画背景。

今天介绍两个个可以轻松创建高度可定制的粒子动画库。

一、particle-bg

1. git地址:

https://github.com/PengJiyuan/particle-bg

不带连线效果:

带连线的效果:

2. 安装

NPM

bash 复制代码
npm i particle-bg

CDN

html 复制代码
https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js

3. 使用

ES Module

javascript 复制代码
import particleBg from 'particle-bg';

particleBg('body');

Browser

html 复制代码
<script src="https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js"></script>
<script>
  particleBg('body');
</script>

API

javascript 复制代码
particleBg(element, config)

element
要插入粒子背景的DOM。

config [Object]
粒子背景的一些配置。

config.color
default: '#fff'
粒子的颜色。

config.count
default: 100
粒子的数量

config.radius
default: 2
粒子的半径

config.distance
default: width / 10
粒子间距小于多少会连线

config.rate
default: width / 10000
粒子运动的速率

config.zIndex
default: 1
canvas的z-index.

config.resize
default: true
是否监听window.resize,自动缩放粒子背景。

config.line
default: true
粒子之间是否连线。

config.bounce
default: false
是否触碰边界进行反弹。

4. 完整demo

html 复制代码
<div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="Bg"></div>
<script src="https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js"></script>
<script>
   particleBg('#Bg', {
        color: '#fff',
        count: 100,
        radius: 2,
        distance: 70,
        rate:1,
        zIndex: 1,
        resize: true,
        line: true,
        bounce: true,
    });
</script>

效果

二、tsParticles

TypeScript Particles 是在 particles.js 基础上重写的一个库,目的是更容易地创建更多的背景动画,并提供更多的实用程序和支持功能。

这个库最大的亮点在于它可以用于许多不同的框架,例如 React.js, Vue.js (2.x and 3.x), Angular, Svelte, jQuery等等JS框架、Web组件。

1. 源码地址:

GitHub地址:https://github.com/matteobruni/tsparticles

官网地址:https://particles.js.org/

更多demo地址:https://codepen.io/collection/DPOage

2. 安装

npm

bash 复制代码
npm install tsparticles-engine

yarn

bash 复制代码
yarn add tsparticles-engine

pnpm

bash 复制代码
pnpm install tsparticles-engine

3. 引入

从版本1.12.11开始使用import和require导入tsParticles了。

javascript 复制代码
const tsParticles = require("tsparticles-engine");
// or
import { tsParticles } from "tsparticles-engine";

4. 使用

index.html

html 复制代码
<div id="tsparticles"></div>

<script src="tsparticles.engine.min.js"></script>

app.js

javascript 复制代码
tsParticles
    .loadJSON("tsparticles", "presets/default.json")
    .then(container => {
        console.log("callback - tsparticles config loaded");
    })
    .catch(error => {
        console.error(error);
    });

5. 几个例子

5.1 ts粒子五彩纸屑烟花

html 复制代码
<script src="https://cdn.jsdelivr.net/npm/tsparticles-confetti@2.11.0/tsparticles.confetti.bundle.min.js"></script>
<script id="rendered-js">
    const duration = 60 * 60 * 1000,
        animationEnd = Date.now() + duration,
        defaults = { startVelocity: 30, spread: 360, ticks: 20, zIndex: 0 };
    function randomInRange(min, max) {
        return Math.random() * (max - min) + min;
    }
    const interval = setInterval(function () {
        const timeLeft = animationEnd - Date.now();

        if (timeLeft <= 0) {
            return clearInterval(interval);
        }
        const particleCount = 20 * (timeLeft / duration);

        // since particles fall down, start a bit higher than random
        confetti(
            Object.assign({}, defaults, {
                particleCount,
                origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 }
            }));
        confetti(
            Object.assign({}, defaults, {
                particleCount,
                origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 }
            }));
    }, 250);
</script>

效果

5.2 多粒子产卵器-用tsParticles制作

html 复制代码
<div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="tsparticles"></div>

<script src="https://cdn.jsdelivr.net/npm/tsparticles@1.19.0-alpha.3/dist/tsparticles.min.js"></script>
<script id="rendered-js">
    tsParticles.load("tsparticles", {
        fpsLimit: 60,
        particles: {
            number: {
                value: 0,
                density: {
                    enable: true,
                    value_area: 800
                }
            },


            color: {
                value: "#ffff00"
            },

            shape: {
                type: "circle"
            },

            opacity: {
                value: 1,
                random: false,
                animation: {
                    enable: true,
                    speed: 0.5,
                    minimumValue: 0,
                    sync: false
                }
            },


            size: {
                value: 8,
                random: { enable: true, minimumValue: 4 },
                animation: {
                    enable: false,
                    speed: 20,
                    minimumValue: 4,
                    sync: false
                }
            },


            move: {
                enable: true,
                gravity: {
                    enable: true,
                    acceleration: -0.5
                },

                speed: 5,
                direction: "top",
                random: false,
                straight: false,
                outModes: {
                    default: "destroy",
                    bottom: "none"
                },

                attract: {
                    enable: true,
                    distance: 300,
                    rotate: {
                        x: 600,
                        y: 1200
                    }
                }
            }
        },




        interactivity: {
            detectsOn: "canvas",
            events: {
                resize: true
            }
        },


        detectRetina: true,
        background: {
            color: "#000000"
        },

        emitters: [
            {
                direction: "top",
                particles: {
                    color: "#f00"
                },

                rate: {
                    quantity: 1,
                    delay: 0.1
                },

                size: {
                    width: 100,
                    height: 10
                },

                position: {
                    x: 50,
                    y: 100
                }
            },


            {
                direction: "top",
                particles: {
                    color: "#0f0"
                },

                rate: {
                    quantity: 1,
                    delay: 0.1
                },

                size: {
                    width: 100,
                    height: 10
                },

                position: {
                    x: 50,
                    y: 100
                }
            }]
    });
    //# sourceURL=pen.js
</script>

效果

5.3 ts粒子鼠标吸引力

html 复制代码
<div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="tsparticles1"></div>

<script src="https://cdn.jsdelivr.net/npm/tsparticles@1.37.4/tsparticles.min.js"></script>
<script id="rendered-js">
    tsParticles.load("tsparticles1", {
        fps_limit: 60,
        interactivity: {
            detect_on: "canvas",
            events: {
                onclick: { enable: true, mode: "push" },
                onhover: {
                    enable: true,
                    mode: "attract",
                    parallax: { enable: false, force: 60, smooth: 10 }
                },

                resize: true
            },

            modes: {
                push: { quantity: 4 },
                attract: { distance: 200, duration: 0.4, factor: 5 }
            }
        },


        particles: {
            color: { value: "#ffffff" },
            line_linked: {
                color: "#ffffff",
                distance: 150,
                enable: true,
                opacity: 0.4,
                width: 1
            },

            move: {
                attract: { enable: false, rotateX: 600, rotateY: 1200 },
                bounce: false,
                direction: "none",
                enable: true,
                out_mode: "out",
                random: false,
                speed: 2,
                straight: false
            },

            number: { density: { enable: true, value_area: 800 }, value: 80 },
            opacity: {
                anim: { enable: false, opacity_min: 0.1, speed: 1, sync: false },
                random: false,
                value: 0.5
            },

            shape: {
                character: {
                    fill: false,
                    font: "Verdana",
                    style: "",
                    value: "*",
                    weight: "400"
                },

                image: {
                    height: 100,
                    replace_color: true,
                    src: "images/github.svg",
                    width: 100
                },

                polygon: { nb_sides: 5 },
                stroke: { color: "#000000", width: 0 },
                type: "circle"
            },

            size: {
                anim: { enable: false, size_min: 0.1, speed: 40, sync: false },
                random: true,
                value: 5
            }
        },


        polygon: {
            draw: { enable: false, lineColor: "#ffffff", lineWidth: 0.5 },
            move: { radius: 10 },
            scale: 1,
            type: "none",
            url: ""
        },

        retina_detect: true
    });
</script>

效果

5.4 粒子烟花

html 复制代码
<script src="https://cdn.jsdelivr.net/npm/tsparticles-fireworks@2.11.0/tsparticles.fireworks.bundle.min.js"></script>
<script id="rendered-js">
   fireworks();
 </script>

效果

源码地址

https://gitcode.net/my12/particle

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇,https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,出于兴趣爱好,于是最近才开始自习unity。如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我可能也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~

相关推荐
oicola11 小时前
Ant Design v5 vs Tailwind CSS:组件库与样式框架的终极对决
前端·css
溪语流沙11 小时前
【Web全栈进阶】Vue Router + Pinia:SPA路由与全局状态
前端·javascript·vue.js
ManageEngineITSM11 小时前
工单优先级怎么定?影响×紧急度矩阵的设计与落地方法
java·大数据·运维·前端·数据库·变更管理
智商网输送线配件11 小时前
电子制造零散耗材补货实战:小批量混批采购平台怎么选才不停机?
大数据·服务器·前端·数据库·自动化
梦帮科技11 小时前
Web 漏洞攻防深度:WAF 规则逃逸技术(畸形分块传输、双重编码)与对抗加固
linux·前端·网络安全·ssl·web
兰亭妙微UI设计公司11 小时前
兰亭妙微软件界面设计公司的解锁按钮设计10大密码
开发语言·前端·javascript
迪幻12 小时前
封装自己的 npm 依赖并存储到个人 GitHub
前端·npm·github
u01110267512 小时前
WebP 转成 PNG 后为什么更大?图片格式转换不等于压缩
前端·javascript·vue.js
万少12 小时前
Qoder sonus模型 20分钟,复刻 马斯克 飞天汽车
前端·后端·算法
seconp12 小时前
基于Spring Boot和Vue 3的智慧旅游网站:住宿预订、行程规划与千问接入
vue.js·spring boot·毕业设计·软件工程·课程设计·旅游·毕设