Skip to content

Instantly share code, notes, and snippets.

@ConfidantCommunications
Last active December 9, 2019 16:56
Show Gist options
  • Save ConfidantCommunications/49ba6a2ec5e0c64d5742ca6c7da5d54f to your computer and use it in GitHub Desktop.
Save ConfidantCommunications/49ba6a2ec5e0c64d5742ca6c7da5d54f to your computer and use it in GitHub Desktop.
Fitting OpenFL DisplayObject within bounds and center
var w = 800; //or whatever
var h = 300;
var obj = new Bitmap(someBitmapData);
var s = new Sprite();
s.graphics.beginFill(0x000000,0);
s.graphics.drawRect(0,0,w,h);
s.graphics.endFill();
var hRatio = obj.height / h;
var wRatio = obj.width / w;
if (wRatio > hRatio) {
obj.scaleX = obj.scaleY = 1/wRatio;
} else if (hRatio > wRatio) {
obj.scaleX = obj.scaleY = 1/hRatio;
} else {
obj.scaleX = obj.scaleY = 1;
}
obj.x = (w - obj.width) / 2;
obj.y = (h - obj.height) / 2;
s.addChild(obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment