Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React将动态画布作为div的背景_Javascript_Reactjs_Canvas_Background - Fatal编程技术网

Javascript 使用React将动态画布作为div的背景

Javascript 使用React将动态画布作为div的背景,javascript,reactjs,canvas,background,Javascript,Reactjs,Canvas,Background,我想使用React为我的网站中的特定组件创建一个动态背景。我想让它成为一个运行react粒子js模拟的画布。一切都在React组件中,并像往常一样使用render()方法进行渲染。结果应该是这样的 具有背景的组件的代码应如下所示: class ParticleBackgrondComponent extends Component{ render(){ return ( <div> <Particles/> {//Th

我想使用React为我的网站中的特定组件创建一个动态背景。我想让它成为一个运行react粒子js模拟的画布。一切都在React组件中,并像往常一样使用
render()
方法进行渲染。结果应该是这样的

具有背景的组件的代码应如下所示:

class ParticleBackgrondComponent extends Component{

render(){
    return (

        <div>
            <Particles/> {//The particle background}

            <h3>
                Some other content
            </h3>
            <p>
                Lorem ipsum dolor sit amet...
            </p>

        </div>
    )
}
类ParticleBackgrondComponent扩展组件{
render(){
返回(
{//粒子背景}
其他一些内容

我爱你,我爱你。。。

) }
}

我尝试将画布的位置设置为绝对,并手动将其放在我想要的div后面,但当我调整窗口大小时,它不起作用。

您正在查找

。动态背景{
背景:元素(.particle canvas);
}
但是,目前(2020年4月),firefox只支持使用非标准的
-moz-element()
。如果您计划在目前针对更多浏览器,我建议您寻找替代解决方案。

您正在寻找解决方案

。动态背景{
背景:元素(.particle canvas);
}
但是,目前(2020年4月),firefox只支持使用非标准的
-moz-element()
。如果您计划在目前针对更多浏览器,我建议您寻找替代解决方案。

虽然
元素()
仍然不受支持,但您完全可以将背景元素放置在前景元素后面,并根据前景的大小动态设置画布的宽度和高度:

const TAU=2*Math.PI;
班级圈子{
构造器(画布){
this.x=Math.random()*canvas.width;
this.y=Math.random()*canvas.height;
this.r=Math.random()*Math.sqrt(canvas.width**2+canvas.height**2)/8;
this.growth=Math.random()*4;
this.decay=Math.max(Math.random()*0.005,0.0005);
this.rgb=[
Math.random()*255,
Math.random()*255,
Math.random()*255,
];
this.alpha=Math.random()*0.35;
}
获取fillStyle(){
返回`rgba(${this.rgb.join(',')},${this.alpha})`;
}
渲染(ctx){
ctx.beginPath();
ctx.fillStyle=this.fillStyle;
ctx.arc(this.x,this.y,this.r,0,TAU);
ctx.fill();
this.r+=this.growth;
这个α-=这个衰变;
}
}
函数渲染(ctx、前景、圆圈=[]){
const{width,height}=前景.getBoundingClientRect();
ctx.canvas.width=宽度;
ctx.canvas.height=高度;
ctx.clearRect(0,0,宽度,高度);
如果(圆圈长度===0){
圆。推(新圆(ctx.canvas));
}
if(Math.random()>0.98){
圆。推(新圆(ctx.canvas));
}
for(常数圆){
圆。渲染(ctx);
}
window.requestAnimationFrame(()=>{
渲染(ctx、前景、圆.filter(圆=>circle.alpha>0))
});
}
addEventListener(“DOMContentLoaded”,()=>{
const canvas=document.querySelector(“canvas”);
const foreground=document.querySelector(“.foreground”);
渲染(canvas.getContext(“2d”),前景);
});
.container{
位置:相对位置;
}
.背景{
位置:绝对位置;
z指数:-1;
}

Lorem ipsum dolor sit amet,是一位杰出的献身者。这是一只秃鹰。在佩伦茨克的《奥古斯·维韦拉》(augue viverra)一书中,利奥·奎斯(leo quis)是一位伟大的天才

同侧拉齐尼肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌,静脉曲张肌。整型tincidunt elementum调味品。帕雷森特·莫莱斯蒂,努克·塞德·乌兰科珀·阿库姆桑,奥古斯·乌纳·马克西姆斯·洛雷姆,在维尼纳蒂斯奥奇·特鲁斯·维塔·利奥

佩伦特·埃吉特·拉库斯·萨吉蒂,索利西图丁·莫里斯·埃吉特,亨德雷特·埃吉特。根据康努比亚·诺斯特拉(conubia nostra)和希梅纳奥斯(inceptos himenaeos)的规定,社会责任和责任等级为:。萨皮安·奥迪奥(Curabitur sapien odio),尤兰科珀(ullamcorper)在埃拉特·维塔(erat vitae),莫利斯·瓦普塔特·利奥(mollis vulputate leo)

虽然
元素()
仍然不受支持,但您完全可以将背景元素放置在前景元素后面,并根据前景的大小动态设置画布的宽度和高度:

const TAU=2*Math.PI;
班级圈子{
构造器(画布){
this.x=Math.random()*canvas.width;
this.y=Math.random()*canvas.height;
this.r=Math.random()*Math.sqrt(canvas.width**2+canvas.height**2)/8;
this.growth=Math.random()*4;
this.decay=Math.max(Math.random()*0.005,0.0005);
this.rgb=[
Math.random()*255,
Math.random()*255,
Math.random()*255,
];
this.alpha=Math.random()*0.35;
}
获取fillStyle(){
返回`rgba(${this.rgb.join(',')},${this.alpha})`;
}
渲染(ctx){
ctx.beginPath();
ctx.fillStyle=this.fillStyle;
ctx.arc(this.x,this.y,this.r,0,TAU);
ctx.fill();
this.r+=this.growth;
这个α-=这个衰变;
}
}
函数渲染(ctx、前景、圆圈=[]){
const{width,height}=前景.getBoundingClientRect();
ctx.canvas.width=宽度;
ctx.canvas.height=高度;
ctx.clearRect(0,0,宽度,高度);
如果(圆圈长度===0){
圆。推(新圆(ctx.canvas));
}
if(Math.random()>0.98){
圆。推(新圆(ctx.canvas));
}
for(常数圆){
圆。渲染(ctx);
}
window.requestAnimationFrame(()=>{
渲染(ctx、前景、圆.filter(圆=>circle.alpha>0))
});
}
addEventListener(“DOMContentLoaded”,()=>{
const canvas=document.querySelector(“canvas”);
const foreground=document.querySelector(“.foreground”);
渲染(canvas.getContext(“2d”),前景);
});
.container{
位置:相对位置;
}
巴克先生
class ParticleBackgroundComponent extends Component{
    constructor(props){
        super(props);

        //to help us get the boundingClientRect
        this.selector = React.createRef();

        //our state variables that store dimensions an position of the background
        this.state = {
            width: 0,
            height: 0,
            top: 0
        };
    }

    componentDidMount() {
        /*the first event listener to figure out when the page is done loading
        (use this if you have images in the component)*/
        window.addEventListener('load', () => {

            //setting the state to draw the background the first time
            var rect = this.selector.current.getBoundingClientRect();
            this.setState({
                width: rect.width,
                height: rect.height,
                top: rect.top + window.scrollY
            });

            //second event listener for resizing
            window.addEventListener('resize', ()=>{
                //redraw the background every time the page resizes 
                rect = this.selector.current.getBoundingClientRect();
                this.setState({
                    width: rect.width,
                    height: rect.height,
                    top: rect.top + window.scrollY
                })
            });

        });
    }

    //to render the particles
    renderBackground(){
        var width = this.state.width;
        var height = this.state.height;
        var top = this.state.top;

        //using a div to set the position of the background
        return (
            <div style={{position: "absolute", top: t, zIndex: -1}}>
                <Particles width={width} height={height}/>
            </div>
        )
    }

    render(){
        return (
            <div ref={this.selector}>

                <h1>
                    Some more content
                </h1>
                <p>
                    Lorem Ipsum dolor sit amet...
                </p>

                {this.renderBackground()}

            </div>
        )
    }
}