Guest User

PIXI.js StressTest Class

a guest
Oct 21st, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Mat Groves http://matgroves.com/
  3.  */
  4. var PIXI = PIXI || {};
  5.  
  6.  
  7. PIXI.StressTest = function(callback)
  8. {
  9.     if ( !this instanceof PIXI.StressTest ){
  10.         console.log("sqdqsd");
  11.         return new PIXI.StressTest(callback);
  12.     }  
  13.     this.callback = callback;
  14.     this.stage = new PIXI.Stage(0x123456);
  15.     this.renderer = new PIXI.CanvasRenderer(500, 500);
  16.     document.body.appendChild(this.renderer.view);
  17.     this.renderer.view.style.position = "absolute";
  18.     // this.renderer.view.style.left = "-500px";
  19.  
  20.     this.duration = 3;
  21.    
  22.     var scope = this;
  23.     var canvas = document.createElement("canvas");
  24.     canvas.width = 52
  25.     canvas.height = 74
  26.     canvas.context = canvas.getContext("2d");
  27.     canvas.context.fillStyle="#FF0000";
  28.     canvas.context.fillRect(0,0,52,74);
  29.    
  30.     this.texture = PIXI.Texture.fromCanvas(canvas);
  31.     scope.begin();
  32.     // this.texture.baseTexture.addEventListener( 'loaded', function(){ scope.begin()} );
  33.    
  34.     this.frameRate = [];
  35. }
  36.  
  37. // constructor
  38. PIXI.StressTest.constructor = PIXI.StressTest;
  39.  
  40. PIXI.StressTest.prototype.begin = function()
  41. {
  42.     this.testSprites = [];
  43.     for (var i=0; i < 300; i++)
  44.     {
  45.         var bunny = new PIXI.Sprite(this.texture);
  46.         bunny.anchor.x = 0.5;
  47.         bunny.anchor.y = 0.5;
  48.         bunny.alpha = .5;
  49.         this.stage.addChild(bunny);
  50.         bunny.position.x = 50 + Math.random() * 400;
  51.         bunny.position.y = 50 + Math.random() * 400;
  52.        
  53.         this.testSprites.push(bunny);
  54.     };
  55.    
  56.     this.renderer.render(this.stage);
  57.    
  58.     this.startTime = Date.now();
  59.     this.lastTime = Date.now();
  60.    
  61.     var scope = this
  62.     requestAnimFrame(function(){scope.update()});
  63. }
  64.  
  65. PIXI.StressTest.prototype.update = function()
  66. {
  67.     var currentTime = Date.now();
  68.    
  69.     for (var i=0; i < this.testSprites.length; i++) {
  70.       this.testSprites[i].rotation += 0.3;
  71.     };
  72.    
  73.     this.renderer.render(this.stage);
  74.    
  75.     var diff = currentTime - this.lastTime;
  76.     diff *= 0.06;
  77.    
  78.     //diff *= 60;
  79.    
  80.     this.frameRate.push(diff);
  81.    
  82.     this.lastTime = currentTime;
  83.    
  84.     var elapsedTime = currentTime - this.startTime;
  85.    
  86.     if(elapsedTime < this.duration * 1000)
  87.     {
  88.         var scope = this
  89.         requestAnimFrame(function(){scope.update()});
  90.        
  91.     }
  92.     else
  93.     {
  94.    
  95.         document.body.removeChild(this.renderer.view);
  96.        
  97.         this.renderer = null;
  98.        
  99.         this.result = this.frameRate.length/this.duration;
  100.        
  101.        
  102.         if(this.callback)this.callback(this.result);
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment