Last active
December 9, 2019 16:56
-
-
Save ConfidantCommunications/49ba6a2ec5e0c64d5742ca6c7da5d54f to your computer and use it in GitHub Desktop.
Fitting OpenFL DisplayObject within bounds and center
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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