Skip to content

Commit b1e3ee4

Browse files
committed
Added base64 to ArrayBuffer and ArrayBuffer to base64
1 parent 4bd3cca commit b1e3ee4

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ The following changes took place in the Pointer class:
9393
* You can now create a desynchronized 2D or WebGL canvas by setting the Game Config property `desynchronized` to `true` (the default is `false`). For more details about what this means see https://developers.google.com/web/updates/2019/05/desynchronized.
9494
* The CanvasRenderer can now use the `transparent` Game Config property in order to tell the browser an opaque background is in use, leading to faster rendering in a 2D context.
9595
* `GameObject.scale` is a new property, that exists as part of the Transform component, that allows you to set the horizontal and vertical scale of a Game Object via a setter, rather than using the `setScale` method. This is handy for uniformly scaling objects via tweens, for example.
96+
* `Base64ToArrayBuffer` is a new utility function that will convert a base64 string into an ArrayBuffer. It works with plain base64 strings, or those with data uri headers attached to them. The resulting ArrayBuffer can be fed to any suitable function that may need it, such as audio decoding.
97+
* `ArrayBufferToBase64` is a new utility function that converts an ArrayBuffer into a base64 string. You can also optionally included a media type, such as `image/jpeg` which will result in a data uri being returned instead of a plain base64 string.
9698

9799
### Updates
98100

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @author Niklas von Hertzen (https://github.com/niklasvh/base64-arraybuffer)
3+
* @author Richard Davey <rich@photonstorm.com>
4+
* @copyright 2019 Photon Storm Ltd.
5+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
6+
*/
7+
8+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
9+
10+
/**
11+
* Converts an ArrayBuffer into a base64 string.
12+
*
13+
* The resulting string can optionally be a data uri if the `mediaType` argument is provided.
14+
*
15+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs for more details.
16+
*
17+
* @function Phaser.Utils.Base64.ArrayBufferToBase64
18+
* @since 3.18.0
19+
*
20+
* @param {ArrayBuffer} arrayBuffer - The Array Buffer to encode.
21+
* @param {string} [mediaType] - An optional media type, i.e. `audio/ogg` or `image/jpeg`. If included the resulting string will be a data URI.
22+
*
23+
* @return {string} The base64 encoded Array Buffer.
24+
*/
25+
var ArrayBufferToBase64 = function (arrayBuffer, mediaType)
26+
{
27+
var bytes = new Uint8Array(arrayBuffer);
28+
var len = bytes.length;
29+
30+
var base64 = (mediaType) ? 'data:' + mediaType + ';base64,' : '';
31+
32+
for (var i = 0; i < len; i += 3)
33+
{
34+
base64 += chars[bytes[i] >> 2];
35+
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
36+
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
37+
base64 += chars[bytes[i + 2] & 63];
38+
}
39+
40+
if ((len % 3) === 2)
41+
{
42+
base64 = base64.substring(0, base64.length - 1) + '=';
43+
}
44+
else if (len % 3 === 1)
45+
{
46+
base64 = base64.substring(0, base64.length - 2) + '==';
47+
}
48+
49+
return base64;
50+
};
51+
52+
module.exports = ArrayBufferToBase64;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @author Niklas von Hertzen (https://github.com/niklasvh/base64-arraybuffer)
3+
* @author Richard Davey <rich@photonstorm.com>
4+
* @copyright 2019 Photon Storm Ltd.
5+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
6+
*/
7+
8+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
9+
10+
// Use a lookup table to find the index.
11+
var lookup = new Uint8Array(256);
12+
13+
for (var i = 0; i < chars.length; i++)
14+
{
15+
lookup[chars.charCodeAt(i)] = i;
16+
}
17+
18+
/**
19+
* Converts a base64 string, either with or without a data uri, into an Array Buffer.
20+
*
21+
* @function Phaser.Utils.Base64.Base64ToArrayBuffer
22+
* @since 3.18.0
23+
*
24+
* @param {string} base64 - The base64 string to be decoded. Can optionally contain a data URI header, which will be stripped out prior to decoding.
25+
*
26+
* @return {ArrayBuffer} An ArrayBuffer decoded from the base64 data.
27+
*/
28+
var Base64ToArrayBuffer = function (base64)
29+
{
30+
// Is it a data uri? if so, strip the header away
31+
base64 = base64.substr(base64.indexOf(',') + 1);
32+
33+
var len = base64.length;
34+
var bufferLength = len * 0.75;
35+
var p = 0;
36+
var encoded1;
37+
var encoded2;
38+
var encoded3;
39+
var encoded4;
40+
41+
if (base64[len - 1] === '=')
42+
{
43+
bufferLength--;
44+
45+
if (base64[len - 2] === '=')
46+
{
47+
bufferLength--;
48+
}
49+
}
50+
51+
var arrayBuffer = new ArrayBuffer(bufferLength);
52+
var bytes = new Uint8Array(arrayBuffer);
53+
54+
for (var i = 0; i < len; i += 4)
55+
{
56+
encoded1 = lookup[base64.charCodeAt(i)];
57+
encoded2 = lookup[base64.charCodeAt(i + 1)];
58+
encoded3 = lookup[base64.charCodeAt(i + 2)];
59+
encoded4 = lookup[base64.charCodeAt(i + 3)];
60+
61+
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
62+
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
63+
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
64+
}
65+
66+
return arrayBuffer;
67+
};
68+
69+
module.exports = Base64ToArrayBuffer;

src/utils/base64/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* @namespace Phaser.Utils.Base64
9+
*/
10+
11+
module.exports = {
12+
13+
ArrayBufferToBase64: require('./ArrayBufferToBase64'),
14+
Base64ToArrayBuffer: require('./Base64ToArrayBuffer')
15+
16+
};

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module.exports = {
1212

1313
Array: require('./array/'),
14+
Base64: require('./base64/'),
1415
Objects: require('./object/'),
1516
String: require('./string/')
1617

0 commit comments

Comments
 (0)