Skip to content

Commit 6f115a6

Browse files
committed
Added ability for the Loader to handle loading SVG files and creating Images from them (for use as Sprite textures)
1 parent 9c3ce04 commit 6f115a6

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '8a8de700-18cc-11e7-bc06-b1929cecfc5e'
2+
build: '7ca218d0-18d8-11e7-adaa-6d4ae57a90ac'
33
};
44
module.exports = CHECKSUM;

v3/src/loader/filetypes/ImageFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var CONST = require('../const');
33
var File = require('../File');
44

5-
var ImageFile = function (key, url, path, xhrSettings, options)
5+
var ImageFile = function (key, url, path, xhrSettings, config)
66
{
77
if (path === undefined) { path = ''; }
88

@@ -20,7 +20,7 @@ var ImageFile = function (key, url, path, xhrSettings, options)
2020
url = path.concat(url);
2121
}
2222

23-
File.call(this, 'image', key, url, 'blob', xhrSettings, options);
23+
File.call(this, 'image', key, url, 'blob', xhrSettings, config);
2424
};
2525

2626
ImageFile.prototype = Object.create(File.prototype);

v3/src/loader/filetypes/SVGFile.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
var CONST = require('../const');
3+
var File = require('../File');
4+
5+
var SVGFile = function (key, url, path, xhrSettings)
6+
{
7+
if (path === undefined) { path = ''; }
8+
9+
if (!key)
10+
{
11+
throw new Error('Error calling \'Loader.svg\' invalid key provided.');
12+
}
13+
14+
if (!url)
15+
{
16+
url = path + key + '.svg';
17+
}
18+
else
19+
{
20+
url = path.concat(url);
21+
}
22+
23+
File.call(this, 'svg', key, url, 'text', xhrSettings);
24+
};
25+
26+
SVGFile.prototype = Object.create(File.prototype);
27+
SVGFile.prototype.constructor = SVGFile;
28+
29+
SVGFile.prototype.onProcess = function (callback)
30+
{
31+
this.state = CONST.FILE_PROCESSING;
32+
33+
var svg = [ this.xhrLoader.responseText ];
34+
35+
try
36+
{
37+
var blob = new window.Blob(svg, { type: 'image/svg+xml;charset=utf-8' });
38+
}
39+
catch (e)
40+
{
41+
_this.state = CONST.FILE_ERRORED;
42+
43+
callback(_this);
44+
45+
return;
46+
}
47+
48+
this.data = new Image();
49+
50+
this.data.crossOrigin = this.crossOrigin;
51+
52+
var _this = this;
53+
var retry = false;
54+
55+
this.data.onload = function ()
56+
{
57+
URL.revokeObjectURL(_this.data.src);
58+
59+
_this.onComplete();
60+
61+
callback(_this);
62+
};
63+
64+
this.data.onerror = function ()
65+
{
66+
URL.revokeObjectURL(_this.data.src);
67+
68+
// Safari 8 re-try
69+
if (!retry)
70+
{
71+
retry = true;
72+
73+
var url = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''));
74+
75+
_this.data.src = URL.createObjectURL(url);
76+
}
77+
else
78+
{
79+
_this.state = CONST.FILE_ERRORED;
80+
81+
callback(_this);
82+
}
83+
};
84+
85+
this.data.src = URL.createObjectURL(blob);
86+
};
87+
88+
module.exports = SVGFile;

v3/src/state/systems/Loader.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var TextFile = require('../../loader/filetypes/TextFile');
1111
var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
1212
var BitmapFontFile = require('../../loader/filetypes/BitmapFontFile');
1313
var SpriteSheet = require('../../loader/filetypes/SpriteSheet');
14+
var SVGFile = require('../../loader/filetypes/SVGFile');
1415

1516
var ParseXMLBitmapFont = require('../../gameobjects/bitmaptext/ParseXMLBitmapFont');
1617

@@ -55,6 +56,7 @@ Loader.prototype.file = function (file)
5556
case 'binary':
5657
case 'text':
5758
case 'glsl':
59+
case 'svg':
5860
entry = this[file.type](file.key, file.url, file.xhrSettings);
5961
break;
6062

@@ -150,6 +152,13 @@ Loader.prototype.bitmapFont = function (key, textureURL, xmlURL, textureXhrSetti
150152
return this;
151153
};
152154

155+
Loader.prototype.svg = function (key, url, xhrSettings)
156+
{
157+
var file = new SVGFile(key, url, this.path, xhrSettings);
158+
159+
return this.addFile(file);
160+
};
161+
153162
Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs, textureXhrSettings, atlasXhrSettings)
154163
{
155164
if (typeof textureURLs === 'number')
@@ -266,6 +275,7 @@ Loader.prototype.processCallback = function ()
266275
switch (file.type)
267276
{
268277
case 'image':
278+
case 'svg':
269279
textures.addImage(file.key, file.data);
270280
break;
271281

0 commit comments

Comments
 (0)