首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >粒子js没有在反应js中应用道具

粒子js没有在反应js中应用道具
EN

Stack Overflow用户
提问于 2022-05-16 11:00:55
回答 1查看 224关注 0票数 0

我正在使用react js作为我的网站项目的背景,它是重叠的滚动条。无论我做什么,我都无法改变粒子js中画布的宽度。

这是我的粒子成分

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import React from 'react'
import Particles from 'react-tsparticles'
import styled from 'styled-components';
import { loadFull } from "tsparticles";

const Box = styled.div`
    position: absolute;
    top:0;
    right:0;
    left:0;
    bottom:0;
    z-index: 0;
`

const ParticleComponent = (props) => {

    const paramOne = {
        "particles": {
      "number": {
        "value": 100,
        "density": {
          "enable": true,
          "value_area": 800
        }
      },
      "color": {
        "value": props.theme.text
      },
      "shape": {
        "type": "circle",
        "stroke": {
          "width": 0,
          "color": props.theme.text
        },
        "polygon": {
          "nb_sides": 5
        },
        "image": {
          "src": "img/github.svg",
          "width": 100,
          "height": 100
        }
      },
      "opacity": {
        "value": 0.5,
        "random": false,
        "anim": {
          "enable": false,
          "speed": 1,
          "opacity_min": 0.1,
          "sync": false
        }
      },
      "size": {
        "value": 3,
        "random": true,
        "anim": {
          "enable": false,
          "speed": 20,
          "size_min": 0.1,
          "sync": false
        }
      },
      "line_linked": {
        "enable": true,
        "distance": 150,
        "color": props.theme.text,
        "opacity": 0.4,
        "width": 1
      },
      "move": {
        "enable": true,
        "speed": 3,
        "direction": "none",
        "random": false,
        "straight": false,
        "out_mode": "out",
        "outModes": {
          "default": "bounce"
        },
        "bounce": true,
        "attract": {
          "enable": false,
          "rotateX": 600,
          "rotateY": 1200
        }
      }
    },
    "interactivity": {
      "detect_on": "canvas",
      "events": {
        "onhover": {
          "enable": true,
          "mode": "repulse"
        },
        "onclick": {
          "enable": true,
          "mode": "push"
        },
        "resize": true
      },
      "modes": {
        "grab": {
          "distance": 400,
          "line_linked": {
            "opacity": 1
          }
        },
        "bubble": {
          "distance": 400,
          "size": 40,
          "duration": 2,
          "opacity": 8,
          "speed": 3
        },
        "repulse": {
          "distance": 100,
          "duration": 0.4
        },
        "push": {
          "particles_nb": 4
        },
        "remove": {
          "particles_nb": 2
        }
      }
    },
    "retina_detect": true
}

const paramTwo = {
    "particles": {
      "number": {
        "value": 100,
        "density": {
          "enable": true,
          "value_area": 800
        }
      },
      "color": {
        "value": props.theme.text
      },
      "shape": {
        "type": "circle",
        "stroke": {
          "width": 0,
          "color": props.theme.text
        },
        "polygon": {
          "nb_sides": 5
        },
        "image": {
          "src": "img/github.svg",
          "width": 100,
          "height": 100
        }
      },
      "opacity": {
        "value": 1,
        "random": true,
        "anim": {
          "enable": true,
          "speed": 1,
          "opacity_min": 0,
          "sync": false
        }
      },
      "size": {
        "value": 3,
        "random": true,
        "anim": {
          "enable": true,
          "speed": 3,
          "size_min": 0.4,
          "sync": false
        }
      },
      "line_linked": {
        "enable": false,
        "distance": 150,
        "color": props.theme.text,
        "opacity": 0.4,
        "width": 1
      },
      "move": {
        "enable": true,
        "speed": 3,
        "direction": "none",
        "random": true,
        "straight": false,
        "out_mode": "out",
        "outModes": {
          "default": "bounce"
        },
        "bounce": false,
        "attract": {
          "enable": false,
          "rotateX": 600,
          "rotateY": 600
        }
      }
    },
    "interactivity": {
      "detect_on": "canvas",
      "events": {
        "onhover": {
          "enable": false,
          "mode": "bubble"
        },
        "onclick": {
          "enable": true,
          "mode": "repulse"
        },
        "resize": true
      },
      "modes": {
        "grab": {
          "distance": 400,
          "line_linked": {
            "opacity": 1
          }
        },
        "bubble": {
          "distance": 250,
          "size": 0,
          "duration": 2,
          "opacity": 0,
          "speed": 3
        },
        "repulse": {
          "distance": 100,
          "duration": 0.4
        },
        "push": {
          "particles_nb": 4
        },
        "remove": {
          "particles_nb": 2
        }
      }
    },
    "retina_detect": true
}

const particlesInit = async (main) => {
    await loadFull(main);
};

const particlesLoaded = (container) => {
};

return (
    <Box>
        <Particles id="tsparticles" init={particlesInit} loaded={particlesLoaded} options= 
       {props.style === "one" ? paramOne : paramTwo   } />
    </Box>
)
}

