javascript - Canvas 动画/reactjs/with requestAnimationFrame

标签 javascript reactjs canvas html5-canvas requestanimationframe

我的应用程序上有多个 Canvas 动画,除了 this 之外一切都运行顺利。 ,但现在我的动画框架有问题(我怀疑)。

当我加载组件(动画1)然后更改为组件(动画2)然后返回第一个组件时,发生了一些奇怪的事情,每次我回到该组件时动画都会变得更快,但我不知道原因。 两个组件的动画是相同的,上下移动对象。

奇怪的是,在我的主机上,即使切换 6-7 次,移动速度也始终相同,但对象每次移动得越来越快...... 知道可能出现什么问题吗?

这是一个动画,第二个与这个非常相似:

    import React, { Component } from 'react';
let loadBall = [];
let canvas;
let c;
let counterX = 40;
let counterY = 30;
let y =  counterY ;
class Loading extends Component {
    constructor(props){
        super(props)
        this.state = {
            vy: 0,
            time:this.props.time
        }
        this.loadingLoop = this.loadingLoop.bind(this);
    }
    componentDidMount(){
        canvas = document.getElementById('ball');
        canvas.height = 150;
        canvas.width = window.innerHeight;
        c = canvas.getContext('2d');
        this.loadingInit()
        this.loadingLoop()
        window.addEventListener('resize', () => {
            canvas.width = window.innerHeight;
            this.loadingInit()
        })
        this.loadingInit();
    }
    loadingLoop(){
        requestAnimationFrame(this.loadingLoop);
        c.clearRect(0,0, canvas.width, canvas.height);
        for (let i = 0; i < loadBall.length; i++) {
            loadBall[i].update();
        }
    }
    loadingInit(){
        loadBall = [];
        for (let i = 0; i < 3; i++) {
            let radius = 30//Math.floor(Math.random() * 20) + 15;
            let x =  (canvas.width / 2) - (radius * 4) + counterX;
            y = counterY;
            let color = colors[i];
            loadBall.push(new loadingBall(x,y, radius, color));
            counterY += 30;
            counterX += 70;
        }
    }
render() {
    return (
        <canvas id='ball' style={{position:'fixed', top: '50%', left: '50%',WebkitTransform:'translate(-50%, -50%)'}}></canvas>
    );
}
}

function loadingBall(x,y,radius,color){
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;
    this.move = 2
    this.update = () =>{
        if (this.y + this.radius + this.move >= canvas.height - 3) {
            this.move = -this.move    
        } 
        if (this.y - this.radius - this.move <= 3) {
            this.move = 2;
        }
        this.y += this.move;
        this.draw();
    }
    this.draw = () => {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillRect(this.x, this.y, this.radius, 5);
        c.fillStyle = this.color;
        c.fill();
        c.strokeStyle = this.color;
        c.stroke();
        c.closePath();
    }
}
export default Loading;

任何建议都会有帮助!

最佳答案

此问题可能是由于卸载组件后渲染循环(即 this.loadingLoop)没有停止而引起的。因此,当卸载组件时,渲染循环会继续运行,并且当再次安装组件时,会启动一个新的渲染循环 - 这会导致两个渲染循环串联运行,从而导致动画中感知到的“加速”。

解决这个问题的一种方法是向组件添加一个标志来控制渲染循环的继续。这可以通过添加以下内容来实现:

constructor(props){
    super(props)
    this.state = {
        vy: 0,
        time:this.props.time
    }
    this.loadingLoop = this.loadingLoop.bind(this);

    // Add this flag to control if animation is allowed to continue
    this.stopAnimation = false;
}

componentDidMount(){

    // Add this to the start of componentDidMount to let animation 
    // start and continue while component is mounted
    this.stopAnimation = false;

    // ...
    // your existing code
    // ...
}

componentWillUnmount() {

    // When the component unmounts, set flag to stop animation
    this.stopAnimation = true;
}

loadingLoop(){

    // If the stopAnimation flag is not truthy, stop future
    // frames for this animation cycle
    if( this.stopAnimation ) {
       return
    }

    requestAnimationFrame(this.loadingLoop);

    c.clearRect(0,0, canvas.width, canvas.height);
    for (let i = 0; i < loadBall.length; i++) {
        loadBall[i].update();
    }
}

关于javascript - Canvas 动画/reactjs/with requestAnimationFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53661473/

相关文章:

javascript - 绝对 div 内的输入没有响应

javascript - 如何通过 jQuery 序列化多个复选框值?

javascript - 在使用 FileReader 上传之前预览图像,旋转图像

javascript - Webapi Controller 参数为空

javascript - 绘制动画线条

javascript - TypeORM 与除 id 之外的字段的关系

ios - iOS Safari/Chrome/Firefox 中的 CSS 关键帧动画闪烁/不同步

javascript - React 组件 render 方法中的语法错误

javascript - DOM 循环中的 HTML5 Canvas

javascript - 仅图像数据对象的一部分的 putImageData