Skip to content

Instantly share code, notes, and snippets.

@only-cliches
Last active March 14, 2024 12:07
Show Gist options
  • Save only-cliches/581823db9cdc8d94ed3f78c1a548f50d to your computer and use it in GitHub Desktop.
Save only-cliches/581823db9cdc8d94ed3f78c1a548f50d to your computer and use it in GitHub Desktop.
PixiJS Background Cover & Background Container
/*
* PixiJS Background Cover/Contain Script
* Returns object
* . {
* container: PixiJS Container
* . doResize: Resize callback
* }
* ARGS:
* bgSize: Object with x and y representing the width and height of background. Example: {x:1280,y:720}
* inputSprite: Pixi Sprite containing a loaded image or other asset. Make sure you preload assets into this sprite.
* type: String, either "cover" or "contain".
* forceSize: Optional object containing the width and height of the source sprite, example: {x:1280,y:720}
*/
function background(bgSize, inputSprite, type, forceSize) {
var sprite = inputSprite;
var bgContainer = new PIXI.Container();
var mask = new PIXI.Graphics().beginFill(0x8bc5ff).drawRect(0,0, bgSize.x, bgSize.y).endFill();
bgContainer.mask = mask;
bgContainer.addChild(mask);
bgContainer.addChild(sprite);
function resize() {
var sp = {x:sprite.width,y:sprite.height};
if(forceSize) sp = forceSize;
var winratio = bgSize.x/bgSize.y;
var spratio = sp.x/sp.y;
var scale = 1;
var pos = new PIXI.Point(0,0);
if(type == 'cover' ? (winratio > spratio) : (winratio < spratio)) {
//photo is wider than background
scale = bgSize.x/sp.x;
pos.y = -((sp.y*scale)-bgSize.y)/2
} else {
//photo is taller than background
scale = bgSize.y/sp.y;
pos.x = -((sp.x*scale)-bgSize.x)/2
}
sprite.scale = new PIXI.Point(scale,scale);
sprite.position = pos;
}
resize();
return {
container: bgContainer,
doResize: resize
}
}
@only-cliches
Copy link
Author

Not sure how to do the background position stuff, might get a chance later to dig into that.

I updated the gist to have a resize callback, should take care of things for you. 👍

@thibka
Copy link

thibka commented Aug 26, 2019

Hey thanks!
Not sure you need line 19 bgContainer.addChild(mask); though.
Besides, the resize function doesn't work when you actually change the sprite container size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment