It’s possible to edit the html wrapper that Flash Builder generates for the output swf – so that it centres any swf both horizontally and vertically without Flashbuilders default and slightly strange scaling.
The hack comes from how I’ve had this setup in Flashdevelop with SWFObject and only requires a few lines of CSS:
1 2 3 4 5 6 7 8 9 10 11 | <style> #divFloat { position:absolute; top:50%; left:50%; right:50%; bottom:50%; margin:-300px; /* Half the height of your SWF*/ margin-left:-400px; /* Half the width of your SWF*/ } </style> |
To get this to work all that is needed is a div with the matching id ‘divFloat’ added to the html body of the html page that acts as a wrapper for the swf; and this will centre the swf vertically and horizontally.
Based on the CSS above we can device a similar approach and apply it to Flash Builder’s publishing method. The part to figure out is how to insert it into Flash Builders template based creation of the html wrapper.
To do so we must edit the index.template.html in the html-template folder in the Flash Builder project directory: which is the blueprint for the html file generated in the bin-debug folder.
In Flash Builder our html wrapper and swf will be based on what name we choose for our main application file (the one that initializes our application). This value is held by the variable ${application} in index.template.html. It is also the id of our embedded content in the html page that results in bin-debug, based on our index.template.
So we can write in index.template.html:
1 2 3 4 5 6 7 8 9 10 11 12 | <style> body { margin: 0px;} #${application} { position:absolute; top:50%; left:50%; right:50%; bottom:50%; margin:-300px; /* Half the height of your SWF*/ margin-left:-400px; /* Half the width of your SWF*/ } </style> |
If the application class has the name ‘OrangeApp.as’ that will translate into #OrangeApp{ in the resulting html file in the bin-debug folder and target the id of the embedded swf so that the CSS is applied to centre it.
Of course the swf still needs to have the following code in it’s application class:
1 2 |
Also specify the width and height of the swf in the index.template.html as usual by just replacing ‘width’ and ‘height’ with the real width and height of our swf. That will center html the swf in Flash Builder; and remove the pesky default ‘Flex scaling’ without too much work!