Convert Uint8ClampedArray to regular array

cincplug picture cincplug · Apr 24, 2015 · Viewed 7.3k times · Source

How can I convert a Uint8ClampedArray (like one used for storing HTML5 canvas image data) to a regular array, in which values won't be constrained to 0-255?

Answer

Austin Greco picture Austin Greco · Apr 25, 2015

You can convert a typed array to a regular array by using Array.prototype.slice

var typedArray = new Uint8ClampedArray([1, 2, 3, 4]);
var normalArray = Array.prototype.slice.call(typedArray);

Also if using ES6 you may be able to use Array.from instead:

var normalArray = Array.from(typedArray);

See MDN - JavaScript typed arrays