From 134aa0203cdb1b41205e615bcbe5b8d25ff41a93 Mon Sep 17 00:00:00 2001 From: limeng Date: Sat, 27 May 2023 18:43:20 +0800 Subject: [PATCH] modify by the comments Signed-off-by: limeng --- .../figures/offscreen_canvas_height.png | Bin 0 -> 2589 bytes ...offscreen_canvas_transferToImageBitmap.png | Bin 0 -> 7358 bytes .../figures/offscreen_canvas_width.png | Bin 0 -> 2596 bytes .../arkui-ts/ts-components-offscreencanvas.md | 76 ++++++++++++------ 4 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 zh-cn/application-dev/reference/arkui-ts/figures/offscreen_canvas_height.png create mode 100644 zh-cn/application-dev/reference/arkui-ts/figures/offscreen_canvas_transferToImageBitmap.png create mode 100644 zh-cn/application-dev/reference/arkui-ts/figures/offscreen_canvas_width.png diff --git a/zh-cn/application-dev/reference/arkui-ts/figures/offscreen_canvas_height.png b/zh-cn/application-dev/reference/arkui-ts/figures/offscreen_canvas_height.png new file mode 100644 index 0000000000000000000000000000000000000000..69818ec80dd321547d5dfc97333409512f1e83d3 GIT binary patch literal 2589 zcmeAS@N?(olHy`uVBq!ia0y~yVANn>U~1uD0*Y7%8@K@}#^NA%Cx&(BWL^R}Ea{HE zjtmSN`?>!lvI6;>1s;*b3=DjSL74G){)!Z!*d|XG$B>G+w>KQ~r7L9{9!lQMH5b`7 zRjkwc{sQ)WceyUEwfOjUN5R9Ol}wyhY!z;7Ix;UYN&2#{_u|~k`G#xkDtA6KYZKgb-he~!*?_O)h~5B|DOM_?fX7^IY#+};-3#HzRZ$u*?#Bgv!_=I zzm}Qv#S}k(wx0LTji1tYcmB*~wlDkVR-X6IR?lMn_xQ~+x$S5FvHiIJ;n`Iaztxp8 z$DUsPUszi4_RQ|-r;q2J*LPn3e~x*~2is@U^B>M-wvW)0H;?_0e185_;Y~XpHkCi* z+_@vh|8ZVa{{H)avt`<^zEVH_VcM&C_J0qbZ*y-Cd?0rA@D^FQ8dIB?|IZFqnV&t} zTv)No@(=H?PdB^u+mE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``#=YhDw~>*b)_wWFLH9HIe$30Cp3MAr>p z^!%9F;}8CMWKs|{Zw|2iA_LOoX#fBF)nC&;Kb1c|{e1oJ*@<7%Hrq;@-=Ck`E`RLd z#mT4hr~R$t+u{4V(&qp5A5}k&&;D%wI4`dM_lIfzKC{hF>lW5nCqI~WW_IF@xV!(& zribs;?RzNe(_e2P$GAP+Soiz3%Afi9#&3(I+x|05FF4JU82GaZ*s^2rboFyt=akR{ E03Yt95C8xG literal 0 HcmV?d00001 diff --git a/zh-cn/application-dev/reference/arkui-ts/ts-components-offscreencanvas.md b/zh-cn/application-dev/reference/arkui-ts/ts-components-offscreencanvas.md index a88519e059..b8ff815c0b 100644 --- a/zh-cn/application-dev/reference/arkui-ts/ts-components-offscreencanvas.md +++ b/zh-cn/application-dev/reference/arkui-ts/ts-components-offscreencanvas.md @@ -1,6 +1,8 @@ # OffscreenCanvas -提供离屏画布组件,用于自定义绘制图形。 +OffscreenCanvas组件用于自定义绘制图形。 + +使用Canvas组件或Canvas API时,渲染、动画和用户交互通常发生在应用程序的主线程上。与画布动画和渲染相关的计算可能会对应用程序性能产生重大影响。OffscreenCanvas提供了一个可以在屏幕外渲染的画布,这样可以在单独的线程中运行一些任务,从而避免主线程上的繁重工作。 > **说明:** > @@ -18,10 +20,10 @@ OffscreenCanvas(width: number, height: number) **参数:** -| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | -| ------ | -------- | ---- | ------ | -------------------------- | -| width | number | 是 | 0 | 离屏画布的宽度,单位为vp。 | -| height | number | 是 | 0 | 离屏画布的高度,单位为vp。 | +| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | +| ------ | -------- | ---- | ------ | ------------------------------------- | +| width | number | 是 | 0 | OffscreenCanvas组件的宽度,单位为vp。 | +| height | number | 是 | 0 | OffscreenCanvas组件的高度,单位为vp。 | ## 属性 @@ -29,8 +31,8 @@ OffscreenCanvas(width: number, height: number) | 名称 | 类型 | 默认值 | 描述 | | ------ | ------ | ------ | ------------------------------------------------------------ | -| width | number | 0 | 离屏画布的宽度,单位为vp。从API version 9开始,该接口支持在ArkTS卡片中使用。 | -| height | number | 0 | 离屏画布的高度,单位为vp。从API version 9开始,该接口支持在ArkTS卡片中使用。 | +| width | number | 0 | OffscreenCanvas组件的宽度,单位为vp。从API version 9开始,该接口支持在ArkTS卡片中使用。 | +| height | number | 0 | OffscreenCanvas组件的高度,单位为vp。从API version 9开始,该接口支持在ArkTS卡片中使用。 | ### width @@ -41,7 +43,7 @@ OffscreenCanvas(width: number, height: number) struct OffscreenCanvasPage { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); - private offscreenCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600) + private offCanvas: OffscreenCanvas = new OffscreenCanvas(200, 300) build() { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { @@ -49,9 +51,14 @@ struct OffscreenCanvasPage { Canvas(this.context) .width('100%') .height('100%') - .backgroundColor('#ffff00') + .backgroundColor('#FFFFFF') .onReady(() => { - console.log(this.offscreenCanvas.width.toString()) + var offContext = this.offCanvas.getContext("2d", this.settings) + offContext.fillStyle = '#CDCDCD' + offContext.fillRect(0, 0, offCanvas.width, 150) + var image = this.offCanvas.transferToImageBitmap() + this.context.setTransform(1, 0, 0, 1, 50, 200) + this.context.transferFromImageBitmap(image) }) } }.width('100%').height('100%') @@ -59,6 +66,8 @@ struct OffscreenCanvasPage { } ``` +![zh-cn_image_0000001194032666](figures/offscreen_canvas_width.png) + ### height ```ts @@ -68,7 +77,7 @@ struct OffscreenCanvasPage { struct OffscreenCanvasPage { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); - private offscreenCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600) + private offCanvas: OffscreenCanvas = new OffscreenCanvas(200, 300) build() { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { @@ -76,9 +85,14 @@ struct OffscreenCanvasPage { Canvas(this.context) .width('100%') .height('100%') - .backgroundColor('#ffff00') + .backgroundColor('#FFFFFF') .onReady(() => { - console.log(this.offscreenCanvas.height.toString()) + var offContext = this.offCanvas.getContext("2d", this.settings) + offContext.fillStyle = '#CDCDCD' + offContext.fillRect(0, 0, 100, offCanvas.height) + var image = this.offCanvas.transferToImageBitmap() + this.context.setTransform(1, 0, 0, 1, 50, 200) + this.context.transferFromImageBitmap(image) }) } }.width('100%').height('100%') @@ -86,13 +100,15 @@ struct OffscreenCanvasPage { } ``` +![zh-cn_image_0000001194032666](figures/offscreen_canvas_height.png) + ## 方法 ### transferToImageBitmap transferToImageBitmap(): ImageBitmap -从离屏画布中最近渲染的图像创建一个ImageBitmap对象。 +从OffscreenCanvas组件中最近渲染的图像创建一个ImageBitmap对象。 从API version 9开始,该接口支持在ArkTS卡片中使用。 @@ -111,20 +127,25 @@ transferToImageBitmap(): ImageBitmap struct OffscreenCanvasPage { private settings: RenderingContextSettings = new RenderingContextSettings(true) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) - private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600) + private offCanvas: OffscreenCanvas = new OffscreenCanvas(300, 500) build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Canvas(this.context) .width('100%') .height('100%') - .backgroundColor('#ffff00') + .borderWidth(5) + .borderColor(Color.Green) + .backgroundColor('#FFFFFF') .onReady(() => { var offContext = this.offCanvas.getContext("2d", this.settings) - offContext.font = '70px sans-serif' + offContext.fillStyle = '#CDCDCD' + offContext.fillRect(0, 0, 300, 500) + offContext.fillStyle = '#000000' + offContext.font = '70px serif bold' offContext.fillText("Offscreen : Hello World!", 20, 60) - var imageData = this.offCanvas.transferToImageBitmap() - this.context.transferFromImageBitmap(imageData) + var image = this.offCanvas.transferToImageBitmap() + this.context.transferFromImageBitmap(image) }) } .width('100%') @@ -132,24 +153,27 @@ struct OffscreenCanvasPage { } } ``` + +![zh-cn_image_0000001194032666](figures/offscreen_canvas_transferToImageBitmap.png) + ### getContext10+ getContext(contextType: "2d", option?: RenderingContextSettings): OffscreenCanvasRenderingContext2D -返回离屏画布的绘图上下文。 +返回OffscreenCanvas组件的绘图上下文。 **参数:** | 参数 | 类型 | 必填 | 默认值 | 说明 | | ----------- | ------------------------------------------------------------ | ---- | ------ | ------------------------------------------------------------ | -| contextType | string | 是 | "2d" | 离屏画布绘图上下文的类型。 | +| contextType | string | 是 | "2d" | OffscreenCanvas组件绘图上下文的类型。 | | option | [RenderingContextSettings](ts-canvasrenderingcontext2d.md#renderingcontextsettings) | 否 | - | 见[RenderingContextSettings](ts-canvasrenderingcontext2d.md#renderingcontextsettings)。 | **返回值:** -| 类型 | 描述 | -| ------------------------------------------------------------ | ---------------------- | -| [OffscreenCanvasRenderingContext2D](ts-offscreencanvasrenderingcontext2d.md) | 离屏画布的绘图上下文。 | +| 类型 | 描述 | +| ------------------------------------------------------------ | --------------------------------- | +| [OffscreenCanvasRenderingContext2D](ts-offscreencanvasrenderingcontext2d.md) | OffscreenCanvas组件的绘图上下文。 | **示例:** @@ -159,7 +183,7 @@ getContext(contextType: "2d", option?: RenderingContextSettings): OffscreenCanva struct OffscreenCanvasExamplePage { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); - private offscreenCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600) + private offscreenCanvas: OffscreenCanvas = new OffscreenCanvas(600, 800) build() { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { @@ -200,5 +224,5 @@ struct OffscreenCanvasExamplePage { } ``` - ![zh-cn_image_0000001194032666](figures/offscreen_canvas.png) +![zh-cn_image_0000001194032666](figures/offscreen_canvas.png) -- GitLab