Cross-browser spec for canvas pixel data?

青春壹個敷衍的年華 提交于 2019-12-19 23:24:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!