• Vue3实现粒子动态背景


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

    npm: https://www.npmjs.com/package/particles.vue3

    安装

    1. pnpm add particles.vue3
    2. pnpm add tsparticles-slim

    注册

    main.js 

    1. import { createApp } from 'vue'
    2. import type { App } from 'vue'
    3. import globleApp from './App.vue'
    4. import router from './router'
    5. import Particles from "particles.vue3"
    6. const app: App = createApp(globleApp)
    7. app.use(router)
    8. .use(Particles)
    9. .mount('#app')

    引入使用,完整代码 

    1. <script setup lang="ts">
    2. import particlesOpt from './config/particles1'
    3. import { loadSlim } from "tsparticles-slim"
    4. import { computed } from 'vue'
    5. const props = withDefaults(defineProps<{
    6. width?: string | number,
    7. height?: string | number,
    8. backgroundColor?: string,
    9. backgroundImage?: string
    10. }>(), {
    11. width: '100%',
    12. height: '100%',
    13. backgroundColor: '#002a3a',
    14. backgroundImage: ''
    15. })
    16. const particlesContainerStyle = computed(() => {
    17. return {
    18. width: typeof props.width === 'string' ? props.width : props.width + 'px',
    19. height: typeof props.height === 'string' ? props.height : props.height + 'px',
    20. backgroundColor: props.backgroundColor,
    21. backgroundImage: `url(${props.backgroundImage})`
    22. }
    23. })
    24. const particlesInit = async (engine: any) => {
    25. await loadSlim(engine);
    26. }
    27. script>
    28. <style scoped>
    29. .wft-particles-container {
    30. display: flex;
    31. justify-content: center;
    32. align-items: center;
    33. position: relative;
    34. background-size: 100% 100%;
    35. background-repeat: no-repeat;
    36. }
    37. #wft-tsparticles {
    38. position: absolute;
    39. left: 0;
    40. top: 0;
    41. width: 100%;
    42. height: 100%;
    43. z-index: 0;
    44. }
    45. style>

    其中的options配置: 

    可以在同级新建config文件夹,新建particles1.ts, particles2.ts,里面定义配置项数据并向外导出

    ① 

    1. export default {
    2. // background: {
    3. // color: {
    4. // value: '#002a3a'
    5. // }
    6. // },
    7. fpsLimit: 60,
    8. interactivity: {
    9. detectsOn: 'canvas',
    10. events: {
    11. onClick: { // 开启鼠标点击的效果
    12. enable: true,
    13. mode: 'push'
    14. },
    15. onHover: { // 开启鼠标悬浮的效果(线条跟着鼠标移动)
    16. enable: true,
    17. mode: 'grab'
    18. },
    19. resize: true
    20. },
    21. modes: { // 配置动画效果
    22. bubble: {
    23. distance: 400,
    24. duration: 2,
    25. opacity: 0.8,
    26. size: 40
    27. },
    28. push: {
    29. quantity: 4
    30. },
    31. grab: {
    32. distance: 200,
    33. duration: 0.4
    34. },
    35. attract: { // 鼠标悬浮时,集中于一点,鼠标移开时释放产生涟漪效果
    36. distance: 200,
    37. duration: 0.4,
    38. factor: 5
    39. }
    40. }
    41. },
    42. particles: {
    43. color: {
    44. value: '#6AECFF' // 粒子点的颜色
    45. },
    46. links: {
    47. color: '#6AECFF', // 线条颜色
    48. distance: 150,
    49. enable: true,
    50. opacity: 0.5, // 不透明度
    51. width: 2 // 线条宽度
    52. },
    53. collisions: {
    54. enable: true
    55. },
    56. move: {
    57. attract: { enable: false, rotateX: 600, rotateY: 1200 },
    58. bounce: false,
    59. direction: 'none',
    60. enable: true,
    61. out_mode: 'out',
    62. random: false,
    63. speed: 1, // 移动速度
    64. straight: false
    65. },
    66. number: {
    67. density: {
    68. enable: true,
    69. value_area: 800
    70. },
    71. value: 80
    72. },
    73. opacity: {
    74. value: 0.5
    75. },
    76. shape: {
    77. type: 'circle'
    78. },
    79. size: {
    80. random: true,
    81. value: 5
    82. }
    83. },
    84. detectRetina: true
    85. }

    ② 

    1. export default {
    2. // background: {
    3. // color: {
    4. // value: '#0d47a1'
    5. // }
    6. // },
    7. fpsLimit: 120,
    8. interactivity: {
    9. events: {
    10. onClick: {
    11. enable: true,
    12. mode: 'push'
    13. },
    14. onHover: {
    15. enable: true,
    16. mode: 'repulse'
    17. },
    18. resize: true
    19. },
    20. modes: {
    21. bubble: {
    22. distance: 400,
    23. duration: 2,
    24. opacity: 0.8,
    25. size: 40
    26. },
    27. push: {
    28. quantity: 4
    29. },
    30. repulse: {
    31. distance: 200,
    32. duration: 0.4
    33. }
    34. }
    35. },
    36. particles: {
    37. color: {
    38. value: '#ffffff'
    39. },
    40. links: {
    41. color: '#ffffff',
    42. distance: 150,
    43. enable: true,
    44. opacity: 0.5,
    45. width: 1
    46. },
    47. collisions: {
    48. enable: true
    49. },
    50. move: {
    51. direction: 'none',
    52. enable: true,
    53. outModes: {
    54. default: 'bounce'
    55. },
    56. random: false,
    57. speed: 6,
    58. straight: false
    59. },
    60. number: {
    61. density: {
    62. enable: true,
    63. area: 800
    64. },
    65. value: 80
    66. },
    67. opacity: {
    68. value: 0.5
    69. },
    70. shape: {
    71. type: 'circle'
    72. },
    73. size: {
    74. value: { min: 1, max: 5 },
    75. }
    76. },
    77. detectRetina: true
    78. }

    效果: 

  • 相关阅读:
    【AI+医疗】AI在医疗影像设备工作周期中的应用探索
    CMake
    VLAN 实验
    如何在微信小程序中实现音视频通话
    [Google DeepMind] LARGE LANGUAGE MODELS AS OPTIMIZERS
    Docker Hub
    2000-2018年各省能源消费和碳排放数据
    图片地址GPS经纬度查询
    AI绘画使用Stable Diffusion(SDXL)绘制玉雕风格的龙
    2. 两数相加
  • 原文地址:https://blog.csdn.net/m0_51431448/article/details/134549667