Skip to content

Commit bd40360

Browse files
authored
Merge pull request phaserjs#3941 from ysraelJMM/master
Resizing of the SVG in the preload
2 parents 65fb366 + b8371eb commit bd40360

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

src/loader/filetypes/SVGFile.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
2525
* A single SVG File suitable for loading by the Loader.
2626
*
2727
* These are created when you use the Phaser.Loader.LoaderPlugin#svg method and are not typically created directly.
28-
*
28+
*
2929
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#svg.
3030
*
3131
* @class SVGFile
@@ -45,7 +45,7 @@ var SVGFile = new Class({
4545

4646
initialize:
4747

48-
function SVGFile (loader, key, url, xhrSettings)
48+
function SVGFile (loader, key, url, svgConfig, xhrSettings)
4949
{
5050
var extension = 'svg';
5151

@@ -55,6 +55,7 @@ var SVGFile = new Class({
5555

5656
key = GetFastValue(config, 'key');
5757
url = GetFastValue(config, 'url');
58+
svgConfig = GetFastValue(config, 'svgConfig');
5859
xhrSettings = GetFastValue(config, 'xhrSettings');
5960
extension = GetFastValue(config, 'extension', extension);
6061
}
@@ -66,6 +67,7 @@ var SVGFile = new Class({
6667
responseType: 'text',
6768
key: key,
6869
url: url,
70+
svgConfig: svgConfig,
6971
xhrSettings: xhrSettings
7072
};
7173

@@ -83,7 +85,27 @@ var SVGFile = new Class({
8385
{
8486
this.state = CONST.FILE_PROCESSING;
8587

86-
var svg = [ this.xhrLoader.responseText ];
88+
var text = this.xhrLoader.responseText;
89+
var svg = [ text ];
90+
var width = this.svgConfig.width;
91+
var height = this.svgConfig.height;
92+
93+
if (width && height)
94+
{
95+
var xml = null;
96+
var parser = new DOMParser();
97+
xml = parser.parseFromString(text, 'text/xml');
98+
var svgXML = xml.getElementsByTagName('svg')[0];
99+
100+
if (svgXML.getAttribute('viewBox'))
101+
{
102+
svgXML.setAttribute('viewBox', '0 0 ' + svgXML.getAttribute('width') + ' ' + svgXML.getAttribute('height'));
103+
}
104+
105+
svgXML.setAttribute('width', width);
106+
svgXML.setAttribute('height', height);
107+
svg = [ (new XMLSerializer()).serializeToString(svgXML) ];
108+
}
87109

88110
try
89111
{
@@ -152,7 +174,7 @@ var SVGFile = new Class({
152174
* Adds an SVG File, or array of SVG Files, to the current load queue.
153175
*
154176
* You can call this method from within your Scene's `preload`, along with any other files you wish to load:
155-
*
177+
*
156178
* ```javascript
157179
* function preload ()
158180
* {
@@ -174,7 +196,7 @@ var SVGFile = new Class({
174196
* then remove it from the Texture Manager first, before loading a new one.
175197
*
176198
* Instead of passing arguments you can pass a configuration object, such as:
177-
*
199+
*
178200
* ```javascript
179201
* this.load.svg({
180202
* key: 'morty',
@@ -185,7 +207,7 @@ var SVGFile = new Class({
185207
* See the documentation for `Phaser.Loader.FileTypes.SVGFileConfig` for more details.
186208
*
187209
* Once the file has finished loading you can use it as a texture for a Game Object by referencing its key:
188-
*
210+
*
189211
* ```javascript
190212
* this.load.svg('morty', 'images/Morty.svg');
191213
* // and later in your game ...
@@ -234,3 +256,4 @@ FileTypesManager.register('svg', function (key, url, xhrSettings)
234256
});
235257

236258
module.exports = SVGFile;
259+

0 commit comments

Comments
 (0)