简体   繁体   English

使用JavaScript构造函数对画布进行动画处理

[英]Animating canvas with a javascript constructor

Hello stackoverflow community! 您好stackoverflow社区! First I must say that I dont have much experience with constructors. 首先,我必须说,我在构造函数方面没有太多经验。 So. 所以。 What I am trying to do, is to animate a parachutist to fly from top to bottom of the screen. 我想做的是使跳伞者从屏幕的顶部飞到底部。 I thought I could use a constructor to set up a parachutist: 我以为可以使用构造函数来设置跳伞运动员:

var parachute = function() {
    this.height = 35;
    this.width = 30;
    this.speed = 50;
    this.xPos = Math.round(Math.random() * (window.width - this.width));
    this.animate = function() {
        this.img = new Image();
        this.yPos = 0;
        this.img.onload = function() {
            ctxPara.globalCompositeOperation = 'copy';
            ctxPara.translate(0, this.yPos);
            ctxPara.drawImage(this.img, this.xPos, 0);
        };
        this.img.src = 'para.png';
        this.yPos++;
    };

}; };

This constructor is used in a function called 'fly': 此构造函数用于称为“ fly”的函数中:

var fly = function() {      
    var newParachute = new parachute();
    setInterval(newParachute.animate, newParachute.speed);
};

And this 'fly' function is triggered when the window loads: 并在窗口加载时触发此“ fly”功能:

window.onload = function() {
    var canvasBg = document.getElementById('canvasBg'); 
    // I splitt the Background and the parachutists in two canvas elements 
    // handling the problem (to erase content and draw new content) with 
    // the canvas animation.
    var canvasPara = document.getElementById('canvasPara');
    ctxPara = canvasPara.getContext('2d');
    canvasPara.width = window.width;
    canvasPara.height = window.height;
    canvasBg.width = window.width;
    canvasBg.height = window.height;
    fly();  
    clouds();  // background is loading here
};

What you should see, is a Parachutist flying down the screen. 您应该看到的是跳伞者在屏幕上飞来飞去。 But unfortunately you don't... Now, after that Long text. 但是很遗憾,您没有...现在,在那篇长篇文章之后。 (Iam very sorry that it is so long :-( ) My question is: Do you know what I am doing wrong? Is my constuctor correct? Is, what i am trying to do, supposed to be written like this? Any advices or suggestions for a succesfull opportunity? (I hope my english isn't that terrible I think it is :-) ) (我很抱歉这么长:-()我的问题是:您知道我做错了吗?我的构造方法正确吗?是我想做的事情,应该这样写吗?任何建议或一个成功的机会的建议(我希望我的英语不是那么糟糕,我认为是:-))

Oh i forgot to mention the error. 哦,我忘了提到错误。 It's a TypeMissMatchError. 这是TypeMissMatchError。 That means 'this.img' is not an img element at this line: 这意味着“ this.img”不是这一行的img元素:

ctxPara.drawImage(this.img, this.xPos, 0);

Now, I followed the example of markE. 现在,我以markE为例。 Instead of showing me a parachutist. 而不是向我展示跳伞运动员。 It shows me an error in this line: ctxPara.drawImage(this.img, this.xPos, this.yPos); 它向我显示了这一行的错误:ctxPara.drawImage(this.img,this.xPos,this.yPos);

var fly = function () {
    var newParachute = new parachute();
    newParachute.img.load.call(newParachute);
    setInterval(newParachute.animate.call(newParachute), newParachute.speed);
};
var parachute = function () {
    this.height = 35;
    this.width = 30;
    this.speed = 25;
    this.xPos = Math.round(Math.random() * (window.innerWidth - this.width));
    this.img = new Image();
    this.yPos = 0;
    this.img.isLoaded = false;
    this.img.load = function () {
        this.img.isLoaded = true;
    };
    this.img.src = 'parachute.png';
    this.animate = function () {
        if (this.img.isLoaded) {
            ctxPara.clearRect(0, 0, canvasPara.width, canvasPara.height);
            ctxPara.drawImage(this.img, this.xPos, this.yPos); // ERROR: 'Unknown Error'.
            this.yPos++;
            console.log('animating');
        }
    };
};

I am stuck again. 我又被卡住了。 But now i don't even know the reason... Please help!? 但是现在我什至不知道原因...请帮助!?

Demo: http://jsfiddle.net/m1erickson/ym55y/ 演示: http//jsfiddle.net/m1erickson/ym55y/

A couple of issues: 几个问题:

(1) To get the window width you can use: (1)要获得窗口宽度,可以使用:

window.innerWidth

(2) setInterval calls newParachute.animate. (2)setInterval调用newParachute.animate。

setInterval(newParachute.animate, newParachute.speed);

But this inside animate the window object--not the Parachute object. 但是this里面动画窗口对象-而不是降落伞对象。

To give the correct this to animate you can use the call method like this: 要给出正确的this动画可以使用call方法是这样的:

var newParachute = new parachute();

setInterval(function(){newParachute.animate.call(newParachute);}, newParachute.speed);

(3) You need to deal with clearing previously drawn images or they will still show on your canvas. (3)您需要清除以前绘制的图像,否则它们仍将显示在画布上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM