Advertisement
Guest User

PIXI.js StressTest Class

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