存儲canvas渲染的像素數(shù)據(jù)

2024-01-22 18:13 更新

ImageData對象可以存儲canvas渲染的像素數(shù)據(jù)。

說明

從 API Version 8 開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。

屬性

屬性類型描述
widthnumber

矩形區(qū)域?qū)嶋H像素寬度,單位為px。

從API version 9開始,該接口支持在ArkTS卡片中使用。

heightnumber

矩形區(qū)域?qū)嶋H像素高度,單位為px。

從API version 9開始,該接口支持在ArkTS卡片中使用。

dataUint8ClampedArray

一維數(shù)組,保存了相應的顏色數(shù)據(jù),數(shù)據(jù)值范圍為0到255。

從API version 9開始,該接口支持在ArkTS卡片中使用。

說明

可使用px2vp接口進行單位轉(zhuǎn)換。

示例:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Translate {
  5. private settings: RenderingContextSettings = new RenderingContextSettings(true)
  6. private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  7. private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
  8. build() {
  9. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  10. Canvas(this.context)
  11. .width('100%')
  12. .height('100%')
  13. .backgroundColor('#ffff00')
  14. .onReady(() =>{
  15. this.context.drawImage(this.img,0,0,130,130)
  16. var imagedata = this.context.getImageData(50,50,130,130)
  17. this.context.putImageData(imagedata,150,150)
  18. })
  19. }
  20. .width('100%')
  21. .height('100%')
  22. }
  23. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號