Created
June 4, 2015 14:28
-
-
Save mikkoh/f8c5148871bb3ba57455 to your computer and use it in GitHub Desktop.
Converting between TypedArrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To convert between typed arrays an ArrayBuffer can be used to create | |
* TypedArray's from one type to another. Each typed array has a property | |
* which is an ArrayBuffer. | |
**/ | |
var uint16 = new Uint16Array([0x0102, 0x0304]); | |
var uint32 = new Uint32Array(uint16.buffer); | |
var uint8clamped = new Uint8ClampedArray(uint16.buffer); | |
var uint8 = new Uint8Array(uint16.buffer); | |
console.log('uint8', uint8); // uint8 [2, 1, 4, 3] | |
console.log('uint8clamped', uint8clamped); // [2, 1, 4, 3] | |
console.log('uint16', uint16); // [258, 772] | |
console.log('uint32', uint32); // [50594050] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment