简体   繁体   English

Uint8Array和Uint8ClampedArray之间的区别

[英]Difference between Uint8Array and Uint8ClampedArray

What is the difference between Uint8Array and Uint8ClampedArray in JavaScript? JavaScript中的Uint8ArrayUint8ClampedArray什么区别? I understand that Uint8ClampedArray is used with canvas for pixel manipulations. 据我所知, Uint8ClampedArray与canvas用于像素操作。 Why is that and what is the benefit? 为什么这样做有什么好处?

Looking at the examples for Uint8ClampedArray and Uint8Array , it looks like the difference is how values are treated when assigned. 查看Uint8ClampedArrayUint8Array的示例,看起来不同的是分配时如何处理值。

If you are trying to set one element to a clamped array to any value outside of the range 0-255 , it will simply default to 0 or 255 (depending on whether the value is smaller or larger). 如果您尝试将一个元素设置为一个钳位数组到0-255范围之外的任何值,它将默认为0或255(取决于该值是小于还是大)。 A normal Uint8Array array just takes the first 8 bit of the value. 普通的Uint8Array数组只占用该值的前8位。

Examples: 例子:

var x = new Uint8ClampedArray([17, -45.3]);
console.log(x[0]); // 17
console.log(x[1]); // 0
console.log(x.length); // 2

var x = new Uint8Array([17, -45.3]);
console.log(x[0]); // 17
console.log(x[1]); // 211
console.log(x.length); // 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM