Skip to content

Latest commit

 

History

History
135 lines (106 loc) · 5.57 KB

File metadata and controls

135 lines (106 loc) · 5.57 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs ms.assetid caps.latest.revision author ms.author manager
Uint8ClampedArray Object (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
0c5537f7-00b4-487a-8fba-ef032e67e7bd
6
mikejo5000
mikejo
ghogen

Uint8ClampedArray Object (JavaScript)

A typed array of 8-bit unsigned integers with values clamped to the range 0-255. The contents are initialized to 0. If the requested number of bytes cannot be allocated, an exception is thrown.

Syntax

  
      uint8ClampedArray = new Uint8ClampedArray( length );  
uint8ClampedArray = new Uint8ClampedArray( array );  
uint8ClampedArray = new Uint8ClampedArray( buffer, byteOffset, length);  

Parameters

uint8ClampedArray
Required. The variable name to which the Uint8ClampedArray object is assigned.

length
Optional. The number of elements in the array.

array
Optional. The array (or typed array) that this array contains. The contents are initialized to the contents of the given array or typed array, with each element converted to the Uint8 type.

buffer
Optional. The ArrayBuffer that the Uint8ClampedArray represents.

byteOffset
Optional. The offset, in bytes, from the start of the buffer at which the Uint8ClampedArray should begin.

length
Optional. The number of elements in the array.

Remarks

Values stored in a Uint8ClampedArray object are between 0 and 255. If you set a negative value for an array member, 0 is set for the value. If you set a value that is larger than 255, 255 is set as the value.

Values in a Uint8ClampedArray object are rounded to the nearest even value, which is called banker's rounding.

Constants

The following table lists the constants of the Uint8ClampedArray object.

Constant Description
BYTES_PER_ELEMENT Constant The size, in bytes, of each element in the array.

Properties

The following table lists the constants of the Uint8ClampedArray object.

Property Description
buffer Property Read-only. Gets the ArrayBuffer that is referenced by this array.
byteLength Property Read-only. The length of this array from the start of its ArrayBuffer, in bytes, as fixed at construction time.
byteOffset Property Read-only. The offset of this array from the start of its ArrayBuffer, in bytes, as fixed at construction time.
length Property The length of the array.

Methods

The following table lists the methods of the Uint8ClampedArray object.

Method Description
set Method Sets a value or an array of values.
subarray Method Gets a new Uint8ClampedArray view of the ArrayBuffer store for this array, specifying the first and last elements of the subarray.

Example

The following example shows how to use a Uint8ClampedArray object to process the binary data acquired from an XmlHttpRequest:

var req = new XMLHttpRequest();  
    req.open('GET', "http://www.example.com");  
    req.responseType = "arraybuffer";  
    req.send();  
  
    req.onreadystatechange = function () {  
        if (req.readyState === 4) {  
            var buffer = req.response;  
            var dataview = new DataView(buffer);  
            var ints = new Uint8ClampedArray(buffer.byteLength);  
            for (var i = 0; i < ints.length; i++) {  
                ints[i] = dataview.getUint8(i);  
            }  
        alert(ints[10]);  
        }  
    }  
  

Example

The following example shows how values are restricted in a Uint8ClampedArray.

var ints = new Uint8ClampedArray(2);  
ints[0] = -1;  // 0 will be assigned.  
ints[1] = 256; // 255 will be assigned.  
  

Example

The following example shows how values are rounded in a Uint8ClampedArray.

var ints = new Uint8ClampedArray(4);  
ints[0] = 11.3; // 11 will be assigned (same as Int8Array).  
ints[1] = 11.8; // 12 will be assigned (same as Int8Array).  
ints[2] = 10.5; // 10 will be assigned (rounded to the nearest   
                // even value).  
ints[3] = 11.5; // 12 will be assigned (rounded to the nearest   
                // even value).  
  

Requirements

[!INCLUDEjsv11_winonly]

See Also

Uint8Array Object
ArrayBuffer Object