首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在html5画布中设置多个自定义形状的动画

在html5画布中设置多个自定义形状的动画
EN

Stack Overflow用户
提问于 2018-07-21 04:32:29
回答 2查看 120关注 0票数 1

非常第一篇文章,所以如果我的问题格式错误,我道歉。

我有一些经验,动画与画布多个圆圈之前,这个项目,但目前正在尝试动画的自定义字母Z。

目前在尝试让100个Z做同样的事情的过程中,由于某种原因,我的构造函数对象只绘制了一个。

到目前为止,我有:

  • console登录了我的绘图功能,查看是否走到了这一步。
  • console记录了Z对象的数组,所有100个对象都在那里。
  • 在那个对象数组中,我检查了它们的所有x坐标,它们是不同的,所以我不认为所有100个对象都位于彼此的顶部。

我怀疑update函数在使用"this“时有多大问题

我想我会丢弃我的整个javascript文件,以防它是别的东西。对不起,我不知道典型的协议。

代码语言:javascript
复制
var canvas = document.getElementById('myCanvas');

var context = canvas.getContext('2d');

canvas.height = window.innerHeight;

canvas.width = window.innerWidth;

canvasHeight = canvas.height;

canvasWidth = canvas.width

var dx = 0 
var dy = 0;
var direction = true

function Z(xCoord, yCoord, dx, dy, direction) {
    this.x = xCoord  
    this.y = yCoord    
    this.dy = dy    
    this.dx = dx    
    this.direction = direction


    this.update = function(){
        //making the Z wiggle
        if (this.dx < 20 && this.direction) {
            this.dx++
        }
        else if (this.dx === 20 && this.direction) {
            this.direction = false;
        }

        if (this.dx > -20 && !this.direction) {
            this.dx--
        }
        else if (this.dx === -20 && !this.direction)  {
            this.direction = true;
        }

        //if Z gets to top of page it resets down at the bottom
        if (this.dy === -canvasHeight - this.y) {
            this.dy = 0
        }

        this.dy--;
        this.draw()
    }

    this.draw = function() {
        context.clearRect(0, 0, context.canvas.width, context.canvas.height);
        context.translate(this.dx, this.dy); /// translate (move)

        //The Letter Z
        context.beginPath();

        context.moveTo(this.x, this.y + canvasHeight); // x+0
        context.lineTo(this.x+10, this.y + canvasHeight); // x+10
        context.lineTo(this.x+2, this.y + canvasHeight-9); // x+2
        context.lineTo(this.x+5, this.y + canvasHeight-9); // x+5
        context.lineTo(this.x+10, this.y + canvasHeight-9); // x+10
        context.lineTo(this.x+10, this.y + canvasHeight-10); // x+10
        context.lineTo(this.x, this.y + canvasHeight-10); // x+0
        context.lineTo(this.x+8, this.y + canvasHeight-1); // x+8
        context.lineTo(this.x, this.y + canvasHeight-1); // x+0
        context.lineTo(this.x, this.y + canvasHeight);  // x+0

        // complete custom shape
        context.closePath();
        context.lineWidth = 4;
        context.fillStyle = 'white';
        context.fill();
        context.strokeStyle = 'black';
        context.stroke();

        context.translate(-this.dx, -this.dy); /// translate (move)


    }

}

var ZArr = []
//for loop to make all the Z's
for (var index = 0; index < 100; index++) {

    var randomX = Math.random() * canvasWidth;
    var randomY = Math.floor(Math.random() * 500) + 1  

    var newZ = new Z(randomX, randomY, dx, dy, direction);

    ZArr.push(newZ)
}

// console.log(ZArr)

function executeFrame() {

    for (let index = 0; index < ZArr.length; index++) {
        ZArr[index].update();  
    }

    requestAnimationFrame(executeFrame);
}

//start animation
executeFrame();
EN

回答 2

Stack Overflow用户

发布于 2018-07-21 04:43:41

context.clearRect(0, 0, context.canvas.width, context.canvas.height);移动到渲染循环中。你擦除数组的每个循环中的每一幅图。

代码语言:javascript
复制
function executeFrame() {
    context.clearRect(0, 0, context.canvas.width, context.canvas.height);
    for (let index = 0; index < ZArr.length; index++) {
        ZArr[index].update();  
    }

    requestAnimationFrame(executeFrame);
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-21 04:44:27

这是因为清除画布线位于Z对象的draw函数中,所以每次尝试绘制新的Z时,该字段都会被清除。这意味着只有数组中的最后一个是可见的。移动这条线就行了。

代码语言:javascript
复制
context.clearRect(0, 0, context.canvas.width, context.canvas.height);

添加到for循环上方的executeFrame函数中。

祝好运!

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

https://stackoverflow.com/questions/51449964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档