相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Looking at the examples for Uint8ClampedArray and Uint8Array , it looks like the difference is how values are treated when assigned.

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). A normal Uint8Array array just takes the first 8 bit of the value.

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
                Note also that Uint8Array([0.9]) is [0], but Uint8ClampedArray([0.9]) is [1], ie. the clamped version uses rounding, but the basic version uses floor.
– dan-man
                May 16, 2016 at 9:22
                The OP mentioned this was used for pixel values in the canvas, is this a shortcut for saturated arithmetic?
– Indolering
                Sep 30, 2016 at 19:54
                I think Uint8Array would be faster - unless you were using the clamping features for graphics, which maybe faster in that case.
– Tomachi
                Nov 2, 2018 at 10:39
                @llya These are for image/video/audio process. The main feature is: it never overflow/underflow.  When you are trying to increase the brightness in an image: max brightness (255)+1 should give 255, not overflow to zero.
– J-16 SDiZ
                Apr 4, 2019 at 6:26
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章