export default ParticleComponent;

我把这个组件呈现成这样

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<ParticleComponent theme={props.theme} />

我试过像这样的内联风格

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<Box>
        <style>
            {
                `
                #tsparticles canvas {
                    width : ${document.body.clientWidth - 5}px !important;
                }
                `
            }
        </style>
        <Particles style={{width: '99%'}} id="tsparticles" init={particlesInit} loaded= 
       {particlesLoaded} options={props.style === "one" ? paramOne : paramTwo   } />
    </Box>

我试过用这种风格的道具

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<Particles style={{width: '99%'}} id="tsparticles" init={particlesInit} loaded= 
  {particlesLoaded} options={props.style === "one" ? paramOne : paramTwo   } />

我试过像这样的粒子js宽度道具

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<Particles width='99%' id="tsparticles" init={particlesInit} loaded={particlesLoaded} options= 
  {props.style === "one" ? paramOne : paramTwo   } />

我正在考虑尝试类名方法,但是当它不改变时,使用内联样式,那么类名方法会有什么帮助。没有其他div设置为fixed,这是重叠的滚动条,设置画布位置绝对也没有帮助,因为我希望它保持固定,其余的元素可以滚动。

EN

回答 1

Stack Overflow用户

发布于 2022-10-04 12:18:09

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import { useCallback } from "react";
import { loadFull } from "tsparticles";
import Particles from "react-tsparticles";
import { particlesOptionsConfig, } from "./particles-config";
import './styles.scss'

const ParticlesBackground = (props) => {
    const { children } = props
    const particlesInit = useCallback(async (engine) => {
        await loadFull(engine);
    }, []);
    const canvasPosition = `
        width: 100% !important;
        height: 100% !important;
        position: absolute !important;
        zIndex: 0 !important;
        top: 0px !important;
        left: 0px !important;
        backgroundColor: rgb(13; 71; 161);
        pointerEvents: none;
    `
    const particlesLoaded = useCallback(async (container) => {
        const element = document.getElementsByClassName("ParticlesCanvasWrapper")[0]
        element.setAttribute("style", canvasPosition);
    }, []);

    return (
        <div>
            <Particles
                id="tsparticles"
                init={particlesInit}
                loaded={particlesLoaded}
                options={particlesOptionsConfig}
                canvasClassName="ParticlesCanvasWrapper"
            >
                {children}
            </Particles>
        </div>

    );
};
export default ParticlesBackground;

组件支柱上的内联样式不起作用。我使用组件生命周期添加内联样式属性,如

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72264334

复制
相关文章
比较字符串(忽略空格,忽略大小写)
总时间限制: 1000ms内存限制: 65536kB描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。 输入两行,每行包含一个字符串。输出若两个字符串相等,输出YES,否则输出NO。样例输入 a A bb BB ccc CCC Aa BBbb CCCccc 样例输出 YES 源码如下: #include <iostream> using namespace std; bool compareTwoStringIgnoreCases(string a,string b); in
用户1631856
2018/04/12
3.2K0
16:忽略大小写的字符串比较
16:忽略大小写的字符串比较 总时间限制: 1000ms 内存限制: 65536kB描述 一般我们用strcmp可比较两个字符串的大小,比较方法为对两个字符串从前往后逐个字符相比较(按ASCII码值大小比较),直到出现不同的字符或遇到'\0'为止。如果全部字符都相同,则认为相同;如果出现不相同的字符,则以第一个不相同的字符的比较结果为准(注意:如果某个字符串遇到'\0'而另一个字符串还未遇到'\0',则前者小于后者)。但在有些时候,我们比较字符串的大小时,希望忽略字母的大小,例如"Hello"和"hell
attack
2018/04/03
1.7K0
忽略大小写比较字符串大小
一般我们用 strcmpstrcmp 可比较两个字符串的大小,比较方法为对两个字符串从前往后逐个字符相比较(按 ASCII 码值大小比较),直到出现不同的字符或遇到 \0 为止。
莫浅子
2022/11/18
1.2K0
python 字符串比较忽略大小写的方法
A.正则表达式,使用IGNORECASE标志 >>> import re >>> m = re.search('multi', 'A mUltiCased string', re.IGNORECASE) >>> bool(m) True B.在比较前把2个字符串转换成同样大写,用upper()方法,或小写,lower() >>> s = 'A mUltiCased string'.lower() >>> s 'a multicased string' >>> s.find('multi') 2
py3study
2020/01/14
8.3K0
python 字符串搜索替换(忽略大小写)
re.IGNORECASE text = 'UPPER PYTHON, lower python, Mixed Python' re.findall('python', text, flags=re.IGNORECASE) ['PYTHON', 'python', 'Python'] re.sub('python', 'snake', text, flags=re.IGNORECASE) 'UPPER snake, lower snake, Mixed snake'
用户5760343
2022/01/10
1K0
Swift4 获取String子字符串
都说Swift2和Swift3不是同一门语言,但是我怎么觉得Swift4有时看着也像别人家的孩子。。。。 这里主要是更新下以前的写的Swift3的String相关知识:
韦弦zhy
2018/09/11
6270
Swift 字符串和字符
Swift 字符串通过 String 类型来表示,也可以表示为 Character 类型值的集合。
Swift社区
2021/11/26
7930
Swift基础 字符串和字符
翻译自:https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
郭顺发
2023/07/17
1920
Swift基础 字符串和字符
go 生成随机字符串和获得定长字符串
挑战A.I.,赢百万奖金......了解更多详情>>> 随机字符串 //RandomStr 随机生成字符串 func RandomStr(length int) string { str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) result := []byte{} r := rand.New(rand.NewSource(time.Now().UnixNano
solate
2019/07/19
1.8K0
ToStringBuilder如何忽略null和空字符串
日常开发中,经常用org.apache.commons.lang3.builder.ToStringBuilder工具类来记录日志,比如:
菩提树下的杨过
2022/05/09
1.1K0
Swift教程(三)--字符串和字符
字符串是一系列的字符,比如说 “hello, world”或者 “likeyou”。Swift 的字符串用 String类型来表示。 String的内容可以通过各种方法来访问到,包括作为 Character值的集合。
roc
2019/07/31
6070
获得定长字符串
        C#中的字符串是Unicode编码,length是Unicode的Char的个数。所以,假如一个字符串中中英文混杂,又想获得一个固定宽度的字符串,就比较麻烦。单纯转换成字节再截取难免会碰到半个汉字的问题。         下面实现了这样的功能,返回固定字节长度的字符串,如果发生截断,后面补充2个或者3个“.”,根据截断点的位置决定。 /// <summary>         /// 返回定长的字符串,如果发生截取,在后面补充两个或三个"."         /// Author:jetz
用户1075292
2018/01/23
7880
Swift3 获取String子字符串Substring简单扩展
Swift3更新后不兼容Swift2了,刚开始看Swift,发现好多方法都不能用了啊,那就只能自己摸索了,同时也在这与大家分享分享,正好让大家帮我指正。
韦弦zhy
2018/09/11
6700
Python 字符串的大小写(一)
文章链接: https://icodeq.com/2021/6a332b281b55/
Zkeq
2022/05/18
5770
Swift3.0 for 循环和遍历字符串
未经允许不得转载:RffanLAB|Rffan实验室 » Swift3.0 for 循环和遍历字符串
用户2353021
2020/05/09
9010
[译]Swift中的空字符串
Swift 一个特色就是有很多的语法糖,初学可能觉得hold不住,实际用的时候倒是挺便利。
小刀c
2022/08/16
9800
Python字符串的大小写(二)
文章链接: https://icodeq.com/2021/28d6866714c1/
Zkeq
2022/05/18
5960
字符串——459. 重复的子字符串
示例 1: 输入: s = “abab” 输出: true 解释: 可由子串 “ab” 重复两次构成。
向着百万年薪努力的小赵
2022/12/02
1.4K0
字符串中查找子串_cstring查找子字符串
假设要从主串 s = “goodgoogle” 中找到 t = “google” 子串。根据我们的思考逻辑,则有:
全栈程序员站长
2022/09/24
3K0
字符串中查找子串_cstring查找子字符串
Java 字符串大小写转化
str.toLowerCase() 将String转换为小写,如果字符串中没有应该被转换的字符,返回原字符串
桑鱼
2020/04/24
1.2K0
Java 字符串大小写转化

相似问题

如何在Nextjs中设置背景图像?

214

如何在白色背景上显示Libgdx粒子效果?

11

粒子设计器设置背景图像

10

OpenglES混合粒子而不是背景粒子

30

如何将粒子-js设置为背景?

11
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文