问题
Does anyone happen to know how far back the current canvas pixel-data spec goes in various browsers? And if "not that far", what the previous spec(s) were?
When you call or push pixels, you get / send:
ImageData
data: Uint8ClampedArray
width: Number
height: Number
But I know that it used to just be:
{
data: Array
width: Number
height: Number
}
So if I'm generating ImageData, how do I detect what I need to generate? Is ImageData && Uint8ClampedArray
sufficient? And do I immediately fall back to Object / Array
, or something else? Wasn't there a pixel array at some point?
[edit] caniuse says IE10 and Opera Mini don't support Uint8ClampedArray, so... ?
回答1:
When calling ctx.getImageData()
, the specs require the UserAgent to return a TypedArray without specifying which type. The support for TypedArray start with IE10. But, like in IE9, the data property of the ImageData object is a CanvasPixelArray
, which has no Constructor method available.
But anyway, the best and IMK only cross-browser way to get the correct ArrayBuffer needed for ImageData
Object, is to call ctx.createImageData(width,height).
Then you can get its data
array and you are sure its type will be correct.
来源:https://stackoverflow.com/questions/32249222/cross-browser-spec-for-canvas-pixel-data