-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


Pixi.js - 20

, 04 2017 . 13:35 +
, !

Pixi.js , . , , . -, , . :




, . Pixi.js 2D WebGL. , , , . , API , . , , :

www.goodboydigital.com/pixijs/bunnymark

, .
.

Pixi , . CDN. npm.
. :

npm install pixi.js

, , index.html main.js
index.html . , source. npm :

node_module/pixi.js/dist/pixi.js

, ( ), main.js .

, pixi .
, model( ) createCanvas, . , ( , ) . ( , ):

var width = window.innerWidth; //  
var height = window.innerHeight; //   
var app; //    

var model = {
    createCanvas: function() {
        app = new PIXI.Application(width, height); // 
        document.body.appendChild(app.view); //    
    }

}


, :

model.createCanvas();

canvas . view, :


var view = {
	loadGame: function(){
		model.createCanvas();
	}
}

view.loadGame(); // 



, . (), . controller, , . .
( , ):

var width = window.innerWidth; //  
var height = window.innerHeight; //   
var app; //    
var colors = [0xFFFF0B, 0xFF700B, 0x4286f4, 0x4286f4, 0xf441e8, 0x8dff6d, 0x41ccc9, 0xe03375, 0x95e032, 0x77c687, 0x43ba5b, 0x0ea3ba]; // , 0x  #

var model = {
    createCanvas: function() {
        app = new PIXI.Application(width, height); // 
        document.body.appendChild(app.view); //    
    },
    drawCircle: function() {
        rand = Math.floor(Math.random() * colors.length); //  (   0      )
        var radius = 50; // 
        var inAreaX = width - 100; //    X,    ,     
        var circleY = -50; //     ( ,   ,     )
        var circleX = Math.floor(Math.random()* inAreaX); //       X
        var circle = new PIXI.Graphics(); //   
        circle.lineStyle(0); // 
        circle.beginFill(colors[rand], 1); //  
        circle.drawCircle(circleX, circleY, radius); // ,    
        circle.endFill(); // 
        circle.interactive = true; //  
        circle.buttonMode = true; //   
        app.stage.addChild(circle); //   
        circle.on('pointerdown', controller.clearFigure); //       
    }
}
var view = {
    loadGame: function() {
        model.createCanvas();
        model.drawCircle();// ,   
    }
}


var controller = {
	clearFigure: function(){
		this.clear(); //    
	}
}

view.loadGame();


app.stage.addChild(circle); Pixi, , - , , . 70% , , , .

.
, .

gravity( colors), , 4. figuresAmount 0( ) figure, . drawCircle ( , , ):

var colors = [0xFFFF0B, 0xFF700B, 0x4286f4, 0x4286f4, 0xf441e8, 0x8dff6d, 0x41ccc9, 0xe03375, 0x95e032, 0x77c687, 0x43ba5b, 0x0ea3ba]; // 
var gravity = 4;
var figuresAmount = -1; //  ( ,        )
var figure = []; //   

 {
.

drawCircle:

circle.buttonMode = true; //   
figuresAmount++; //   
figure.push(circle); //     circle   ,     
app.stage.addChild(circle); //   


view.loadGame , , . Pixi :

app.ticker.add();


:

model.drawCircle(); //    
setInterval(model.drawCircle, 500); //    

app.ticker.add(function() { //  
       for (var i = 0; i < figuresAmount; i++) {
             figure[i].position.y += gravity; //  
       }        
    });
 } //  loadGame();


.
( ) Game Over.

, . drawCircle circle.live, true( , ), , false:)
figuresAmount, . drawCircle:

circle.buttonMode = true; //   
circle.live = true; //         
figuresAmount++;
circle.num = figuresAmount; //    
figure.push(circle); //     circle   ,     


( ):

gameOver: function() {
        var style = new PIXI.TextStyle({ //  
            fill: '0xffffff',
            fontSize: 36,
        }); 
        var gameOverText = new PIXI.Text('Game Over', style); //  
        gameOverText.x = width / 2; //  
        gameOverText.y = height / 2; //  
        gameOverText.pivot.x = 50; //   
        gameOverText.pivot.y = 50; //    y
        app.stage.addChild(gameOverText); //  
}


clearFigure , , circle.live false. , clear() , anvasa( , ).

:

clearFigure: function() {
        this.clear();
        figure[this.num].live = false;
}


gameOver :

app.ticker.add(function() { //  
            for (var i = 0; i < figuresAmount; i++) {
                figure[i].position.y += gravity; //  
                if (figure[i].position.y > height && figure[i].live == true) {//          ,   ,      "     "
                    model.gameOver();
                    return false;
                } 
            }


, . - , codepen. , .

Pixi.js .
, .

!
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330174/

:  

: [1] []
 

:
: 

: ( )

:

  URL