Game Development Reference
In-Depth Information
one whole element, which would be fine. A 16 bits element would only fit half of an
element, which is not possible. A 32 bits element would likewise only fit a portion of
it, which is also not allowed.
As you can see, as long as the array buffer has a bit length that is a multiple of the
bit size of the data type used in the view, things work out fine. If the view is 8 bits
long, then an array buffer of 8, 16, 24, 32, or 40, would work out fine. If the view is
32 bits long, then the buffer must be at least 4 bytes long (32 bits), 8 bytes (64 bits),
24 bytes (96 bits), and so on. Then, by dividing the amount of bytes in the buffer by
the amount of bytes in the data type represented by the view, we can calculate the
total number of elements that we can fit in said array.
// 96 bytes in the buffer
var buffer = new ArrayBuffer(96);
// Each element in the buffer is 32 bits long,
or 4 bytes
var view = new Int32Array(buffer);
// 96 / 4 = 24 elements in this typed array
view.length == 24;
Typed array view types
As a summary, a plain old array buffer has no actual size. Although it wouldn't make
sense to create an array buffer of a byte length of say 5 bytes, we are more than wel-
come to do so. Only after the array buffer is created can we create a view to hold the
buffer. Based on the byte size of the buffer, we can determine how many elements
the array buffer view can access by selecting an appropriate data type. Currently,
there are nine data types that we can choose from for the array buffer view.
Int8Array : It is a signed integer , 8 bits long, ranging from 32,768 to
32,767
Search WWH ::




Custom Search