Skip to content

Commit acea677

Browse files
committed
Added ability to load HTML files into textures.
1 parent 6f115a6 commit acea677

3 files changed

Lines changed: 105 additions & 1 deletion

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: '7ca218d0-18d8-11e7-adaa-6d4ae57a90ac'
2+
build: '74477d20-1936-11e7-8c5f-29386239866f'
33
};
44
module.exports = CHECKSUM;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
var CONST = require('../const');
3+
var File = require('../File');
4+
5+
var HTMLFile = function (key, url, width, height, path, xhrSettings)
6+
{
7+
if (width === undefined) { width = 512; }
8+
if (height === undefined) { height = 512; }
9+
if (path === undefined) { path = ''; }
10+
11+
if (!key)
12+
{
13+
throw new Error('Error calling \'Loader.html\' invalid key provided.');
14+
}
15+
16+
if (!url)
17+
{
18+
url = path + key + '.html';
19+
}
20+
else
21+
{
22+
url = path.concat(url);
23+
}
24+
25+
var config = {
26+
width: width,
27+
height: height
28+
};
29+
30+
File.call(this, 'html', key, url, 'text', xhrSettings, config);
31+
};
32+
33+
HTMLFile.prototype = Object.create(File.prototype);
34+
HTMLFile.prototype.constructor = HTMLFile;
35+
36+
HTMLFile.prototype.onProcess = function (callback)
37+
{
38+
this.state = CONST.FILE_PROCESSING;
39+
40+
var w = this.config.width;
41+
var h = this.config.height;
42+
43+
var data = [];
44+
45+
data.push('<svg width="' + w + 'px" height="' + h + 'px" viewBox="0 0 ' + w + ' ' + h + '" xmlns="http://www.w3.org/2000/svg">');
46+
data.push('<foreignObject width="100%" height="100%">');
47+
data.push('<body xmlns="http://www.w3.org/1999/xhtml">');
48+
data.push(this.xhrLoader.responseText);
49+
data.push('</body>');
50+
data.push('</foreignObject>');
51+
data.push('</svg>');
52+
53+
var svg = [ data.join('\n') ];
54+
55+
try
56+
{
57+
var blob = new window.Blob(svg, { type: 'image/svg+xml;charset=utf-8' });
58+
}
59+
catch (e)
60+
{
61+
_this.state = CONST.FILE_ERRORED;
62+
63+
callback(_this);
64+
65+
return;
66+
}
67+
68+
this.data = new Image();
69+
70+
this.data.crossOrigin = this.crossOrigin;
71+
72+
var _this = this;
73+
74+
this.data.onload = function ()
75+
{
76+
URL.revokeObjectURL(_this.data.src);
77+
78+
_this.onComplete();
79+
80+
callback(_this);
81+
};
82+
83+
this.data.onerror = function ()
84+
{
85+
URL.revokeObjectURL(_this.data.src);
86+
87+
_this.state = CONST.FILE_ERRORED;
88+
89+
callback(_this);
90+
};
91+
92+
this.data.src = URL.createObjectURL(blob);
93+
};
94+
95+
module.exports = HTMLFile;

v3/src/state/systems/Loader.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
1212
var BitmapFontFile = require('../../loader/filetypes/BitmapFontFile');
1313
var SpriteSheet = require('../../loader/filetypes/SpriteSheet');
1414
var SVGFile = require('../../loader/filetypes/SVGFile');
15+
var HTMLFile = require('../../loader/filetypes/HTMLFile');
1516

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

@@ -130,6 +131,13 @@ Loader.prototype.spritesheet = function (key, url, config, xhrSettings)
130131
return this.addFile(file);
131132
};
132133

134+
Loader.prototype.html = function (key, url, width, height, xhrSettings)
135+
{
136+
var file = new HTMLFile(key, url, width, height, this.path, xhrSettings);
137+
138+
return this.addFile(file);
139+
};
140+
133141
Loader.prototype.atlas = function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
134142
{
135143
// Returns an object with two properties: 'texture' and 'data'
@@ -276,6 +284,7 @@ Loader.prototype.processCallback = function ()
276284
{
277285
case 'image':
278286
case 'svg':
287+
case 'html':
279288
textures.addImage(file.key, file.data);
280289
break;
281290

0 commit comments

Comments
 (0)