Newer
Older
pixi.js / test / unit / core / Bounds.test.js
@Mat Groves Mat Groves on 9 Aug 2016 10 KB fixes #1698
describe('getBounds', function () {


    it('should register correct width and height with a LOADED Sprite', function() {
        var parent = new PIXI.Container();
        var texture = PIXI.RenderTexture.create(10, 10);

        var sprite = new PIXI.Sprite(texture);

        parent.addChild(sprite);


        var bounds = sprite.getBounds();

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(10);
        expect(bounds.height).to.equal(10);

        sprite.position.x = 20;
        sprite.position.y = 20;

        sprite.scale.x = 2;
        sprite.scale.y = 2;

        var bounds = sprite.getBounds();

        expect(bounds.x).to.equal(20);
        expect(bounds.y).to.equal(20);
        expect(bounds.width).to.equal(20);
        expect(bounds.height).to.equal(20);


        var bounds = sprite.getBounds(true);

        expect(bounds.x).to.equal(20);
        expect(bounds.y).to.equal(20);
        expect(bounds.width).to.equal(20);
        expect(bounds.height).to.equal(20);

    });

    it('should register correct width and height with Graphics', function() {
        var parent = new PIXI.Container();

        var graphics = new PIXI.Graphics();

        var bounds = graphics.getBounds();

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(0);
        expect(bounds.height).to.equal(0);

        graphics.beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

        parent.addChild(graphics);

        var bounds = graphics.getBounds();

        expect(bounds.x).to.equal(-10);
        expect(bounds.y).to.equal(-10);
        expect(bounds.width).to.equal(20);
        expect(bounds.height).to.equal(20);

        graphics.position.x = 20;
        graphics.position.y = 20;

        graphics.scale.x = 2;
        graphics.scale.y = 2;

        var bounds = graphics.getBounds();

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(40);
        expect(bounds.height).to.equal(40);

        var bounds = graphics.getBounds(true);

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(40);
        expect(bounds.height).to.equal(40);

    });

    it('should register correct width and height with an empty Container', function() {
        var parent = new PIXI.Container();

        var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

        parent.addChild(container);

        var bounds = container.getBounds();

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(0);
        expect(bounds.height).to.equal(0);

        container.position.x = 20;
        container.position.y = 20;

        container.scale.x = 2;
        container.scale.y = 2;

        var bounds = container.getBounds();

        expect(bounds.x).to.equal(0);
        expect(bounds.y).to.equal(0);
        expect(bounds.width).to.equal(0);
        expect(bounds.height).to.equal(0);

    });

    it('should register correct width and height with a Container', function() {
        var parent = new PIXI.Container();

        var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

    	var graphics = new PIXI.Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

    	var texture = PIXI.RenderTexture.create(10, 10);
        var sprite = new PIXI.Sprite(texture);

        container.addChild(sprite);
        container.addChild(graphics);

        parent.addChild(container);

	    sprite.position.x = 30;
    	sprite.position.y = 20;
	    graphics.position.x = 100;
    	graphics.position.y = 100;

        var bounds = container.getBounds();

        expect(bounds.x).to.equal(30);
        expect(bounds.y).to.equal(20);
        expect(bounds.width).to.equal(80);
        expect(bounds.height).to.equal(90);

	    container.rotation = 0.1;

        var bounds = container.getBounds();

        expect(bounds.x | 0).to.equal(26);
        expect(bounds.y | 0).to.equal(22);
        expect(bounds.width | 0).to.equal(73);
        expect(bounds.height | 0).to.equal(97);

        var bounds = container.getBounds(true);

        expect(bounds.x | 0).to.equal(26);
        expect(bounds.y | 0).to.equal(22);
        expect(bounds.width | 0).to.equal(73);
        expect(bounds.height | 0).to.equal(97);


    });

	it('should register correct width and height with an item that has already had its parent Container transformed', function() {
        var parent = new PIXI.Container();

        var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

    	var graphics = new PIXI.Graphics().beginFill(0xFF0000).drawRect(0, 0, 10, 10);//texture);


        parent.addChild(container);
        container.addChild(graphics);

	    container.position.x = 100;
    	container.position.y = 100;

        var bounds = container.getBounds();

        expect(bounds.x).to.equal(100);
        expect(bounds.y).to.equal(100);
        expect(bounds.width).to.equal(10);
        expect(bounds.height).to.equal(10);


        var bounds = graphics.getBounds(true);

        expect(bounds.x).to.equal(100);
        expect(bounds.y).to.equal(100);
        expect(bounds.width).to.equal(10);
        expect(bounds.height).to.equal(10);




    });

	it('should register correct width and height with a Mesh', function() {
	        var parent = new PIXI.Container();

	    	var texture = PIXI.RenderTexture.create(10, 10);

	        var plane = new PIXI.mesh.Plane(texture);

	        parent.addChild(plane);

		    plane.position.x = 20;
	    	plane.position.y = 20;

	        var bounds = plane.getBounds();

	        expect(bounds.x).to.equal(20);
	        expect(bounds.y).to.equal(20);
	        expect(bounds.width).to.equal(10);
	        expect(bounds.height).to.equal(10);

	        plane.scale.x = 2;
	    	plane.scale.y = 2;

	        var bounds = plane.getBounds();

	        expect(bounds.x).to.equal(20);
	        expect(bounds.y).to.equal(20);
	        expect(bounds.width).to.equal(20);
	        expect(bounds.height).to.equal(20);

	    });

        it('should register correct width and height with an a DisplayObject is visible false', function() {

            var parent = new PIXI.Container();

            var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

            var graphics = new PIXI.Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

            var texture = PIXI.RenderTexture.create(10, 10);
            var sprite = new PIXI.Sprite(texture);

            container.addChild(sprite);
            container.addChild(graphics);

            parent.addChild(container);

            sprite.position.x = 30;
            sprite.position.y = 20;
            graphics.position.x = 100;
            graphics.position.y = 100;

            graphics.visible = false;

            var bounds = container.getBounds();

            expect(bounds.x).to.equal(30);
            expect(bounds.y).to.equal(20);
            expect(bounds.width).to.equal(10);
            expect(bounds.height).to.equal(10);

            container.visible = false;

            var bounds = container.getBounds();

            expect(bounds.x).to.equal(0);
            expect(bounds.y).to.equal(0);
            expect(bounds.width).to.equal(0);
            expect(bounds.height).to.equal(0);


        });

        it('should register correct width and height with an a DisplayObject parent has moved', function() {

            var parent = new PIXI.Container();

            var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

            var graphics = new PIXI.Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10);//texture);

            container.addChild(graphics);

            parent.addChild(container);

          //  graphics.position.x = 100;
          //  graphics.position.y = 100;
            container.position.x -= 100;
            container.position.y -= 100;

            var bounds = graphics.getBounds();

            expect(bounds.x).to.equal(-110);
            expect(bounds.y).to.equal(-110);
            expect(bounds.width).to.equal(20);
            expect(bounds.height).to.equal(20);


        });

        it('should register correct width and height with an a Text Object', function() {

            var parent = new PIXI.Container();

            var container = new PIXI.Container();//Graphics().beginFill(0xFF0000).drawCircle(0, 0, 10, 10);//texture);

            var text = new PIXI.Text('i am some text');

            container.addChild(text);

            parent.addChild(container);

            var bounds = text.getBounds();
            var bx = bounds.width;

            expect(bounds.x).to.equal(0);
            expect(bounds.y).to.equal(0);
            expect(bounds.width).to.be.greaterThan(0);
            expect(bounds.height).to.greaterThan(0);

            text.text = 'hello!';

            var bounds = text.getBounds();

            // this variable seems to be different on different devices. a font thing?
            expect(bounds.width).to.not.equal(bx);

        });

        it('should return a different rectangle if getting local bounds after global bounds ', function() {

            var parent = new PIXI.Container();
            var texture = PIXI.RenderTexture.create(10, 10);

            var sprite = new PIXI.Sprite(texture);

            sprite.position.x = 20;
            sprite.position.y = 20;

            sprite.scale.x = 2;
            sprite.scale.y = 2;

            parent.addChild(sprite);


            var bounds = sprite.getBounds();

            expect(bounds.x).to.equal(20);
            expect(bounds.y).to.equal(20);
            expect(bounds.width).to.equal(20);
            expect(bounds.height).to.equal(20);


            var localBounds = sprite.getLocalBounds();

            expect(bounds.x).to.equal(20);
            expect(bounds.y).to.equal(20);
            expect(bounds.width).to.equal(20);
            expect(bounds.height).to.equal(20);



        });


	});