harmony 鸿蒙OffscreenCanvas

2022-08-09 浏览 (1096)

OffscreenCanvas

NOTE

The APIs of this component are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

OffscreenCanvas defines a canvas object that can be rendered off the screen.

Attributes

NameTypeDescription
widthnumberWidth of the OffscreenCanvas object.
heightnumberHeight of the OffscreenCanvas object.

Methods

getContext

getContext(type: string, options?: CanvasRenderingContext2DSettings): OffscreenCanvasRenderingContext2D

Obtains the OffscreenCanvas context. This API returns a 2D drawing object.

Parameters

NameTypeMandatoryDescription
contextIdstringYesContext ID. The value can only be "2d".
optionsCanvasRenderingContext2DSettingsNo2D drawing object, which can be used to draw rectangles, images, and texts, on the OffscreenCanvas.

Return value

TypeDescription
OffscreenCanvasRenderingContext2D2D drawing object, which can be used to draw rectangles, images, and texts, on the OffscreenCanvas.

toDataURL

toDataURL(type?: string, quality?:number):

Generates a URL containing image display information.

Parameters

NameTypeMandatoryDescription
typestringNoImage format. The default value is image/png.
qualitynumberNoImage quality, which ranges from 0 to 1, when the image format is image/jpeg or image/webp. If the set value is beyond the value range, the default value 0.92 is used.

Return value

TypeDescription
stringImage URL.

transferToImageBitmap

transferToImageBitmap(): ImageBitmap

Creates an ImageBitmap object on the most recently rendered image of the OffscreenCanvas.

Return value

TypeDescription
ImageBitmapPixel data rendered on the OffscreenCanvas.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvasId" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
</div>
//xxx.js
export default {
  onShow() {
    var canvas = this.$refs.canvasId.getContext('2d');
    var offscreen = new OffscreenCanvas(500,500);
    var offscreenCanvasCtx = offscreen.getContext("2d");

    // ... some drawing for the canvas using the offscreenCanvasCtx ...

    var dataURL = offscreen.toDataURL();
    console.log(dataURL); //data:image/png;base64,xxxxxx

    var bitmap = offscreen.transferToImageBitmap();
    canvas.transferFromImageBitmap(bitmap);
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/046a6637d5fe40c7abf921447d464861