Advertisement
Guest User

PIXIJS Bar

a guest
Apr 28th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <script type="text/javascript" src="pixi.min.js"></script>
  5.     </head>
  6.     <body>
  7.         <script>
  8.             /*global PIXI,texture*/
  9.             var Renderer = PIXI.autoDetectRenderer(1280, 720, {backgroundColor: '0xE9E9E9'});
  10.             document.body.appendChild(Renderer.view);
  11.            
  12.             var Stage = new PIXI.Container();
  13.            
  14.             var Bar = {
  15.                 filled: 0,
  16.                 maxFill: 92,
  17.                 sprites: {
  18.                     body: null,
  19.                     barInner: null,
  20.                     barEnd: null,
  21.                     img: null
  22.                 },
  23.                 visible: false,
  24.                 position: {
  25.                     x: 50,
  26.                     y: 650
  27.                 },
  28.                 offset: {
  29.                     img: { x: -38, y: -3 },
  30.                     barin: { x: 0, y: 26 },
  31.                     barend:{ x: 0, y: 26 }
  32.                 },
  33.                 toggle: function() {
  34.                     this.visible = !this.visible;
  35.                     if(this.visible) {
  36.                         var texture = PIXI.Texture.fromImage('img/maxbar.png');
  37.                         this.sprites.body = new PIXI.Sprite(texture);
  38.                         this.sprites.body.position = {x: this.position.x, y: this.position.y};
  39.                         Stage.addChild(this.sprites.body);
  40.                        
  41.                         texture = PIXI.Texture.fromImage('img/barin.png');
  42.                         this.sprites.barInner = new PIXI.extras.TilingSprite(texture, 1, 11);
  43.                         this.sprites.barInner.scale.x = this.filled;
  44.                         this.sprites.barInner.scale.y = 1;
  45.                         this.sprites.barInner.position = {x: this.position.x +  + this.offset.barin.x, y: this.position.y + this.offset.barin.y };
  46.                         Stage.addChild(this.sprites.barInner);
  47.                        
  48.                         texture = PIXI.Texture.fromImage('img/barend.png');
  49.                         this.sprites.barEnd = new PIXI.Sprite(texture);
  50.                         this.sprites.barEnd.position = {x: this.position.x +  + this.offset.barend.x + this.filled, y: this.position.y + this.offset.barend.y };
  51.                         Stage.addChild(this.sprites.barEnd);
  52.                        
  53.                         texture = PIXI.Texture.fromImage('img/barimg.png');
  54.                         this.sprites.img = new PIXI.Sprite(texture);
  55.                         this.sprites.img.position = {x: this.position.x + this.offset.img.x, y: this.position.y + this.offset.img.y};
  56.                         Stage.addChild(this.sprites.img);
  57.                     } else {
  58.                         Stage.removeChild(this.sprites.body);
  59.                         Stage.removeChild(this.sprites.barInner);
  60.                         Stage.removeChild(this.sprites.barEnd);
  61.                         Stage.removeChild(this.sprites.img);
  62.                         this.sprites = {
  63.                             body: null,
  64.                             barInner: null,
  65.                             barEnd: null,
  66.                             img: null
  67.                         }
  68.                     }
  69.                 },
  70.                 update: function() {
  71.                     this.sprites.barInner.scale.x = this.filled;
  72.                     this.sprites.barEnd.position.x = this.position.x + this.offset.barend.x + this.filled;
  73.                 },
  74.                 plus: 0.5
  75.             }
  76.            
  77.             Bar.toggle();
  78.            
  79.             loop();
  80.             function loop()
  81.             {
  82.                 Bar.filled += Bar.plus;
  83.                 if(Bar.filled >= Bar.maxFill) { Bar.plus = -0.5; }
  84.                 else if(Bar.filled <= 0) { Bar.plus = 0.5; }
  85.                 Bar.update();
  86.                
  87.                 Renderer.render(Stage);
  88.                 requestAnimationFrame(loop);
  89.             }
  90.         </script>
  91.     </body>
  92. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement