Skip to content

Latest commit

 

History

History

uint8c

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Uint8ClampedArray

Typed array constructor which returns a typed array representing an array of 8-bit unsigned integers in the platform byte order clamped to 0-255.

Usage

var Uint8ClampedArray = require( '@stdlib/array/uint8c' );

Uint8ClampedArray()

A typed array constructor which returns a typed array representing an array of 8-bit unsigned integers in the platform byte order clamped to 0-255.

var arr = new Uint8ClampedArray();
// returns <Uint8ClampedArray>

Uint8ClampedArray( length )

Returns a typed array having a specified length.

var arr = new Uint8ClampedArray( 5 );
// returns <Uint8ClampedArray>[ 0, 0, 0, 0, 0 ]

Uint8ClampedArray( typedarray )

Creates a typed array from another typed array.

var Float32Array = require( '@stdlib/array/float32' );

var arr1 = new Float32Array( [ 5.0, 5.0, 5.0 ] );
var arr2 = new Uint8ClampedArray( arr1 );
// returns <Uint8ClampedArray>[ 5, 5, 5 ]

Uint8ClampedArray( obj )

Creates a typed array from an array-like object or iterable.

var arr = new Uint8ClampedArray( [ 5.0, 5.0, 5.0 ] );
// returns <Uint8ClampedArray>[ 5, 5, 5 ]

Uint8ClampedArray( buffer[, byteOffset[, length]] )

Returns a typed array view of an ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array/buffer' );

var buf = new ArrayBuffer( 4 );
var arr = new Uint8ClampedArray( buf, 0, 4 );
// returns <Uint8ClampedArray>[ 0, 0, 0, 0 ]

Properties

Uint8ClampedArray.BYTES_PER_ELEMENT

Number of bytes per view element.

var nbytes = Uint8ClampedArray.BYTES_PER_ELEMENT;
// returns 1

Uint8ClampedArray.name

Typed array constructor name.

var str = Uint8ClampedArray.name;
// returns 'Uint8ClampedArray'

Uint8ClampedArray.prototype.buffer

Read-only property which returns the ArrayBuffer referenced by the typed array.

var arr = new Uint8ClampedArray( 5 );
var buf = arr.buffer;
// returns <ArrayBuffer>

Uint8ClampedArray.prototype.byteLength

Read-only property which returns the length (in bytes) of the typed array.

var arr = new Uint8ClampedArray( 5 );
var byteLength = arr.byteLength;
// returns 5

Uint8ClampedArray.prototype.byteOffset

Read-only property which returns the offset (in bytes) of the typed array from the start of its ArrayBuffer.

var arr = new Uint8ClampedArray( 5 );
var byteOffset = arr.byteOffset;
// returns 0

Uint8ClampedArray.prototype.BYTES_PER_ELEMENT

Number of bytes per view element.

var arr = new Uint8ClampedArray( 5 );
var nbytes = arr.BYTES_PER_ELEMENT;
// returns 1

Uint8ClampedArray.prototype.length

Read-only property which returns the number of view elements.

var arr = new Uint8ClampedArray( 5 );
var len = arr.length;
// returns 5

Methods

Uint8ClampedArray.from( src[, map[, thisArg]] )

Creates a new typed array from an array-like object or an iterable.

var arr = Uint8ClampedArray.from( [ 1, 2 ] );
// returns <Uint8ClampedArray>[ 1, 2 ]

To invoke a function for each src value, provide a callback function.

function mapFcn( v ) {
    return v * 2;
}

var arr = Uint8ClampedArray.from( [ 1, 2 ], mapFcn );
// returns <Uint8ClampedArray>[ 2, 4 ]

A callback function is provided two arguments:

  • value: source value
  • index: source index

To set the callback execution context, provide a thisArg.

function mapFcn( v ) {
    this.count += 1;
    return v * 2;
}

var ctx = {
    'count': 0
};

var arr = Uint8ClampedArray.from( [ 1, 2 ], mapFcn, ctx );
// returns <Uint8ClampedArray>[ 2, 4 ]

var n = ctx.count;
// returns 2

Uint8ClampedArray.of( element0[, element1[, ...[, elementN]]] )

Creates a new typed array from a variable number of arguments.

var arr = Uint8ClampedArray.of( 1, 2 );
// returns <Uint8ClampedArray>[ 1, 2 ]

Examples

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var ctor = require( '@stdlib/array/uint8c' );

var arr;
var i;

arr = new ctor( 10 );
for ( i = 0; i < arr.length; i++ ) {
    arr[ i ] = round( randu()*100.0 );
}
console.log( arr );