Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Uint8ClampedArray to regular array

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?

like image 911
cincplug Avatar asked Apr 24 '15 12:04

cincplug


1 Answers

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

like image 122
Austin Greco Avatar answered Sep 18 '22 19:09

Austin Greco