Skip to content

Commit dc59296

Browse files
authored
Merge pull request phaserjs#3071 from pavle-goloskokovic/master
Adding fallback for loading files on browsers where URL API is not supported
2 parents a827774 + 29faabd commit dc59296

6 files changed

Lines changed: 62 additions & 335 deletions

File tree

v3/src/loader/File.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,52 @@ var File = new Class({
162162

163163
});
164164

165+
/**
166+
* Static method for creating object URL using URL API and setting it as image 'src' attribute.
167+
* If URL API is not supported (usually on old browsers) it falls back to creating Base64 encoded url using FileReader.
168+
*
169+
* @method createObjectURL
170+
* @static
171+
* @param image {Image} Image object which 'src' attribute should be set to object URL.
172+
* @param blob {Blob} A Blob object to create an object URL for.
173+
* @param defaultType {string} Default mime type used if blob type is not available.
174+
*/
175+
File.createObjectURL = function (image, blob, defaultType)
176+
{
177+
if(URL)
178+
{
179+
image.src = URL.createObjectURL(blob);
180+
}
181+
else
182+
{
183+
var reader = new FileReader();
184+
185+
reader.onload = function()
186+
{
187+
delete image.crossOrigin;
188+
image.src = 'data:' + (blob.type || defaultType) + ';base64,' + reader.result.split(',')[1];
189+
};
190+
191+
reader.onerror = image.onerror;
192+
193+
reader.readAsDataURL(blob);
194+
}
195+
};
196+
197+
/**
198+
* Static method for releasing an existing object URL which was previously created
199+
* by calling {@link File#createObjectURL} method.
200+
*
201+
* @method revokeObjectURL
202+
* @static
203+
* @param image {Image} Image object which 'src' attribute should be revoked.
204+
*/
205+
File.revokeObjectURL = function (image)
206+
{
207+
if(URL)
208+
{
209+
URL.revokeObjectURL(image.src);
210+
}
211+
};
212+
165213
module.exports = File;

v3/src/loader/filetypes/HTMLFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var HTMLFile = new Class({
7474

7575
this.data.onload = function ()
7676
{
77-
URL.revokeObjectURL(_this.data.src);
77+
File.revokeObjectURL(_this.data);
7878

7979
_this.onComplete();
8080

@@ -83,14 +83,14 @@ var HTMLFile = new Class({
8383

8484
this.data.onerror = function ()
8585
{
86-
URL.revokeObjectURL(_this.data.src);
86+
File.revokeObjectURL(_this.data);
8787

8888
_this.state = CONST.FILE_ERRORED;
8989

9090
callback(_this);
9191
};
9292

93-
this.data.src = URL.createObjectURL(blob);
93+
File.createObjectURL(this.data, blob, 'image/svg+xml');
9494
}
9595

9696
});

v3/src/loader/filetypes/ImageFile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var ImageFile = new Class({
5656

5757
this.data.onload = function ()
5858
{
59-
URL.revokeObjectURL(_this.data.src);
59+
File.revokeObjectURL(_this.data);
6060

6161
_this.onComplete();
6262

@@ -65,14 +65,15 @@ var ImageFile = new Class({
6565

6666
this.data.onerror = function ()
6767
{
68-
URL.revokeObjectURL(_this.data.src);
68+
File.revokeObjectURL(_this.data);
6969

7070
_this.state = CONST.FILE_ERRORED;
7171

7272
callback(_this);
7373
};
7474

75-
this.data.src = URL.createObjectURL(this.xhrLoader.response);
75+
File.createObjectURL(this.data, this.xhrLoader.response, 'image/png');
76+
7677
}
7778

7879
});

v3/src/loader/filetypes/SVGFile.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ var SVGFile = new Class({
5656

5757
this.data.onload = function ()
5858
{
59-
URL.revokeObjectURL(_this.data.src);
59+
if(!retry)
60+
{
61+
File.revokeObjectURL(_this.data);
62+
}
6063

6164
_this.onComplete();
6265

@@ -65,16 +68,14 @@ var SVGFile = new Class({
6568

6669
this.data.onerror = function ()
6770
{
68-
URL.revokeObjectURL(_this.data.src);
69-
7071
// Safari 8 re-try
7172
if (!retry)
7273
{
7374
retry = true;
7475

75-
var url = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''));
76+
File.revokeObjectURL(_this.data);
7677

77-
_this.data.src = URL.createObjectURL(url);
78+
_this.data.src = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''));
7879
}
7980
else
8081
{
@@ -84,7 +85,7 @@ var SVGFile = new Class({
8485
}
8586
};
8687

87-
this.data.src = URL.createObjectURL(blob);
88+
File.createObjectURL(this.data, blob, 'image/svg+xml');
8889
}
8990

9091
});

0 commit comments

Comments
 (0)