1e41f4b71Sopenharmony_ci# CanvasRenderingContext2D 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci使用RenderingContext在Canvas组件上进行绘制,绘制对象可以是矩形、文本、图片等。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 接口 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciCanvasRenderingContext2D(settings?: RenderingContextSettings, unit?: LengthMetricsUnit) 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**原子化服务API:** 在API version 11中,该接口支持在原子化服务中使用。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**参数:** 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 24e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 25e41f4b71Sopenharmony_ci| settings | [RenderingContextSettings](#renderingcontextsettings) | 否 | 用来配置CanvasRenderingContext2D对象的参数,见[RenderingContextSettings](#renderingcontextsettings)。 | 26e41f4b71Sopenharmony_ci| unit<sup>12+</sup> | [LengthMetricsUnit](../js-apis-arkui-graphics.md#lengthmetricsunit12) | 否 | 用来配置CanvasRenderingContext2D对象的单位模式,配置后无法更改,见[LengthMetricsUnit](#lengthmetricsunit12)。<br>默认值:DEFAULT。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci### RenderingContextSettings 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciRenderingContextSettings(antialias?: boolean) 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci用来配置CanvasRenderingContext2D对象的参数,包括是否开启抗锯齿。 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**参数:** 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 44e41f4b71Sopenharmony_ci| --------- | ------- | ---- | ----------------------------- | 45e41f4b71Sopenharmony_ci| antialias | boolean | 否 | 表明canvas是否开启抗锯齿。<br>默认值:false。 | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci### LengthMetricsUnit<sup>12+</sup> 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci用来配置CanvasRenderingContext2D对象的单位模式,默认单位模式为LengthMetricsUnit.DEFAULT,对应默认单位vp,配置后无法动态更改,详细说明见[LengthMetricsUnit](../js-apis-arkui-graphics.md#lengthmetricsunit12)。 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**示例:** 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci```ts 54e41f4b71Sopenharmony_ci// xxx.ets 55e41f4b71Sopenharmony_ciimport { LengthMetricsUnit } from '@kit.ArkUI' 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci@Entry 58e41f4b71Sopenharmony_ci@Component 59e41f4b71Sopenharmony_cistruct LengthMetricsUnitDemo { 60e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 61e41f4b71Sopenharmony_ci private contextPX: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings, LengthMetricsUnit.PX); 62e41f4b71Sopenharmony_ci private contextVP: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci build() { 65e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 66e41f4b71Sopenharmony_ci Canvas(this.contextPX) 67e41f4b71Sopenharmony_ci .width('100%') 68e41f4b71Sopenharmony_ci .height(150) 69e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 70e41f4b71Sopenharmony_ci .onReady(() => { 71e41f4b71Sopenharmony_ci this.contextPX.fillRect(10,10,100,100) 72e41f4b71Sopenharmony_ci this.contextPX.clearRect(10,10,50,50) 73e41f4b71Sopenharmony_ci }) 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci Canvas(this.contextVP) 76e41f4b71Sopenharmony_ci .width('100%') 77e41f4b71Sopenharmony_ci .height(150) 78e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 79e41f4b71Sopenharmony_ci .onReady(() => { 80e41f4b71Sopenharmony_ci this.contextVP.fillRect(10,10,100,100) 81e41f4b71Sopenharmony_ci this.contextVP.clearRect(10,10,50,50) 82e41f4b71Sopenharmony_ci }) 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci .width('100%') 85e41f4b71Sopenharmony_ci .height('100%') 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci} 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci## 属性 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 101e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ------------------ | ---------------------- | ---------------------------------------- | 102e41f4b71Sopenharmony_ci| [fillStyle](#fillstyle) | string \|number<sup>10+</sup> \|[CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | 否 | 否 | 指定绘制的填充色。<br/>- 类型为string时,表示设置填充区域的颜色。<br/>默认值:'black'<br/>- 类型为number时,表示设置填充区域的颜色。<br/>默认值:'#000000'<br/>- 类型为CanvasGradient时,表示渐变对象,使用[createLinearGradient](#createlineargradient)方法创建。<br/>- 类型为CanvasPattern时,使用[createPattern](#createpattern)方法创建。 | 103e41f4b71Sopenharmony_ci| [lineWidth](#linewidth) | number | 否 | 否 | 设置绘制线条的宽度。<br/>默认值:1(px)<br/>默认单位:vp <br/> linewidth取值不支持0和负数,0和负数按异常值处理,异常值按默认值处理。 | 104e41f4b71Sopenharmony_ci| [strokeStyle](#strokestyle) | string \|number<sup>10+</sup> \|[CanvasGradient](ts-components-canvas-canvasgradient.md) \| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) | 否 | 否 | 设置线条的颜色。<br/>- 类型为string时,表示设置线条使用的颜色。<br/>默认值:'black'<br/>- 类型为number时,表示设置线条使用的颜色。<br/>默认值:'#000000'<br/>- 类型为CanvasGradient时,表示渐变对象,使用[createLinearGradient](#createlineargradient)方法创建。<br/>- 类型为CanvasPattern时,使用[createPattern](#createpattern)方法创建。 | 105e41f4b71Sopenharmony_ci| [lineCap](#linecap) | [CanvasLineCap](#canvaslinecap) | 否 | 否 | 指定线端点的样式,可选值为:<br/>- 'butt':线端点以方形结束。<br/>- 'round':线端点以圆形结束。<br/>- 'square':线端点以方形结束,该样式下会增加一个长度和线段厚度相同,宽度是线段厚度一半的矩形。<br/>默认值:'butt'。 | 106e41f4b71Sopenharmony_ci| [lineJoin](#linejoin) | [CanvasLineJoin](#canvaslinejoin) | 否 | 否 | 指定线段间相交的交点样式,可选值为:<br/>- 'round':在线段相连处绘制一个扇形,扇形的圆角半径是线段的宽度。<br/>- 'bevel':在线段相连处使用三角形为底填充, 每个部分矩形拐角独立。<br/>- 'miter':在相连部分的外边缘处进行延伸,使其相交于一点,形成一个菱形区域,该属性可以通过设置miterLimit属性展现效果。<br/>默认值:'miter'。 | 107e41f4b71Sopenharmony_ci| [miterLimit](#miterlimit) | number | 否 | 否 | 设置斜接面限制值,该值指定了线条相交处内角和外角的距离。 <br/>默认值:10px<br/>单位:px<br/>miterLimit取值不支持0和负数,0和负数按异常值处理,异常值按默认值处理。 | 108e41f4b71Sopenharmony_ci| [font](#font) | string | 否 | 否 | 设置文本绘制中的字体样式。<br/>语法:ctx.font='font-size font-family'<br/>- font-size(可选),指定字号和行高,单位支持px和vp。在不同设备上呈现的字体大小可能不同。<br/>- font-family(可选),指定字体系列。<br/>语法:ctx.font='font-style font-weight font-size font-family'<br/>- font-style(可选),用于指定字体样式,支持如下几种样式:'normal','italic'。<br/>- font-weight(可选),用于指定字体的粗细,支持如下几种类型:'normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900。<br/>- font-size(可选),指定字号和行高,单位支持px、vp。使用时需要添加单位。<br/>- font-family(可选),指定字体系列,支持如下几种类型:'sans-serif', 'serif', 'monospace'。<br/>默认值:'normal normal 14px sans-serif'。| 109e41f4b71Sopenharmony_ci| [textAlign](#textalign) | [CanvasTextAlign](#canvastextalign) | 否 | 否 | 设置文本绘制中的文本对齐方式,可选值为:<br/>- 'left':文本左对齐。<br/>- 'right':文本右对齐。<br/>- 'center':文本居中对齐。<br/>- 'start':文本对齐界线开始的地方。<br/>- 'end':文本对齐界线结束的地方。<br/>ltr布局模式下'start'和'left'一致,rtl布局模式下'start'和'right'一致·。<br/>默认值:'start'。 | 110e41f4b71Sopenharmony_ci| [textBaseline](#textbaseline) | [CanvasTextBaseline](#canvastextbaseline) | 否 | 否 | 设置文本绘制中的水平对齐方式,可选值为:<br/>- 'alphabetic':文本基线是标准的字母基线。<br/>- 'top':文本基线在文本块的顶部。<br/>- 'hanging':文本基线是悬挂基线。<br/>- 'middle':文本基线在文本块的中间。<br/>- 'ideographic':文字基线是表意字基线;如果字符本身超出了alphabetic基线,那么ideograhpic基线位置在字符本身的底部。<br/>- 'bottom':文本基线在文本块的底部。 与ideographic基线的区别在于ideographic基线不需要考虑下行字母。<br/>默认值:'alphabetic'。 | 111e41f4b71Sopenharmony_ci| [globalAlpha](#globalalpha) | number | 否 | 否 | 设置透明度,0.0为完全透明,1.0为完全不透明。<br/>默认值:1.0。 | 112e41f4b71Sopenharmony_ci| [lineDashOffset](#linedashoffset) | number | 否 | 否 | 设置画布的虚线偏移量,精度为float。 <br/>默认值:0.0<br/>默认单位:vp。 | 113e41f4b71Sopenharmony_ci| [globalCompositeOperation](#globalcompositeoperation) | string | 否 | 否 | 设置合成操作的方式。类型字段可选值有'source-over','source-atop','source-in','source-out','destination-over','destination-atop','destination-in','destination-out','lighter','copy','xor'。<br/>默认值:'source-over'。 | 114e41f4b71Sopenharmony_ci| [shadowBlur](#shadowblur) | number | 否 | 否 | 设置绘制阴影时的模糊级别,值越大越模糊,精度为float。 <br/>默认值:0.0<br/>单位:px。<br/>shadowBlur取值不支持负数,负数按异常值处理,异常值按默认值处理。 | 115e41f4b71Sopenharmony_ci| [shadowColor](#shadowcolor) | string | 否 | 否 | 设置绘制阴影时的阴影颜色。<br/>默认值:透明黑色。 | 116e41f4b71Sopenharmony_ci| [shadowOffsetX](#shadowoffsetx) | number | 否 | 否 | 设置绘制阴影时和原有对象的水平偏移值。<br/>默认值:0.0<br/>默认单位:vp。 | 117e41f4b71Sopenharmony_ci| [shadowOffsetY](#shadowoffsety) | number | 否 | 否 | 设置绘制阴影时和原有对象的垂直偏移值。<br/>默认值:0.0<br/>默认单位:vp。 | 118e41f4b71Sopenharmony_ci| [imageSmoothingEnabled](#imagesmoothingenabled) | boolean | 否 | 否 | 用于设置绘制图片时是否进行图像平滑度调整,true为启用,false为不启用。 <br/>默认值:true。 | 119e41f4b71Sopenharmony_ci| [height](#height) | number | 是 | 否 | 组件高度。 <br/>默认单位:vp。 | 120e41f4b71Sopenharmony_ci| [width](#width) | number | 是 | 否 | 组件宽度。 <br/>默认单位:vp。 | 121e41f4b71Sopenharmony_ci| [imageSmoothingQuality](#imagesmoothingquality) | [ImageSmoothingQuality](#imagesmoothingquality-1) | 否 | 否 | imageSmoothingEnabled为true时,用于设置图像平滑度。<br/>默认值:"low"。 | 122e41f4b71Sopenharmony_ci| [direction](#direction) | [CanvasDirection](#canvasdirection) | 否 | 否 | 用于设置绘制文字时使用的文字方向。<br/>默认值:"inherit"。 | 123e41f4b71Sopenharmony_ci| [filter](#filter) | string | 否 | 否 | 用于设置图像的滤镜,可以组合任意数量的滤镜。<br/>支持的滤镜效果如下:<br/>- 'none': 无滤镜效果<br/>- 'blur':给图像设置高斯模糊<br/>- 'brightness':给图片应用一种线性乘法,使其看起来更亮或更暗<br/>- 'contrast':调整图像的对比度<br/>- 'grayscale':将图像转换为灰度图像<br/>- 'hue-rotate':给图像应用色相旋转<br/>- 'invert':反转输入图像<br/>- 'opacity':转化图像的透明程度<br/>- 'saturate':转换图像饱和度<br/>- 'sepia':将图像转换为深褐色<br/>默认值:'none'。 | 124e41f4b71Sopenharmony_ci| [canvas<sup>13+</sup>](#canvas13) | [FrameNode](../../apis-arkui/js-apis-arkui-frameNode.md) | 是 | 否 | 获取和CanvasRenderingContext2D关联的Canvas组件的FrameNode实例。<br/>可用于监听关联的Canvas组件的可见状态。<br/>默认值:null。 | 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci> **说明:** 127e41f4b71Sopenharmony_ci> 128e41f4b71Sopenharmony_ci> fillStyle、shadowColor与 strokeStyle 中string类型格式为 'rgb(255, 255, 255)','rgba(255, 255, 255, 1.0)','\#FFFFFF'。 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci### fillStyle 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci```ts 134e41f4b71Sopenharmony_ci// xxx.ets 135e41f4b71Sopenharmony_ci@Entry 136e41f4b71Sopenharmony_ci@Component 137e41f4b71Sopenharmony_cistruct FillStyleExample { 138e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 139e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci build() { 142e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 143e41f4b71Sopenharmony_ci Canvas(this.context) 144e41f4b71Sopenharmony_ci .width('100%') 145e41f4b71Sopenharmony_ci .height('100%') 146e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 147e41f4b71Sopenharmony_ci .onReady(() =>{ 148e41f4b71Sopenharmony_ci this.context.fillStyle = '#0000ff' 149e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 150, 100) 150e41f4b71Sopenharmony_ci }) 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci .width('100%') 153e41f4b71Sopenharmony_ci .height('100%') 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci} 156e41f4b71Sopenharmony_ci``` 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci### lineWidth 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci```ts 164e41f4b71Sopenharmony_ci// xxx.ets 165e41f4b71Sopenharmony_ci@Entry 166e41f4b71Sopenharmony_ci@Component 167e41f4b71Sopenharmony_cistruct LineWidthExample { 168e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 169e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci build() { 172e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 173e41f4b71Sopenharmony_ci Canvas(this.context) 174e41f4b71Sopenharmony_ci .width('100%') 175e41f4b71Sopenharmony_ci .height('100%') 176e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 177e41f4b71Sopenharmony_ci .onReady(() =>{ 178e41f4b71Sopenharmony_ci this.context.lineWidth = 5 179e41f4b71Sopenharmony_ci this.context.strokeRect(25, 25, 85, 105) 180e41f4b71Sopenharmony_ci }) 181e41f4b71Sopenharmony_ci } 182e41f4b71Sopenharmony_ci .width('100%') 183e41f4b71Sopenharmony_ci .height('100%') 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci} 186e41f4b71Sopenharmony_ci``` 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci### strokeStyle 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci```ts 194e41f4b71Sopenharmony_ci// xxx.ets 195e41f4b71Sopenharmony_ci@Entry 196e41f4b71Sopenharmony_ci@Component 197e41f4b71Sopenharmony_cistruct StrokeStyleExample { 198e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 199e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci build() { 202e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 203e41f4b71Sopenharmony_ci Canvas(this.context) 204e41f4b71Sopenharmony_ci .width('100%') 205e41f4b71Sopenharmony_ci .height('100%') 206e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 207e41f4b71Sopenharmony_ci .onReady(() =>{ 208e41f4b71Sopenharmony_ci this.context.lineWidth = 10 209e41f4b71Sopenharmony_ci this.context.strokeStyle = '#0000ff' 210e41f4b71Sopenharmony_ci this.context.strokeRect(25, 25, 155, 105) 211e41f4b71Sopenharmony_ci }) 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci .width('100%') 214e41f4b71Sopenharmony_ci .height('100%') 215e41f4b71Sopenharmony_ci } 216e41f4b71Sopenharmony_ci} 217e41f4b71Sopenharmony_ci``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci### lineCap 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci```ts 226e41f4b71Sopenharmony_ci// xxx.ets 227e41f4b71Sopenharmony_ci@Entry 228e41f4b71Sopenharmony_ci@Component 229e41f4b71Sopenharmony_cistruct LineCapExample { 230e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 231e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci build() { 234e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 235e41f4b71Sopenharmony_ci Canvas(this.context) 236e41f4b71Sopenharmony_ci .width('100%') 237e41f4b71Sopenharmony_ci .height('100%') 238e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 239e41f4b71Sopenharmony_ci .onReady(() =>{ 240e41f4b71Sopenharmony_ci this.context.lineWidth = 8 241e41f4b71Sopenharmony_ci this.context.beginPath() 242e41f4b71Sopenharmony_ci this.context.lineCap = 'round' 243e41f4b71Sopenharmony_ci this.context.moveTo(30, 50) 244e41f4b71Sopenharmony_ci this.context.lineTo(220, 50) 245e41f4b71Sopenharmony_ci this.context.stroke() 246e41f4b71Sopenharmony_ci }) 247e41f4b71Sopenharmony_ci } 248e41f4b71Sopenharmony_ci .width('100%') 249e41f4b71Sopenharmony_ci .height('100%') 250e41f4b71Sopenharmony_ci } 251e41f4b71Sopenharmony_ci} 252e41f4b71Sopenharmony_ci``` 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci### lineJoin 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci```ts 260e41f4b71Sopenharmony_ci// xxx.ets 261e41f4b71Sopenharmony_ci@Entry 262e41f4b71Sopenharmony_ci@Component 263e41f4b71Sopenharmony_cistruct LineJoinExample { 264e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 265e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci build() { 268e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 269e41f4b71Sopenharmony_ci Canvas(this.context) 270e41f4b71Sopenharmony_ci .width('100%') 271e41f4b71Sopenharmony_ci .height('100%') 272e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 273e41f4b71Sopenharmony_ci .onReady(() =>{ 274e41f4b71Sopenharmony_ci this.context.beginPath() 275e41f4b71Sopenharmony_ci this.context.lineWidth = 8 276e41f4b71Sopenharmony_ci this.context.lineJoin = 'miter' 277e41f4b71Sopenharmony_ci this.context.moveTo(30, 30) 278e41f4b71Sopenharmony_ci this.context.lineTo(120, 60) 279e41f4b71Sopenharmony_ci this.context.lineTo(30, 110) 280e41f4b71Sopenharmony_ci this.context.stroke() 281e41f4b71Sopenharmony_ci }) 282e41f4b71Sopenharmony_ci } 283e41f4b71Sopenharmony_ci .width('100%') 284e41f4b71Sopenharmony_ci .height('100%') 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci} 287e41f4b71Sopenharmony_ci``` 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci### miterLimit 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci```ts 295e41f4b71Sopenharmony_ci// xxx.ets 296e41f4b71Sopenharmony_ci@Entry 297e41f4b71Sopenharmony_ci@Component 298e41f4b71Sopenharmony_cistruct MiterLimit { 299e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 300e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci build() { 303e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 304e41f4b71Sopenharmony_ci Canvas(this.context) 305e41f4b71Sopenharmony_ci .width('100%') 306e41f4b71Sopenharmony_ci .height('100%') 307e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 308e41f4b71Sopenharmony_ci .onReady(() =>{ 309e41f4b71Sopenharmony_ci this.context.lineWidth = 8 310e41f4b71Sopenharmony_ci this.context.lineJoin = 'miter' 311e41f4b71Sopenharmony_ci this.context.miterLimit = 3 312e41f4b71Sopenharmony_ci this.context.moveTo(30, 30) 313e41f4b71Sopenharmony_ci this.context.lineTo(60, 35) 314e41f4b71Sopenharmony_ci this.context.lineTo(30, 37) 315e41f4b71Sopenharmony_ci this.context.stroke() 316e41f4b71Sopenharmony_ci }) 317e41f4b71Sopenharmony_ci } 318e41f4b71Sopenharmony_ci .width('100%') 319e41f4b71Sopenharmony_ci .height('100%') 320e41f4b71Sopenharmony_ci } 321e41f4b71Sopenharmony_ci} 322e41f4b71Sopenharmony_ci``` 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci### font 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci```ts 330e41f4b71Sopenharmony_ci// xxx.ets 331e41f4b71Sopenharmony_ci@Entry 332e41f4b71Sopenharmony_ci@Component 333e41f4b71Sopenharmony_cistruct Fonts { 334e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 335e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci build() { 338e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 339e41f4b71Sopenharmony_ci Canvas(this.context) 340e41f4b71Sopenharmony_ci .width('100%') 341e41f4b71Sopenharmony_ci .height('100%') 342e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 343e41f4b71Sopenharmony_ci .onReady(() =>{ 344e41f4b71Sopenharmony_ci this.context.font = '30px sans-serif' 345e41f4b71Sopenharmony_ci this.context.fillText("Hello px", 20, 60) 346e41f4b71Sopenharmony_ci this.context.font = '30vp sans-serif' 347e41f4b71Sopenharmony_ci this.context.fillText("Hello vp", 20, 100) 348e41f4b71Sopenharmony_ci }) 349e41f4b71Sopenharmony_ci } 350e41f4b71Sopenharmony_ci .width('100%') 351e41f4b71Sopenharmony_ci .height('100%') 352e41f4b71Sopenharmony_ci } 353e41f4b71Sopenharmony_ci} 354e41f4b71Sopenharmony_ci``` 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci### textAlign 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci```ts 362e41f4b71Sopenharmony_ci// xxx.ets 363e41f4b71Sopenharmony_ci@Entry 364e41f4b71Sopenharmony_ci@Component 365e41f4b71Sopenharmony_cistruct CanvasExample { 366e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 367e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci build() { 370e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 371e41f4b71Sopenharmony_ci Canvas(this.context) 372e41f4b71Sopenharmony_ci .width('100%') 373e41f4b71Sopenharmony_ci .height('100%') 374e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 375e41f4b71Sopenharmony_ci .onReady(() => { 376e41f4b71Sopenharmony_ci this.context.strokeStyle = '#0000ff' 377e41f4b71Sopenharmony_ci this.context.moveTo(140, 10) 378e41f4b71Sopenharmony_ci this.context.lineTo(140, 160) 379e41f4b71Sopenharmony_ci this.context.stroke() 380e41f4b71Sopenharmony_ci this.context.font = '18px sans-serif' 381e41f4b71Sopenharmony_ci this.context.textAlign = 'start' 382e41f4b71Sopenharmony_ci this.context.fillText('textAlign=start', 140, 60) 383e41f4b71Sopenharmony_ci this.context.textAlign = 'end' 384e41f4b71Sopenharmony_ci this.context.fillText('textAlign=end', 140, 80) 385e41f4b71Sopenharmony_ci this.context.textAlign = 'left' 386e41f4b71Sopenharmony_ci this.context.fillText('textAlign=left', 140, 100) 387e41f4b71Sopenharmony_ci this.context.textAlign = 'center' 388e41f4b71Sopenharmony_ci this.context.fillText('textAlign=center', 140, 120) 389e41f4b71Sopenharmony_ci this.context.textAlign = 'right' 390e41f4b71Sopenharmony_ci this.context.fillText('textAlign=right', 140, 140) 391e41f4b71Sopenharmony_ci }) 392e41f4b71Sopenharmony_ci } 393e41f4b71Sopenharmony_ci .width('100%') 394e41f4b71Sopenharmony_ci .height('100%') 395e41f4b71Sopenharmony_ci } 396e41f4b71Sopenharmony_ci} 397e41f4b71Sopenharmony_ci``` 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci### textBaseline 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci```ts 405e41f4b71Sopenharmony_ci// xxx.ets 406e41f4b71Sopenharmony_ci@Entry 407e41f4b71Sopenharmony_ci@Component 408e41f4b71Sopenharmony_cistruct TextBaseline { 409e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 410e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci build() { 413e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 414e41f4b71Sopenharmony_ci Canvas(this.context) 415e41f4b71Sopenharmony_ci .width('100%') 416e41f4b71Sopenharmony_ci .height('100%') 417e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 418e41f4b71Sopenharmony_ci .onReady(() =>{ 419e41f4b71Sopenharmony_ci this.context.strokeStyle = '#0000ff' 420e41f4b71Sopenharmony_ci this.context.moveTo(0, 120) 421e41f4b71Sopenharmony_ci this.context.lineTo(400, 120) 422e41f4b71Sopenharmony_ci this.context.stroke() 423e41f4b71Sopenharmony_ci this.context.font = '20px sans-serif' 424e41f4b71Sopenharmony_ci this.context.textBaseline = 'top' 425e41f4b71Sopenharmony_ci this.context.fillText('Top', 10, 120) 426e41f4b71Sopenharmony_ci this.context.textBaseline = 'bottom' 427e41f4b71Sopenharmony_ci this.context.fillText('Bottom', 55, 120) 428e41f4b71Sopenharmony_ci this.context.textBaseline = 'middle' 429e41f4b71Sopenharmony_ci this.context.fillText('Middle', 125, 120) 430e41f4b71Sopenharmony_ci this.context.textBaseline = 'alphabetic' 431e41f4b71Sopenharmony_ci this.context.fillText('Alphabetic', 195, 120) 432e41f4b71Sopenharmony_ci this.context.textBaseline = 'hanging' 433e41f4b71Sopenharmony_ci this.context.fillText('Hanging', 295, 120) 434e41f4b71Sopenharmony_ci }) 435e41f4b71Sopenharmony_ci } 436e41f4b71Sopenharmony_ci .width('100%') 437e41f4b71Sopenharmony_ci .height('100%') 438e41f4b71Sopenharmony_ci } 439e41f4b71Sopenharmony_ci} 440e41f4b71Sopenharmony_ci``` 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci### globalAlpha 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci```ts 448e41f4b71Sopenharmony_ci// xxx.ets 449e41f4b71Sopenharmony_ci@Entry 450e41f4b71Sopenharmony_ci@Component 451e41f4b71Sopenharmony_cistruct GlobalAlpha { 452e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 453e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci build() { 456e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 457e41f4b71Sopenharmony_ci Canvas(this.context) 458e41f4b71Sopenharmony_ci .width('100%') 459e41f4b71Sopenharmony_ci .height('100%') 460e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 461e41f4b71Sopenharmony_ci .onReady(() =>{ 462e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 463e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 50, 50) 464e41f4b71Sopenharmony_ci this.context.globalAlpha = 0.4 465e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 466e41f4b71Sopenharmony_ci this.context.fillRect(50, 50, 50, 50) 467e41f4b71Sopenharmony_ci }) 468e41f4b71Sopenharmony_ci } 469e41f4b71Sopenharmony_ci .width('100%') 470e41f4b71Sopenharmony_ci .height('100%') 471e41f4b71Sopenharmony_ci } 472e41f4b71Sopenharmony_ci} 473e41f4b71Sopenharmony_ci``` 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci### lineDashOffset 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci```ts 481e41f4b71Sopenharmony_ci// xxx.ets 482e41f4b71Sopenharmony_ci@Entry 483e41f4b71Sopenharmony_ci@Component 484e41f4b71Sopenharmony_cistruct LineDashOffset { 485e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 486e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci build() { 489e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 490e41f4b71Sopenharmony_ci Canvas(this.context) 491e41f4b71Sopenharmony_ci .width('100%') 492e41f4b71Sopenharmony_ci .height('100%') 493e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 494e41f4b71Sopenharmony_ci .onReady(() =>{ 495e41f4b71Sopenharmony_ci this.context.arc(100, 75, 50, 0, 6.28) 496e41f4b71Sopenharmony_ci this.context.setLineDash([10,20]) 497e41f4b71Sopenharmony_ci this.context.lineDashOffset = 10.0 498e41f4b71Sopenharmony_ci this.context.stroke() 499e41f4b71Sopenharmony_ci }) 500e41f4b71Sopenharmony_ci } 501e41f4b71Sopenharmony_ci .width('100%') 502e41f4b71Sopenharmony_ci .height('100%') 503e41f4b71Sopenharmony_ci } 504e41f4b71Sopenharmony_ci} 505e41f4b71Sopenharmony_ci``` 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci### globalCompositeOperation 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| 名称 | 描述 | 513e41f4b71Sopenharmony_ci| ---------------- | ------------------------ | 514e41f4b71Sopenharmony_ci| source-over | 在现有绘制内容上显示新绘制内容,属于默认值。 | 515e41f4b71Sopenharmony_ci| source-atop | 在现有绘制内容顶部显示新绘制内容。 | 516e41f4b71Sopenharmony_ci| source-in | 在现有绘制内容中显示新绘制内容。 | 517e41f4b71Sopenharmony_ci| source-out | 在现有绘制内容之外显示新绘制内容。 | 518e41f4b71Sopenharmony_ci| destination-over | 在新绘制内容上方显示现有绘制内容。 | 519e41f4b71Sopenharmony_ci| destination-atop | 在新绘制内容顶部显示现有绘制内容。 | 520e41f4b71Sopenharmony_ci| destination-in | 在新绘制内容中显示现有绘制内容。 | 521e41f4b71Sopenharmony_ci| destination-out | 在新绘制内容外显示现有绘制内容。 | 522e41f4b71Sopenharmony_ci| lighter | 显示新绘制内容和现有绘制内容。 | 523e41f4b71Sopenharmony_ci| copy | 显示新绘制内容而忽略现有绘制内容。 | 524e41f4b71Sopenharmony_ci| xor | 使用异或操作对新绘制内容与现有绘制内容进行融合。 | 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci```ts 527e41f4b71Sopenharmony_ci// xxx.ets 528e41f4b71Sopenharmony_ci@Entry 529e41f4b71Sopenharmony_ci@Component 530e41f4b71Sopenharmony_cistruct GlobalCompositeOperation { 531e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 532e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci build() { 535e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 536e41f4b71Sopenharmony_ci Canvas(this.context) 537e41f4b71Sopenharmony_ci .width('100%') 538e41f4b71Sopenharmony_ci .height('100%') 539e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 540e41f4b71Sopenharmony_ci .onReady(() =>{ 541e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 542e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 50, 50) 543e41f4b71Sopenharmony_ci this.context.globalCompositeOperation = 'source-over' 544e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 545e41f4b71Sopenharmony_ci this.context.fillRect(50, 50, 50, 50) 546e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 547e41f4b71Sopenharmony_ci this.context.fillRect(120, 20, 50, 50) 548e41f4b71Sopenharmony_ci this.context.globalCompositeOperation = 'destination-over' 549e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 550e41f4b71Sopenharmony_ci this.context.fillRect(150, 50, 50, 50) 551e41f4b71Sopenharmony_ci }) 552e41f4b71Sopenharmony_ci } 553e41f4b71Sopenharmony_ci .width('100%') 554e41f4b71Sopenharmony_ci .height('100%') 555e41f4b71Sopenharmony_ci } 556e41f4b71Sopenharmony_ci} 557e41f4b71Sopenharmony_ci``` 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci### shadowBlur 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci```ts 565e41f4b71Sopenharmony_ci// xxx.ets 566e41f4b71Sopenharmony_ci@Entry 567e41f4b71Sopenharmony_ci@Component 568e41f4b71Sopenharmony_cistruct ShadowBlur { 569e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 570e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci build() { 573e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 574e41f4b71Sopenharmony_ci Canvas(this.context) 575e41f4b71Sopenharmony_ci .width('100%') 576e41f4b71Sopenharmony_ci .height('100%') 577e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 578e41f4b71Sopenharmony_ci .onReady(() =>{ 579e41f4b71Sopenharmony_ci this.context.shadowBlur = 30 580e41f4b71Sopenharmony_ci this.context.shadowColor = 'rgb(0,0,0)' 581e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 582e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 100, 80) 583e41f4b71Sopenharmony_ci }) 584e41f4b71Sopenharmony_ci } 585e41f4b71Sopenharmony_ci .width('100%') 586e41f4b71Sopenharmony_ci .height('100%') 587e41f4b71Sopenharmony_ci } 588e41f4b71Sopenharmony_ci} 589e41f4b71Sopenharmony_ci``` 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ci### shadowColor 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci```ts 597e41f4b71Sopenharmony_ci// xxx.ets 598e41f4b71Sopenharmony_ci@Entry 599e41f4b71Sopenharmony_ci@Component 600e41f4b71Sopenharmony_cistruct ShadowColor { 601e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 602e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci build() { 605e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 606e41f4b71Sopenharmony_ci Canvas(this.context) 607e41f4b71Sopenharmony_ci .width('100%') 608e41f4b71Sopenharmony_ci .height('100%') 609e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 610e41f4b71Sopenharmony_ci .onReady(() =>{ 611e41f4b71Sopenharmony_ci this.context.shadowBlur = 30 612e41f4b71Sopenharmony_ci this.context.shadowColor = 'rgb(0,0,255)' 613e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 614e41f4b71Sopenharmony_ci this.context.fillRect(30, 30, 100, 100) 615e41f4b71Sopenharmony_ci }) 616e41f4b71Sopenharmony_ci } 617e41f4b71Sopenharmony_ci .width('100%') 618e41f4b71Sopenharmony_ci .height('100%') 619e41f4b71Sopenharmony_ci } 620e41f4b71Sopenharmony_ci} 621e41f4b71Sopenharmony_ci``` 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci 626e41f4b71Sopenharmony_ci### shadowOffsetX 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci```ts 629e41f4b71Sopenharmony_ci// xxx.ets 630e41f4b71Sopenharmony_ci@Entry 631e41f4b71Sopenharmony_ci@Component 632e41f4b71Sopenharmony_cistruct ShadowOffsetX { 633e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 634e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ci build() { 637e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 638e41f4b71Sopenharmony_ci Canvas(this.context) 639e41f4b71Sopenharmony_ci .width('100%') 640e41f4b71Sopenharmony_ci .height('100%') 641e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 642e41f4b71Sopenharmony_ci .onReady(() =>{ 643e41f4b71Sopenharmony_ci this.context.shadowBlur = 10 644e41f4b71Sopenharmony_ci this.context.shadowOffsetX = 20 645e41f4b71Sopenharmony_ci this.context.shadowColor = 'rgb(0,0,0)' 646e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 647e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 100, 80) 648e41f4b71Sopenharmony_ci }) 649e41f4b71Sopenharmony_ci } 650e41f4b71Sopenharmony_ci .width('100%') 651e41f4b71Sopenharmony_ci .height('100%') 652e41f4b71Sopenharmony_ci } 653e41f4b71Sopenharmony_ci} 654e41f4b71Sopenharmony_ci``` 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci### shadowOffsetY 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci```ts 662e41f4b71Sopenharmony_ci// xxx.ets 663e41f4b71Sopenharmony_ci@Entry 664e41f4b71Sopenharmony_ci@Component 665e41f4b71Sopenharmony_cistruct ShadowOffsetY { 666e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 667e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 668e41f4b71Sopenharmony_ci build() { 669e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 670e41f4b71Sopenharmony_ci Canvas(this.context) 671e41f4b71Sopenharmony_ci .width('100%') 672e41f4b71Sopenharmony_ci .height('100%') 673e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 674e41f4b71Sopenharmony_ci .onReady(() =>{ 675e41f4b71Sopenharmony_ci this.context.shadowBlur = 10 676e41f4b71Sopenharmony_ci this.context.shadowOffsetY = 20 677e41f4b71Sopenharmony_ci this.context.shadowColor = 'rgb(0,0,0)' 678e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 679e41f4b71Sopenharmony_ci this.context.fillRect(30, 30, 100, 100) 680e41f4b71Sopenharmony_ci }) 681e41f4b71Sopenharmony_ci } 682e41f4b71Sopenharmony_ci .width('100%') 683e41f4b71Sopenharmony_ci .height('100%') 684e41f4b71Sopenharmony_ci } 685e41f4b71Sopenharmony_ci} 686e41f4b71Sopenharmony_ci``` 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci### imageSmoothingEnabled 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci```ts 694e41f4b71Sopenharmony_ci// xxx.ets 695e41f4b71Sopenharmony_ci@Entry 696e41f4b71Sopenharmony_ci@Component 697e41f4b71Sopenharmony_cistruct ImageSmoothingEnabled { 698e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 699e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 700e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg") 701e41f4b71Sopenharmony_ci 702e41f4b71Sopenharmony_ci build() { 703e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 704e41f4b71Sopenharmony_ci Canvas(this.context) 705e41f4b71Sopenharmony_ci .width('100%') 706e41f4b71Sopenharmony_ci .height('100%') 707e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 708e41f4b71Sopenharmony_ci .onReady(() =>{ 709e41f4b71Sopenharmony_ci this.context.imageSmoothingEnabled = false 710e41f4b71Sopenharmony_ci this.context.drawImage( this.img,0,0,400,200) 711e41f4b71Sopenharmony_ci }) 712e41f4b71Sopenharmony_ci } 713e41f4b71Sopenharmony_ci .width('100%') 714e41f4b71Sopenharmony_ci .height('100%') 715e41f4b71Sopenharmony_ci } 716e41f4b71Sopenharmony_ci} 717e41f4b71Sopenharmony_ci``` 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci### height 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci```ts 725e41f4b71Sopenharmony_ci// xxx.ets 726e41f4b71Sopenharmony_ci@Entry 727e41f4b71Sopenharmony_ci@Component 728e41f4b71Sopenharmony_cistruct HeightExample { 729e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 730e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci build() { 733e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 734e41f4b71Sopenharmony_ci Canvas(this.context) 735e41f4b71Sopenharmony_ci .width(300) 736e41f4b71Sopenharmony_ci .height(300) 737e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 738e41f4b71Sopenharmony_ci .onReady(() => { 739e41f4b71Sopenharmony_ci let h = this.context.height 740e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 300, h/2) 741e41f4b71Sopenharmony_ci }) 742e41f4b71Sopenharmony_ci } 743e41f4b71Sopenharmony_ci .width('100%') 744e41f4b71Sopenharmony_ci .height('100%') 745e41f4b71Sopenharmony_ci } 746e41f4b71Sopenharmony_ci} 747e41f4b71Sopenharmony_ci``` 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci### width 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci```ts 755e41f4b71Sopenharmony_ci// xxx.ets 756e41f4b71Sopenharmony_ci@Entry 757e41f4b71Sopenharmony_ci@Component 758e41f4b71Sopenharmony_cistruct WidthExample { 759e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 760e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ci build() { 763e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 764e41f4b71Sopenharmony_ci Canvas(this.context) 765e41f4b71Sopenharmony_ci .width(300) 766e41f4b71Sopenharmony_ci .height(300) 767e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 768e41f4b71Sopenharmony_ci .onReady(() => { 769e41f4b71Sopenharmony_ci let w = this.context.width 770e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, w/2, 300) 771e41f4b71Sopenharmony_ci }) 772e41f4b71Sopenharmony_ci } 773e41f4b71Sopenharmony_ci .width('100%') 774e41f4b71Sopenharmony_ci .height('100%') 775e41f4b71Sopenharmony_ci } 776e41f4b71Sopenharmony_ci} 777e41f4b71Sopenharmony_ci``` 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci 782e41f4b71Sopenharmony_ci### canvas<sup>13+</sup> 783e41f4b71Sopenharmony_ci 784e41f4b71Sopenharmony_ci```ts 785e41f4b71Sopenharmony_ciimport { FrameNode } from '@kit.ArkUI' 786e41f4b71Sopenharmony_ci// xxx.ets 787e41f4b71Sopenharmony_ci@Entry 788e41f4b71Sopenharmony_ci@Component 789e41f4b71Sopenharmony_cistruct CanvasExample { 790e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 791e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 792e41f4b71Sopenharmony_ci private text: string = '' 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci build() { 795e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 796e41f4b71Sopenharmony_ci Canvas(this.context) 797e41f4b71Sopenharmony_ci .width('100%') 798e41f4b71Sopenharmony_ci .height('100%') 799e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 800e41f4b71Sopenharmony_ci .onReady(() => { 801e41f4b71Sopenharmony_ci let node: FrameNode = this.context.canvas 802e41f4b71Sopenharmony_ci node?.commonEvent.setOnVisibleAreaApproximateChange( 803e41f4b71Sopenharmony_ci { ratios: [0, 1], expectedUpdateInterval: 10}, 804e41f4b71Sopenharmony_ci (isVisible: boolean, currentRatio: number) => { 805e41f4b71Sopenharmony_ci if (!isVisible && currentRatio <= 0.0) { 806e41f4b71Sopenharmony_ci this.text = 'Canvas is completely invisible.' 807e41f4b71Sopenharmony_ci } 808e41f4b71Sopenharmony_ci if (isVisible && currentRatio >= 1.0) { 809e41f4b71Sopenharmony_ci this.text = 'Canvas is fully visible.' 810e41f4b71Sopenharmony_ci } 811e41f4b71Sopenharmony_ci this.context.reset() 812e41f4b71Sopenharmony_ci this.context.font = '30vp sans-serif' 813e41f4b71Sopenharmony_ci this.context.fillText(this.text, 50, 50) 814e41f4b71Sopenharmony_ci } 815e41f4b71Sopenharmony_ci ) 816e41f4b71Sopenharmony_ci }) 817e41f4b71Sopenharmony_ci } 818e41f4b71Sopenharmony_ci .width('100%') 819e41f4b71Sopenharmony_ci .height('100%') 820e41f4b71Sopenharmony_ci } 821e41f4b71Sopenharmony_ci} 822e41f4b71Sopenharmony_ci``` 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci### imageSmoothingQuality 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci```ts 830e41f4b71Sopenharmony_ci // xxx.ets 831e41f4b71Sopenharmony_ci @Entry 832e41f4b71Sopenharmony_ci @Component 833e41f4b71Sopenharmony_ci struct ImageSmoothingQualityDemo { 834e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 835e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 836e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/example.jpg"); 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ci build() { 839e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 840e41f4b71Sopenharmony_ci Canvas(this.context) 841e41f4b71Sopenharmony_ci .width('100%') 842e41f4b71Sopenharmony_ci .height('100%') 843e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 844e41f4b71Sopenharmony_ci .onReady(() =>{ 845e41f4b71Sopenharmony_ci let ctx = this.context 846e41f4b71Sopenharmony_ci ctx.imageSmoothingEnabled = true 847e41f4b71Sopenharmony_ci ctx.imageSmoothingQuality = 'high' 848e41f4b71Sopenharmony_ci ctx.drawImage(this.img, 0, 0, 400, 200) 849e41f4b71Sopenharmony_ci }) 850e41f4b71Sopenharmony_ci } 851e41f4b71Sopenharmony_ci .width('100%') 852e41f4b71Sopenharmony_ci .height('100%') 853e41f4b71Sopenharmony_ci } 854e41f4b71Sopenharmony_ci } 855e41f4b71Sopenharmony_ci``` 856e41f4b71Sopenharmony_ci 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci 859e41f4b71Sopenharmony_ci 860e41f4b71Sopenharmony_ci### direction 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci```ts 863e41f4b71Sopenharmony_ci // xxx.ets 864e41f4b71Sopenharmony_ci @Entry 865e41f4b71Sopenharmony_ci @Component 866e41f4b71Sopenharmony_ci struct DirectionDemo { 867e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 868e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci build() { 871e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 872e41f4b71Sopenharmony_ci Canvas(this.context) 873e41f4b71Sopenharmony_ci .width('100%') 874e41f4b71Sopenharmony_ci .height('100%') 875e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 876e41f4b71Sopenharmony_ci .onReady(() =>{ 877e41f4b71Sopenharmony_ci let ctx = this.context 878e41f4b71Sopenharmony_ci ctx.font = '48px serif'; 879e41f4b71Sopenharmony_ci ctx.textAlign = 'start' 880e41f4b71Sopenharmony_ci ctx.fillText("Hi ltr!", 200, 50); 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci ctx.direction = "rtl"; 883e41f4b71Sopenharmony_ci ctx.fillText("Hi rtl!", 200, 100); 884e41f4b71Sopenharmony_ci }) 885e41f4b71Sopenharmony_ci } 886e41f4b71Sopenharmony_ci .width('100%') 887e41f4b71Sopenharmony_ci .height('100%') 888e41f4b71Sopenharmony_ci } 889e41f4b71Sopenharmony_ci } 890e41f4b71Sopenharmony_ci``` 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci### filter 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci```ts 898e41f4b71Sopenharmony_ci // xxx.ets 899e41f4b71Sopenharmony_ci @Entry 900e41f4b71Sopenharmony_ci @Component 901e41f4b71Sopenharmony_ci struct FilterDemo { 902e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 903e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 904e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/example.jpg"); 905e41f4b71Sopenharmony_ci 906e41f4b71Sopenharmony_ci build() { 907e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 908e41f4b71Sopenharmony_ci Canvas(this.context) 909e41f4b71Sopenharmony_ci .width('100%') 910e41f4b71Sopenharmony_ci .height('100%') 911e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 912e41f4b71Sopenharmony_ci .onReady(() =>{ 913e41f4b71Sopenharmony_ci let ctx = this.context 914e41f4b71Sopenharmony_ci let img = this.img 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 0, 100, 100); 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci ctx.filter = 'grayscale(50%)'; 919e41f4b71Sopenharmony_ci ctx.drawImage(img, 100, 0, 100, 100); 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci ctx.filter = 'sepia(60%)'; 922e41f4b71Sopenharmony_ci ctx.drawImage(img, 200, 0, 100, 100); 923e41f4b71Sopenharmony_ci 924e41f4b71Sopenharmony_ci ctx.filter = 'saturate(30%)'; 925e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 100, 100, 100); 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ci ctx.filter = 'hue-rotate(90deg)'; 928e41f4b71Sopenharmony_ci ctx.drawImage(img, 100, 100, 100, 100); 929e41f4b71Sopenharmony_ci 930e41f4b71Sopenharmony_ci ctx.filter = 'invert(100%)'; 931e41f4b71Sopenharmony_ci ctx.drawImage(img, 200, 100, 100, 100); 932e41f4b71Sopenharmony_ci 933e41f4b71Sopenharmony_ci ctx.filter = 'opacity(25%)'; 934e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 200, 100, 100); 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci ctx.filter = 'brightness(0.4)'; 937e41f4b71Sopenharmony_ci ctx.drawImage(img, 100, 200, 100, 100); 938e41f4b71Sopenharmony_ci 939e41f4b71Sopenharmony_ci ctx.filter = 'contrast(200%)'; 940e41f4b71Sopenharmony_ci ctx.drawImage(img, 200, 200, 100, 100); 941e41f4b71Sopenharmony_ci 942e41f4b71Sopenharmony_ci ctx.filter = 'blur(5px)'; 943e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 300, 100, 100); 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ci let result = ctx.toDataURL() 946e41f4b71Sopenharmony_ci console.info(result) 947e41f4b71Sopenharmony_ci }) 948e41f4b71Sopenharmony_ci } 949e41f4b71Sopenharmony_ci .width('100%') 950e41f4b71Sopenharmony_ci .height('100%') 951e41f4b71Sopenharmony_ci } 952e41f4b71Sopenharmony_ci } 953e41f4b71Sopenharmony_ci``` 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci## 方法 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci以下方法在隐藏页面中调用会产生缓存,应避免在隐藏页面中频繁刷新Canvas。 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci### fillRect 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_cifillRect(x: number, y: number, w: number, h: number): void 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci填充一个矩形。 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci**参数:** 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 976e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------- | 977e41f4b71Sopenharmony_ci| x | number | 是 | 指定矩形左上角点的x坐标。<br>默认单位:vp。 | 978e41f4b71Sopenharmony_ci| y | number | 是 | 指定矩形左上角点的y坐标。<br>默认单位:vp。 | 979e41f4b71Sopenharmony_ci| w | number | 是 | 指定矩形的宽度。<br>默认单位:vp。 | 980e41f4b71Sopenharmony_ci| h | number | 是 | 指定矩形的高度。<br>默认单位:vp。 | 981e41f4b71Sopenharmony_ci 982e41f4b71Sopenharmony_ci**示例:** 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci ```ts 985e41f4b71Sopenharmony_ci // xxx.ets 986e41f4b71Sopenharmony_ci @Entry 987e41f4b71Sopenharmony_ci @Component 988e41f4b71Sopenharmony_ci struct FillRect { 989e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 990e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 991e41f4b71Sopenharmony_ci 992e41f4b71Sopenharmony_ci build() { 993e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 994e41f4b71Sopenharmony_ci Canvas(this.context) 995e41f4b71Sopenharmony_ci .width('100%') 996e41f4b71Sopenharmony_ci .height('100%') 997e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 998e41f4b71Sopenharmony_ci .onReady(() =>{ 999e41f4b71Sopenharmony_ci this.context.fillRect(30,30,100,100) 1000e41f4b71Sopenharmony_ci }) 1001e41f4b71Sopenharmony_ci } 1002e41f4b71Sopenharmony_ci .width('100%') 1003e41f4b71Sopenharmony_ci .height('100%') 1004e41f4b71Sopenharmony_ci } 1005e41f4b71Sopenharmony_ci } 1006e41f4b71Sopenharmony_ci ``` 1007e41f4b71Sopenharmony_ci 1008e41f4b71Sopenharmony_ci  1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci### strokeRect 1012e41f4b71Sopenharmony_ci 1013e41f4b71Sopenharmony_cistrokeRect(x: number, y: number, w: number, h: number): void 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci绘制具有边框的矩形,矩形内部不填充。 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1018e41f4b71Sopenharmony_ci 1019e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci**参数:** 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1026e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 1027e41f4b71Sopenharmony_ci| x | number | 是 | 指定矩形的左上角x坐标。<br>默认单位:vp。 | 1028e41f4b71Sopenharmony_ci| y | number | 是 | 指定矩形的左上角y坐标。<br>默认单位:vp。 | 1029e41f4b71Sopenharmony_ci| w | number | 是 | 指定矩形的宽度。<br>默认单位:vp。| 1030e41f4b71Sopenharmony_ci| h | number | 是 | 指定矩形的高度。<br>默认单位:vp。| 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ci**示例:** 1033e41f4b71Sopenharmony_ci 1034e41f4b71Sopenharmony_ci ```ts 1035e41f4b71Sopenharmony_ci // xxx.ets 1036e41f4b71Sopenharmony_ci @Entry 1037e41f4b71Sopenharmony_ci @Component 1038e41f4b71Sopenharmony_ci struct StrokeRect { 1039e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1040e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_ci build() { 1043e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1044e41f4b71Sopenharmony_ci Canvas(this.context) 1045e41f4b71Sopenharmony_ci .width('100%') 1046e41f4b71Sopenharmony_ci .height('100%') 1047e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1048e41f4b71Sopenharmony_ci .onReady(() =>{ 1049e41f4b71Sopenharmony_ci this.context.strokeRect(30, 30, 200, 150) 1050e41f4b71Sopenharmony_ci }) 1051e41f4b71Sopenharmony_ci } 1052e41f4b71Sopenharmony_ci .width('100%') 1053e41f4b71Sopenharmony_ci .height('100%') 1054e41f4b71Sopenharmony_ci } 1055e41f4b71Sopenharmony_ci } 1056e41f4b71Sopenharmony_ci ``` 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci  1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci 1061e41f4b71Sopenharmony_ci### clearRect 1062e41f4b71Sopenharmony_ci 1063e41f4b71Sopenharmony_ciclearRect(x: number, y: number, w: number, h: number): void 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci删除指定区域内的绘制内容。 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1068e41f4b71Sopenharmony_ci 1069e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1070e41f4b71Sopenharmony_ci 1071e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci**参数:** 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1076e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------- | 1077e41f4b71Sopenharmony_ci| x | number | 是 | 指定矩形上的左上角x坐标。<br>默认单位:vp。 | 1078e41f4b71Sopenharmony_ci| y | number | 是 | 指定矩形上的左上角y坐标。<br>默认单位:vp。 | 1079e41f4b71Sopenharmony_ci| w | number | 是 | 指定矩形的宽度。<br>默认单位:vp。 | 1080e41f4b71Sopenharmony_ci| h | number | 是 | 指定矩形的高度。<br>默认单位:vp。 | 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ci**示例:** 1083e41f4b71Sopenharmony_ci 1084e41f4b71Sopenharmony_ci ```ts 1085e41f4b71Sopenharmony_ci // xxx.ets 1086e41f4b71Sopenharmony_ci @Entry 1087e41f4b71Sopenharmony_ci @Component 1088e41f4b71Sopenharmony_ci struct ClearRect { 1089e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1090e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1091e41f4b71Sopenharmony_ci 1092e41f4b71Sopenharmony_ci build() { 1093e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1094e41f4b71Sopenharmony_ci Canvas(this.context) 1095e41f4b71Sopenharmony_ci .width('100%') 1096e41f4b71Sopenharmony_ci .height('100%') 1097e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1098e41f4b71Sopenharmony_ci .onReady(() =>{ 1099e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 1100e41f4b71Sopenharmony_ci this.context.fillRect(20,20,200,200) 1101e41f4b71Sopenharmony_ci this.context.clearRect(30,30,150,100) 1102e41f4b71Sopenharmony_ci }) 1103e41f4b71Sopenharmony_ci } 1104e41f4b71Sopenharmony_ci .width('100%') 1105e41f4b71Sopenharmony_ci .height('100%') 1106e41f4b71Sopenharmony_ci } 1107e41f4b71Sopenharmony_ci } 1108e41f4b71Sopenharmony_ci ``` 1109e41f4b71Sopenharmony_ci 1110e41f4b71Sopenharmony_ci  1111e41f4b71Sopenharmony_ci 1112e41f4b71Sopenharmony_ci 1113e41f4b71Sopenharmony_ci### fillText 1114e41f4b71Sopenharmony_ci 1115e41f4b71Sopenharmony_cifillText(text: string, x: number, y: number, maxWidth?: number): void 1116e41f4b71Sopenharmony_ci 1117e41f4b71Sopenharmony_ci绘制填充类文本。 1118e41f4b71Sopenharmony_ci 1119e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1120e41f4b71Sopenharmony_ci 1121e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1122e41f4b71Sopenharmony_ci 1123e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1124e41f4b71Sopenharmony_ci 1125e41f4b71Sopenharmony_ci**参数:** 1126e41f4b71Sopenharmony_ci 1127e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1128e41f4b71Sopenharmony_ci| -------- | ------ | ---- | --------------- | 1129e41f4b71Sopenharmony_ci| text | string | 是 | 需要绘制的文本内容。 | 1130e41f4b71Sopenharmony_ci| x | number | 是 | 需要绘制的文本的左下角x坐标。<br>默认单位:vp。 | 1131e41f4b71Sopenharmony_ci| y | number | 是 | 需要绘制的文本的左下角y坐标。<br>默认单位:vp。 | 1132e41f4b71Sopenharmony_ci| maxWidth | number | 否 | 指定文本允许的最大宽度。<br>默认单位:vp。<br>默认值:不限制宽度。 | 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci**示例:** 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_ci ```ts 1137e41f4b71Sopenharmony_ci // xxx.ets 1138e41f4b71Sopenharmony_ci @Entry 1139e41f4b71Sopenharmony_ci @Component 1140e41f4b71Sopenharmony_ci struct FillText { 1141e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1142e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci build() { 1145e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1146e41f4b71Sopenharmony_ci Canvas(this.context) 1147e41f4b71Sopenharmony_ci .width('100%') 1148e41f4b71Sopenharmony_ci .height('100%') 1149e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1150e41f4b71Sopenharmony_ci .onReady(() =>{ 1151e41f4b71Sopenharmony_ci this.context.font = '30px sans-serif' 1152e41f4b71Sopenharmony_ci this.context.fillText("Hello World!", 20, 100) 1153e41f4b71Sopenharmony_ci }) 1154e41f4b71Sopenharmony_ci } 1155e41f4b71Sopenharmony_ci .width('100%') 1156e41f4b71Sopenharmony_ci .height('100%') 1157e41f4b71Sopenharmony_ci } 1158e41f4b71Sopenharmony_ci } 1159e41f4b71Sopenharmony_ci ``` 1160e41f4b71Sopenharmony_ci 1161e41f4b71Sopenharmony_ci  1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci 1164e41f4b71Sopenharmony_ci### strokeText 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_cistrokeText(text: string, x: number, y: number, maxWidth?: number): void 1167e41f4b71Sopenharmony_ci 1168e41f4b71Sopenharmony_ci绘制描边类文本。 1169e41f4b71Sopenharmony_ci 1170e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1171e41f4b71Sopenharmony_ci 1172e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1173e41f4b71Sopenharmony_ci 1174e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1175e41f4b71Sopenharmony_ci 1176e41f4b71Sopenharmony_ci**参数:** 1177e41f4b71Sopenharmony_ci 1178e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1179e41f4b71Sopenharmony_ci| -------- | ------ | ---- | --------------- | 1180e41f4b71Sopenharmony_ci| text | string | 是 | 需要绘制的文本内容。 | 1181e41f4b71Sopenharmony_ci| x | number | 是 | 需要绘制的文本的左下角x坐标。<br>默认单位:vp。 | 1182e41f4b71Sopenharmony_ci| y | number | 是 | 需要绘制的文本的左下角y坐标。<br>默认单位:vp。 | 1183e41f4b71Sopenharmony_ci| maxWidth | number | 否 | 需要绘制的文本的最大宽度。<br>默认单位:vp。<br>默认值:不限制宽度。 | 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ci**示例:** 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ci ```ts 1188e41f4b71Sopenharmony_ci // xxx.ets 1189e41f4b71Sopenharmony_ci @Entry 1190e41f4b71Sopenharmony_ci @Component 1191e41f4b71Sopenharmony_ci struct StrokeText { 1192e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1193e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci build() { 1196e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1197e41f4b71Sopenharmony_ci Canvas(this.context) 1198e41f4b71Sopenharmony_ci .width('100%') 1199e41f4b71Sopenharmony_ci .height('100%') 1200e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1201e41f4b71Sopenharmony_ci .onReady(() =>{ 1202e41f4b71Sopenharmony_ci this.context.font = '55px sans-serif' 1203e41f4b71Sopenharmony_ci this.context.strokeText("Hello World!", 20, 60) 1204e41f4b71Sopenharmony_ci }) 1205e41f4b71Sopenharmony_ci } 1206e41f4b71Sopenharmony_ci .width('100%') 1207e41f4b71Sopenharmony_ci .height('100%') 1208e41f4b71Sopenharmony_ci } 1209e41f4b71Sopenharmony_ci } 1210e41f4b71Sopenharmony_ci ``` 1211e41f4b71Sopenharmony_ci 1212e41f4b71Sopenharmony_ci  1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_ci### measureText 1216e41f4b71Sopenharmony_ci 1217e41f4b71Sopenharmony_cimeasureText(text: string): TextMetrics 1218e41f4b71Sopenharmony_ci 1219e41f4b71Sopenharmony_ci该方法返回一个文本测算的对象,通过该对象可以获取指定文本的宽度值。不同设备上获取的宽度值可能不同。 1220e41f4b71Sopenharmony_ci 1221e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1222e41f4b71Sopenharmony_ci 1223e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1224e41f4b71Sopenharmony_ci 1225e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_ci**参数:** 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1230e41f4b71Sopenharmony_ci| ---- | ------ | ---- |---------- | 1231e41f4b71Sopenharmony_ci| text | string | 是 | 需要进行测量的文本。 | 1232e41f4b71Sopenharmony_ci 1233e41f4b71Sopenharmony_ci**返回值:** 1234e41f4b71Sopenharmony_ci 1235e41f4b71Sopenharmony_ci| 类型 | 说明 | 1236e41f4b71Sopenharmony_ci| ----------- | ---------------------------------------- | 1237e41f4b71Sopenharmony_ci| [TextMetrics](#textmetrics) | 文本的尺寸信息。 | 1238e41f4b71Sopenharmony_ci 1239e41f4b71Sopenharmony_ci**示例:** 1240e41f4b71Sopenharmony_ci 1241e41f4b71Sopenharmony_ci ```ts 1242e41f4b71Sopenharmony_ci // xxx.ets 1243e41f4b71Sopenharmony_ci @Entry 1244e41f4b71Sopenharmony_ci @Component 1245e41f4b71Sopenharmony_ci struct MeasureText { 1246e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1247e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_ci build() { 1250e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1251e41f4b71Sopenharmony_ci Canvas(this.context) 1252e41f4b71Sopenharmony_ci .width('100%') 1253e41f4b71Sopenharmony_ci .height('100%') 1254e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1255e41f4b71Sopenharmony_ci .onReady(() =>{ 1256e41f4b71Sopenharmony_ci this.context.font = '50px sans-serif' 1257e41f4b71Sopenharmony_ci this.context.fillText("Hello World!", 20, 100) 1258e41f4b71Sopenharmony_ci this.context.fillText("width:" + this.context.measureText("Hello World!").width, 20, 200) 1259e41f4b71Sopenharmony_ci }) 1260e41f4b71Sopenharmony_ci } 1261e41f4b71Sopenharmony_ci .width('100%') 1262e41f4b71Sopenharmony_ci .height('100%') 1263e41f4b71Sopenharmony_ci } 1264e41f4b71Sopenharmony_ci } 1265e41f4b71Sopenharmony_ci ``` 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_ci  1268e41f4b71Sopenharmony_ci 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ci### stroke 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_cistroke(): void 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_ci根据当前的路径,进行边框绘制操作。 1275e41f4b71Sopenharmony_ci 1276e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci **示例:** 1283e41f4b71Sopenharmony_ci 1284e41f4b71Sopenharmony_ci ```ts 1285e41f4b71Sopenharmony_ci // xxx.ets 1286e41f4b71Sopenharmony_ci @Entry 1287e41f4b71Sopenharmony_ci @Component 1288e41f4b71Sopenharmony_ci struct Stroke { 1289e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1290e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1291e41f4b71Sopenharmony_ci 1292e41f4b71Sopenharmony_ci build() { 1293e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1294e41f4b71Sopenharmony_ci Canvas(this.context) 1295e41f4b71Sopenharmony_ci .width('100%') 1296e41f4b71Sopenharmony_ci .height('100%') 1297e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1298e41f4b71Sopenharmony_ci .onReady(() => { 1299e41f4b71Sopenharmony_ci this.context.moveTo(125, 25) 1300e41f4b71Sopenharmony_ci this.context.lineTo(125, 105) 1301e41f4b71Sopenharmony_ci this.context.lineTo(175, 105) 1302e41f4b71Sopenharmony_ci this.context.lineTo(175, 25) 1303e41f4b71Sopenharmony_ci this.context.strokeStyle = 'rgb(255,0,0)' 1304e41f4b71Sopenharmony_ci this.context.stroke() 1305e41f4b71Sopenharmony_ci }) 1306e41f4b71Sopenharmony_ci } 1307e41f4b71Sopenharmony_ci .width('100%') 1308e41f4b71Sopenharmony_ci .height('100%') 1309e41f4b71Sopenharmony_ci } 1310e41f4b71Sopenharmony_ci } 1311e41f4b71Sopenharmony_ci ``` 1312e41f4b71Sopenharmony_ci 1313e41f4b71Sopenharmony_ci  1314e41f4b71Sopenharmony_ci 1315e41f4b71Sopenharmony_cistroke(path: Path2D): void 1316e41f4b71Sopenharmony_ci 1317e41f4b71Sopenharmony_ci根据指定的路径,进行边框绘制操作。 1318e41f4b71Sopenharmony_ci 1319e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1320e41f4b71Sopenharmony_ci 1321e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1322e41f4b71Sopenharmony_ci 1323e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1324e41f4b71Sopenharmony_ci 1325e41f4b71Sopenharmony_ci**参数:** 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1328e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | ------------ | 1329e41f4b71Sopenharmony_ci| path | [Path2D](ts-components-canvas-path2d.md) | 是 | 需要绘制的Path2D。 | 1330e41f4b71Sopenharmony_ci 1331e41f4b71Sopenharmony_ci **示例:** 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_ci ```ts 1334e41f4b71Sopenharmony_ci // xxx.ets 1335e41f4b71Sopenharmony_ci @Entry 1336e41f4b71Sopenharmony_ci @Component 1337e41f4b71Sopenharmony_ci struct Stroke { 1338e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1339e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1340e41f4b71Sopenharmony_ci private path2Da: Path2D = new Path2D() 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci build() { 1343e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1344e41f4b71Sopenharmony_ci Canvas(this.context) 1345e41f4b71Sopenharmony_ci .width('100%') 1346e41f4b71Sopenharmony_ci .height('100%') 1347e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1348e41f4b71Sopenharmony_ci .onReady(() => { 1349e41f4b71Sopenharmony_ci this.path2Da.moveTo(25, 25) 1350e41f4b71Sopenharmony_ci this.path2Da.lineTo(25, 105) 1351e41f4b71Sopenharmony_ci this.path2Da.lineTo(75, 105) 1352e41f4b71Sopenharmony_ci this.path2Da.lineTo(75, 25) 1353e41f4b71Sopenharmony_ci this.context.strokeStyle = 'rgb(0,0,255)' 1354e41f4b71Sopenharmony_ci this.context.stroke(this.path2Da) 1355e41f4b71Sopenharmony_ci }) 1356e41f4b71Sopenharmony_ci } 1357e41f4b71Sopenharmony_ci .width('100%') 1358e41f4b71Sopenharmony_ci .height('100%') 1359e41f4b71Sopenharmony_ci } 1360e41f4b71Sopenharmony_ci } 1361e41f4b71Sopenharmony_ci ``` 1362e41f4b71Sopenharmony_ci 1363e41f4b71Sopenharmony_ci  1364e41f4b71Sopenharmony_ci 1365e41f4b71Sopenharmony_ci### beginPath 1366e41f4b71Sopenharmony_ci 1367e41f4b71Sopenharmony_cibeginPath(): void 1368e41f4b71Sopenharmony_ci 1369e41f4b71Sopenharmony_ci创建一个新的绘制路径。 1370e41f4b71Sopenharmony_ci 1371e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1372e41f4b71Sopenharmony_ci 1373e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1374e41f4b71Sopenharmony_ci 1375e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1376e41f4b71Sopenharmony_ci 1377e41f4b71Sopenharmony_ci**示例:** 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ci ```ts 1380e41f4b71Sopenharmony_ci // xxx.ets 1381e41f4b71Sopenharmony_ci @Entry 1382e41f4b71Sopenharmony_ci @Component 1383e41f4b71Sopenharmony_ci struct BeginPath { 1384e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1385e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci build() { 1388e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1389e41f4b71Sopenharmony_ci Canvas(this.context) 1390e41f4b71Sopenharmony_ci .width('100%') 1391e41f4b71Sopenharmony_ci .height('100%') 1392e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1393e41f4b71Sopenharmony_ci .onReady(() =>{ 1394e41f4b71Sopenharmony_ci this.context.beginPath() 1395e41f4b71Sopenharmony_ci this.context.lineWidth = 6 1396e41f4b71Sopenharmony_ci this.context.strokeStyle = '#0000ff' 1397e41f4b71Sopenharmony_ci this.context.moveTo(15, 80) 1398e41f4b71Sopenharmony_ci this.context.lineTo(280, 160) 1399e41f4b71Sopenharmony_ci this.context.stroke() 1400e41f4b71Sopenharmony_ci }) 1401e41f4b71Sopenharmony_ci } 1402e41f4b71Sopenharmony_ci .width('100%') 1403e41f4b71Sopenharmony_ci .height('100%') 1404e41f4b71Sopenharmony_ci } 1405e41f4b71Sopenharmony_ci } 1406e41f4b71Sopenharmony_ci ``` 1407e41f4b71Sopenharmony_ci 1408e41f4b71Sopenharmony_ci  1409e41f4b71Sopenharmony_ci 1410e41f4b71Sopenharmony_ci 1411e41f4b71Sopenharmony_ci### moveTo 1412e41f4b71Sopenharmony_ci 1413e41f4b71Sopenharmony_cimoveTo(x: number, y: number): void 1414e41f4b71Sopenharmony_ci 1415e41f4b71Sopenharmony_ci路径从当前点移动到指定点。 1416e41f4b71Sopenharmony_ci 1417e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1418e41f4b71Sopenharmony_ci 1419e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1420e41f4b71Sopenharmony_ci 1421e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1422e41f4b71Sopenharmony_ci 1423e41f4b71Sopenharmony_ci**参数:** 1424e41f4b71Sopenharmony_ci 1425e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1426e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------- | 1427e41f4b71Sopenharmony_ci| x | number | 是 | 指定位置的x坐标。<br>默认单位:vp。 | 1428e41f4b71Sopenharmony_ci| y | number | 是 | 指定位置的y坐标。<br>默认单位:vp。 | 1429e41f4b71Sopenharmony_ci 1430e41f4b71Sopenharmony_ci**示例:** 1431e41f4b71Sopenharmony_ci 1432e41f4b71Sopenharmony_ci ```ts 1433e41f4b71Sopenharmony_ci // xxx.ets 1434e41f4b71Sopenharmony_ci @Entry 1435e41f4b71Sopenharmony_ci @Component 1436e41f4b71Sopenharmony_ci struct MoveTo { 1437e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1438e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1439e41f4b71Sopenharmony_ci 1440e41f4b71Sopenharmony_ci build() { 1441e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1442e41f4b71Sopenharmony_ci Canvas(this.context) 1443e41f4b71Sopenharmony_ci .width('100%') 1444e41f4b71Sopenharmony_ci .height('100%') 1445e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1446e41f4b71Sopenharmony_ci .onReady(() =>{ 1447e41f4b71Sopenharmony_ci this.context.beginPath() 1448e41f4b71Sopenharmony_ci this.context.moveTo(10, 10) 1449e41f4b71Sopenharmony_ci this.context.lineTo(280, 160) 1450e41f4b71Sopenharmony_ci this.context.stroke() 1451e41f4b71Sopenharmony_ci }) 1452e41f4b71Sopenharmony_ci } 1453e41f4b71Sopenharmony_ci .width('100%') 1454e41f4b71Sopenharmony_ci .height('100%') 1455e41f4b71Sopenharmony_ci } 1456e41f4b71Sopenharmony_ci } 1457e41f4b71Sopenharmony_ci ``` 1458e41f4b71Sopenharmony_ci 1459e41f4b71Sopenharmony_ci  1460e41f4b71Sopenharmony_ci 1461e41f4b71Sopenharmony_ci 1462e41f4b71Sopenharmony_ci### lineTo 1463e41f4b71Sopenharmony_ci 1464e41f4b71Sopenharmony_cilineTo(x: number, y: number): void 1465e41f4b71Sopenharmony_ci 1466e41f4b71Sopenharmony_ci从当前点到指定点进行路径连接。 1467e41f4b71Sopenharmony_ci 1468e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1469e41f4b71Sopenharmony_ci 1470e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1471e41f4b71Sopenharmony_ci 1472e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1473e41f4b71Sopenharmony_ci 1474e41f4b71Sopenharmony_ci**参数:** 1475e41f4b71Sopenharmony_ci 1476e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1477e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------- | 1478e41f4b71Sopenharmony_ci| x | number | 是 | 指定位置的x坐标。<br>默认单位:vp。 | 1479e41f4b71Sopenharmony_ci| y | number | 是 | 指定位置的y坐标。<br>默认单位:vp。 | 1480e41f4b71Sopenharmony_ci 1481e41f4b71Sopenharmony_ci**示例:** 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ci ```ts 1484e41f4b71Sopenharmony_ci // xxx.ets 1485e41f4b71Sopenharmony_ci @Entry 1486e41f4b71Sopenharmony_ci @Component 1487e41f4b71Sopenharmony_ci struct LineTo { 1488e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1489e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci build() { 1492e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1493e41f4b71Sopenharmony_ci Canvas(this.context) 1494e41f4b71Sopenharmony_ci .width('100%') 1495e41f4b71Sopenharmony_ci .height('100%') 1496e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1497e41f4b71Sopenharmony_ci .onReady(() =>{ 1498e41f4b71Sopenharmony_ci this.context.beginPath() 1499e41f4b71Sopenharmony_ci this.context.moveTo(10, 10) 1500e41f4b71Sopenharmony_ci this.context.lineTo(280, 160) 1501e41f4b71Sopenharmony_ci this.context.stroke() 1502e41f4b71Sopenharmony_ci }) 1503e41f4b71Sopenharmony_ci } 1504e41f4b71Sopenharmony_ci .width('100%') 1505e41f4b71Sopenharmony_ci .height('100%') 1506e41f4b71Sopenharmony_ci } 1507e41f4b71Sopenharmony_ci } 1508e41f4b71Sopenharmony_ci ``` 1509e41f4b71Sopenharmony_ci 1510e41f4b71Sopenharmony_ci  1511e41f4b71Sopenharmony_ci 1512e41f4b71Sopenharmony_ci 1513e41f4b71Sopenharmony_ci### closePath 1514e41f4b71Sopenharmony_ci 1515e41f4b71Sopenharmony_ciclosePath(): void 1516e41f4b71Sopenharmony_ci 1517e41f4b71Sopenharmony_ci结束当前路径形成一个封闭路径。 1518e41f4b71Sopenharmony_ci 1519e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1520e41f4b71Sopenharmony_ci 1521e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1522e41f4b71Sopenharmony_ci 1523e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1524e41f4b71Sopenharmony_ci 1525e41f4b71Sopenharmony_ci**示例:** 1526e41f4b71Sopenharmony_ci 1527e41f4b71Sopenharmony_ci ```ts 1528e41f4b71Sopenharmony_ci // xxx.ets 1529e41f4b71Sopenharmony_ci @Entry 1530e41f4b71Sopenharmony_ci @Component 1531e41f4b71Sopenharmony_ci struct ClosePath { 1532e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1533e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1534e41f4b71Sopenharmony_ci 1535e41f4b71Sopenharmony_ci build() { 1536e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1537e41f4b71Sopenharmony_ci Canvas(this.context) 1538e41f4b71Sopenharmony_ci .width('100%') 1539e41f4b71Sopenharmony_ci .height('100%') 1540e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1541e41f4b71Sopenharmony_ci .onReady(() =>{ 1542e41f4b71Sopenharmony_ci this.context.beginPath() 1543e41f4b71Sopenharmony_ci this.context.moveTo(30, 30) 1544e41f4b71Sopenharmony_ci this.context.lineTo(110, 30) 1545e41f4b71Sopenharmony_ci this.context.lineTo(70, 90) 1546e41f4b71Sopenharmony_ci this.context.closePath() 1547e41f4b71Sopenharmony_ci this.context.stroke() 1548e41f4b71Sopenharmony_ci }) 1549e41f4b71Sopenharmony_ci } 1550e41f4b71Sopenharmony_ci .width('100%') 1551e41f4b71Sopenharmony_ci .height('100%') 1552e41f4b71Sopenharmony_ci } 1553e41f4b71Sopenharmony_ci } 1554e41f4b71Sopenharmony_ci ``` 1555e41f4b71Sopenharmony_ci 1556e41f4b71Sopenharmony_ci  1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_ci 1559e41f4b71Sopenharmony_ci### createPattern 1560e41f4b71Sopenharmony_ci 1561e41f4b71Sopenharmony_cicreatePattern(image: ImageBitmap, repetition: string | null): CanvasPattern | null 1562e41f4b71Sopenharmony_ci 1563e41f4b71Sopenharmony_ci通过指定图像和重复方式创建图片填充的模板。 1564e41f4b71Sopenharmony_ci 1565e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1566e41f4b71Sopenharmony_ci 1567e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1568e41f4b71Sopenharmony_ci 1569e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1570e41f4b71Sopenharmony_ci 1571e41f4b71Sopenharmony_ci**参数:** 1572e41f4b71Sopenharmony_ci 1573e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1574e41f4b71Sopenharmony_ci| ---------- | ---------- | ---- | ---------------------------------------- | 1575e41f4b71Sopenharmony_ci| image | [ImageBitmap](ts-components-canvas-imagebitmap.md) | 是 | 图源对象,具体参考ImageBitmap对象。 | 1576e41f4b71Sopenharmony_ci| repetition | string \| null | 是 | 设置图像重复的方式:<br>'repeat':沿x轴和y轴重复绘制图像;<br>'repeat-x':沿x轴重复绘制图像;<br>'repeat-y':沿y轴重复绘制图像;<br>'no-repeat':不重复绘制图像;<br>'clamp':在原始边界外绘制时,超出部分使用边缘的颜色绘制;<br>'mirror':沿x轴和y轴重复翻转绘制图像。 | 1577e41f4b71Sopenharmony_ci 1578e41f4b71Sopenharmony_ci**返回值:** 1579e41f4b71Sopenharmony_ci 1580e41f4b71Sopenharmony_ci| 类型 | 说明 | 1581e41f4b71Sopenharmony_ci| ---------------------------------------- | ----------------------- | 1582e41f4b71Sopenharmony_ci| [CanvasPattern](ts-components-canvas-canvaspattern.md#canvaspattern) \| null | 通过指定图像和重复方式创建图片填充的模板对象。 | 1583e41f4b71Sopenharmony_ci 1584e41f4b71Sopenharmony_ci**示例:** 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci ```ts 1587e41f4b71Sopenharmony_ci // xxx.ets 1588e41f4b71Sopenharmony_ci @Entry 1589e41f4b71Sopenharmony_ci @Component 1590e41f4b71Sopenharmony_ci struct CreatePattern { 1591e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1592e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1593e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/icon.jpg") 1594e41f4b71Sopenharmony_ci 1595e41f4b71Sopenharmony_ci build() { 1596e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1597e41f4b71Sopenharmony_ci Canvas(this.context) 1598e41f4b71Sopenharmony_ci .width('100%') 1599e41f4b71Sopenharmony_ci .height('100%') 1600e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1601e41f4b71Sopenharmony_ci .onReady(() =>{ 1602e41f4b71Sopenharmony_ci let pattern = this.context.createPattern(this.img, 'repeat') 1603e41f4b71Sopenharmony_ci if (pattern) { 1604e41f4b71Sopenharmony_ci this.context.fillStyle = pattern 1605e41f4b71Sopenharmony_ci } 1606e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 200, 200) 1607e41f4b71Sopenharmony_ci }) 1608e41f4b71Sopenharmony_ci } 1609e41f4b71Sopenharmony_ci .width('100%') 1610e41f4b71Sopenharmony_ci .height('100%') 1611e41f4b71Sopenharmony_ci } 1612e41f4b71Sopenharmony_ci } 1613e41f4b71Sopenharmony_ci ``` 1614e41f4b71Sopenharmony_ci 1615e41f4b71Sopenharmony_ci  1616e41f4b71Sopenharmony_ci 1617e41f4b71Sopenharmony_ci 1618e41f4b71Sopenharmony_ci### bezierCurveTo 1619e41f4b71Sopenharmony_ci 1620e41f4b71Sopenharmony_cibezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void 1621e41f4b71Sopenharmony_ci 1622e41f4b71Sopenharmony_ci创建三次贝赛尔曲线的路径。 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1625e41f4b71Sopenharmony_ci 1626e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1627e41f4b71Sopenharmony_ci 1628e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1629e41f4b71Sopenharmony_ci 1630e41f4b71Sopenharmony_ci**参数:** 1631e41f4b71Sopenharmony_ci 1632e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1633e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------- | 1634e41f4b71Sopenharmony_ci| cp1x | number | 是 | 第一个贝塞尔参数的x坐标值。<br>默认单位:vp。 | 1635e41f4b71Sopenharmony_ci| cp1y | number | 是 | 第一个贝塞尔参数的y坐标值。<br>默认单位:vp。 | 1636e41f4b71Sopenharmony_ci| cp2x | number | 是 | 第二个贝塞尔参数的x坐标值。<br>默认单位:vp。 | 1637e41f4b71Sopenharmony_ci| cp2y | number | 是 | 第二个贝塞尔参数的y坐标值。<br>默认单位:vp。 | 1638e41f4b71Sopenharmony_ci| x | number | 是 | 路径结束时的x坐标值。<br>默认单位:vp。 | 1639e41f4b71Sopenharmony_ci| y | number | 是 | 路径结束时的y坐标值。<br>默认单位:vp。 | 1640e41f4b71Sopenharmony_ci 1641e41f4b71Sopenharmony_ci**示例:** 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci ```ts 1644e41f4b71Sopenharmony_ci // xxx.ets 1645e41f4b71Sopenharmony_ci @Entry 1646e41f4b71Sopenharmony_ci @Component 1647e41f4b71Sopenharmony_ci struct BezierCurveTo { 1648e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1649e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1650e41f4b71Sopenharmony_ci 1651e41f4b71Sopenharmony_ci build() { 1652e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1653e41f4b71Sopenharmony_ci Canvas(this.context) 1654e41f4b71Sopenharmony_ci .width('100%') 1655e41f4b71Sopenharmony_ci .height('100%') 1656e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1657e41f4b71Sopenharmony_ci .onReady(() =>{ 1658e41f4b71Sopenharmony_ci this.context.beginPath() 1659e41f4b71Sopenharmony_ci this.context.moveTo(10, 10) 1660e41f4b71Sopenharmony_ci this.context.bezierCurveTo(20, 100, 200, 100, 200, 20) 1661e41f4b71Sopenharmony_ci this.context.stroke() 1662e41f4b71Sopenharmony_ci }) 1663e41f4b71Sopenharmony_ci } 1664e41f4b71Sopenharmony_ci .width('100%') 1665e41f4b71Sopenharmony_ci .height('100%') 1666e41f4b71Sopenharmony_ci } 1667e41f4b71Sopenharmony_ci } 1668e41f4b71Sopenharmony_ci ``` 1669e41f4b71Sopenharmony_ci 1670e41f4b71Sopenharmony_ci  1671e41f4b71Sopenharmony_ci 1672e41f4b71Sopenharmony_ci 1673e41f4b71Sopenharmony_ci### quadraticCurveTo 1674e41f4b71Sopenharmony_ci 1675e41f4b71Sopenharmony_ciquadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void 1676e41f4b71Sopenharmony_ci 1677e41f4b71Sopenharmony_ci创建二次贝赛尔曲线的路径。 1678e41f4b71Sopenharmony_ci 1679e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1680e41f4b71Sopenharmony_ci 1681e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1682e41f4b71Sopenharmony_ci 1683e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1684e41f4b71Sopenharmony_ci 1685e41f4b71Sopenharmony_ci**参数:** 1686e41f4b71Sopenharmony_ci 1687e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1688e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----------- | 1689e41f4b71Sopenharmony_ci| cpx | number | 是 | 贝塞尔参数的x坐标值。<br>默认单位:vp。 | 1690e41f4b71Sopenharmony_ci| cpy | number | 是 | 贝塞尔参数的y坐标值。<br>默认单位:vp。 | 1691e41f4b71Sopenharmony_ci| x | number | 是 | 路径结束时的x坐标值。<br>默认单位:vp。 | 1692e41f4b71Sopenharmony_ci| y | number | 是 | 路径结束时的y坐标值。<br>默认单位:vp。 | 1693e41f4b71Sopenharmony_ci 1694e41f4b71Sopenharmony_ci**示例:** 1695e41f4b71Sopenharmony_ci 1696e41f4b71Sopenharmony_ci ```ts 1697e41f4b71Sopenharmony_ci // xxx.ets 1698e41f4b71Sopenharmony_ci @Entry 1699e41f4b71Sopenharmony_ci @Component 1700e41f4b71Sopenharmony_ci struct QuadraticCurveTo { 1701e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1702e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1703e41f4b71Sopenharmony_ci 1704e41f4b71Sopenharmony_ci build() { 1705e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1706e41f4b71Sopenharmony_ci Canvas(this.context) 1707e41f4b71Sopenharmony_ci .width('100%') 1708e41f4b71Sopenharmony_ci .height('100%') 1709e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1710e41f4b71Sopenharmony_ci .onReady(() =>{ 1711e41f4b71Sopenharmony_ci this.context.beginPath() 1712e41f4b71Sopenharmony_ci this.context.moveTo(20, 20) 1713e41f4b71Sopenharmony_ci this.context.quadraticCurveTo(100, 100, 200, 20) 1714e41f4b71Sopenharmony_ci this.context.stroke() 1715e41f4b71Sopenharmony_ci }) 1716e41f4b71Sopenharmony_ci } 1717e41f4b71Sopenharmony_ci .width('100%') 1718e41f4b71Sopenharmony_ci .height('100%') 1719e41f4b71Sopenharmony_ci } 1720e41f4b71Sopenharmony_ci } 1721e41f4b71Sopenharmony_ci ``` 1722e41f4b71Sopenharmony_ci 1723e41f4b71Sopenharmony_ci  1724e41f4b71Sopenharmony_ci 1725e41f4b71Sopenharmony_ci 1726e41f4b71Sopenharmony_ci### arc 1727e41f4b71Sopenharmony_ci 1728e41f4b71Sopenharmony_ciarc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void 1729e41f4b71Sopenharmony_ci 1730e41f4b71Sopenharmony_ci绘制弧线路径。 1731e41f4b71Sopenharmony_ci 1732e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1733e41f4b71Sopenharmony_ci 1734e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1735e41f4b71Sopenharmony_ci 1736e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1737e41f4b71Sopenharmony_ci 1738e41f4b71Sopenharmony_ci**参数:** 1739e41f4b71Sopenharmony_ci 1740e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1741e41f4b71Sopenharmony_ci| ---------------- | ------- | ---- | ---------- | 1742e41f4b71Sopenharmony_ci| x | number | 是 | 弧线圆心的x坐标值。<br>默认单位:vp。 | 1743e41f4b71Sopenharmony_ci| y | number | 是 | 弧线圆心的y坐标值。<br>默认单位:vp。 | 1744e41f4b71Sopenharmony_ci| radius | number | 是 | 弧线的圆半径。<br>默认单位:vp。 | 1745e41f4b71Sopenharmony_ci| startAngle | number | 是 | 弧线的起始弧度。<br>单位:弧度 | 1746e41f4b71Sopenharmony_ci| endAngle | number | 是 | 弧线的终止弧度。<br>单位:弧度 | 1747e41f4b71Sopenharmony_ci| counterclockwise | boolean | 否 | 是否逆时针绘制圆弧。<br>true:逆时针方向绘制椭圆。<br>false:顺时针方向绘制椭圆。<br>默认值:false。 | 1748e41f4b71Sopenharmony_ci 1749e41f4b71Sopenharmony_ci**示例:** 1750e41f4b71Sopenharmony_ci 1751e41f4b71Sopenharmony_ci ```ts 1752e41f4b71Sopenharmony_ci // xxx.ets 1753e41f4b71Sopenharmony_ci @Entry 1754e41f4b71Sopenharmony_ci @Component 1755e41f4b71Sopenharmony_ci struct Arc { 1756e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1757e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1758e41f4b71Sopenharmony_ci 1759e41f4b71Sopenharmony_ci build() { 1760e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1761e41f4b71Sopenharmony_ci Canvas(this.context) 1762e41f4b71Sopenharmony_ci .width('100%') 1763e41f4b71Sopenharmony_ci .height('100%') 1764e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1765e41f4b71Sopenharmony_ci .onReady(() =>{ 1766e41f4b71Sopenharmony_ci this.context.beginPath() 1767e41f4b71Sopenharmony_ci this.context.arc(100, 75, 50, 0, 6.28) 1768e41f4b71Sopenharmony_ci this.context.stroke() 1769e41f4b71Sopenharmony_ci }) 1770e41f4b71Sopenharmony_ci } 1771e41f4b71Sopenharmony_ci .width('100%') 1772e41f4b71Sopenharmony_ci .height('100%') 1773e41f4b71Sopenharmony_ci } 1774e41f4b71Sopenharmony_ci } 1775e41f4b71Sopenharmony_ci ``` 1776e41f4b71Sopenharmony_ci 1777e41f4b71Sopenharmony_ci  1778e41f4b71Sopenharmony_ci 1779e41f4b71Sopenharmony_ci 1780e41f4b71Sopenharmony_ci### arcTo 1781e41f4b71Sopenharmony_ci 1782e41f4b71Sopenharmony_ciarcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void 1783e41f4b71Sopenharmony_ci 1784e41f4b71Sopenharmony_ci依据给定的控制点和圆弧半径创建圆弧路径。 1785e41f4b71Sopenharmony_ci 1786e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1787e41f4b71Sopenharmony_ci 1788e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1789e41f4b71Sopenharmony_ci 1790e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1791e41f4b71Sopenharmony_ci 1792e41f4b71Sopenharmony_ci**参数:** 1793e41f4b71Sopenharmony_ci 1794e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1795e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- | 1796e41f4b71Sopenharmony_ci| x1 | number | 是 | 第一个控制点的x坐标值。<br>默认单位:vp。 | 1797e41f4b71Sopenharmony_ci| y1 | number | 是 | 第一个控制点的y坐标值。<br>默认单位:vp。 | 1798e41f4b71Sopenharmony_ci| x2 | number | 是 | 第二个控制点的x坐标值。<br>默认单位:vp。 | 1799e41f4b71Sopenharmony_ci| y2 | number | 是 | 第二个控制点的y坐标值。<br>默认单位:vp。 | 1800e41f4b71Sopenharmony_ci| radius | number | 是 | 圆弧的圆半径值。<br>默认单位:vp。 | 1801e41f4b71Sopenharmony_ci 1802e41f4b71Sopenharmony_ci**示例:** 1803e41f4b71Sopenharmony_ci 1804e41f4b71Sopenharmony_ci ```ts 1805e41f4b71Sopenharmony_ci // xxx.ets 1806e41f4b71Sopenharmony_ci @Entry 1807e41f4b71Sopenharmony_ci @Component 1808e41f4b71Sopenharmony_ci struct ArcTo { 1809e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1810e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1811e41f4b71Sopenharmony_ci 1812e41f4b71Sopenharmony_ci build() { 1813e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1814e41f4b71Sopenharmony_ci Canvas(this.context) 1815e41f4b71Sopenharmony_ci .width('100%') 1816e41f4b71Sopenharmony_ci .height('100%') 1817e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1818e41f4b71Sopenharmony_ci .onReady(() =>{ 1819e41f4b71Sopenharmony_ci // 切线 1820e41f4b71Sopenharmony_ci this.context.beginPath() 1821e41f4b71Sopenharmony_ci this.context.strokeStyle = '#808080' 1822e41f4b71Sopenharmony_ci this.context.lineWidth = 1.5; 1823e41f4b71Sopenharmony_ci this.context.moveTo(360, 20); 1824e41f4b71Sopenharmony_ci this.context.lineTo(360, 170); 1825e41f4b71Sopenharmony_ci this.context.lineTo(110, 170); 1826e41f4b71Sopenharmony_ci this.context.stroke(); 1827e41f4b71Sopenharmony_ci 1828e41f4b71Sopenharmony_ci // 圆弧 1829e41f4b71Sopenharmony_ci this.context.beginPath() 1830e41f4b71Sopenharmony_ci this.context.strokeStyle = '#000000' 1831e41f4b71Sopenharmony_ci this.context.lineWidth = 3; 1832e41f4b71Sopenharmony_ci this.context.moveTo(360, 20) 1833e41f4b71Sopenharmony_ci this.context.arcTo(360, 170, 110, 170, 150) 1834e41f4b71Sopenharmony_ci this.context.stroke() 1835e41f4b71Sopenharmony_ci 1836e41f4b71Sopenharmony_ci // 起始点 1837e41f4b71Sopenharmony_ci this.context.beginPath(); 1838e41f4b71Sopenharmony_ci this.context.fillStyle = '#00ff00'; 1839e41f4b71Sopenharmony_ci this.context.arc(360, 20, 4, 0, 2 * Math.PI); 1840e41f4b71Sopenharmony_ci this.context.fill(); 1841e41f4b71Sopenharmony_ci 1842e41f4b71Sopenharmony_ci // 控制点 1843e41f4b71Sopenharmony_ci this.context.beginPath(); 1844e41f4b71Sopenharmony_ci this.context.fillStyle = '#ff0000'; 1845e41f4b71Sopenharmony_ci this.context.arc(360, 170, 4, 0, 2 * Math.PI); 1846e41f4b71Sopenharmony_ci this.context.arc(110, 170, 4, 0, 2 * Math.PI); 1847e41f4b71Sopenharmony_ci this.context.fill(); 1848e41f4b71Sopenharmony_ci }) 1849e41f4b71Sopenharmony_ci } 1850e41f4b71Sopenharmony_ci .width('100%') 1851e41f4b71Sopenharmony_ci .height('100%') 1852e41f4b71Sopenharmony_ci } 1853e41f4b71Sopenharmony_ci } 1854e41f4b71Sopenharmony_ci ``` 1855e41f4b71Sopenharmony_ci 1856e41f4b71Sopenharmony_ci  1857e41f4b71Sopenharmony_ci 1858e41f4b71Sopenharmony_ci > 此示例中,arcTo()创建的圆弧为黑色,圆弧的两条切线为灰色。控制点为红色,起始点为绿色。 1859e41f4b71Sopenharmony_ci > 1860e41f4b71Sopenharmony_ci > 可以想象两条切线:一条切线从起始点到第一个控制点,另一条切线从第一个控制点到第二个控制点。arcTo()在这两条切线间创建一个圆弧,并使圆弧与这两条切线都相切。 1861e41f4b71Sopenharmony_ci 1862e41f4b71Sopenharmony_ci 1863e41f4b71Sopenharmony_ci### ellipse 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ciellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci在规定的矩形区域绘制一个椭圆。 1868e41f4b71Sopenharmony_ci 1869e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1872e41f4b71Sopenharmony_ci 1873e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1874e41f4b71Sopenharmony_ci 1875e41f4b71Sopenharmony_ci**参数:** 1876e41f4b71Sopenharmony_ci 1877e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1878e41f4b71Sopenharmony_ci| ---------------- | ------- | ---- | ---------------------------------------- | 1879e41f4b71Sopenharmony_ci| x | number | 是 | 椭圆圆心的x轴坐标。<br>默认单位:vp。 | 1880e41f4b71Sopenharmony_ci| y | number | 是 | 椭圆圆心的y轴坐标。<br>默认单位:vp。 | 1881e41f4b71Sopenharmony_ci| radiusX | number | 是 | 椭圆x轴的半径长度。<br>默认单位:vp。 | 1882e41f4b71Sopenharmony_ci| radiusY | number | 是 | 椭圆y轴的半径长度。<br>默认单位:vp。 | 1883e41f4b71Sopenharmony_ci| rotation | number | 是 | 椭圆的旋转角度。<br>单位:弧度。 | 1884e41f4b71Sopenharmony_ci| startAngle | number | 是 | 椭圆绘制的起始点角度。<br>单位:弧度。 | 1885e41f4b71Sopenharmony_ci| endAngle | number | 是 | 椭圆绘制的结束点角度。<br>单位:弧度。 | 1886e41f4b71Sopenharmony_ci| counterclockwise | boolean | 否 | 是否以逆时针方向绘制椭圆。<br>true:逆时针方向绘制椭圆。<br>false:顺时针方向绘制椭圆。<br>默认值:false。 | 1887e41f4b71Sopenharmony_ci 1888e41f4b71Sopenharmony_ci**示例:** 1889e41f4b71Sopenharmony_ci 1890e41f4b71Sopenharmony_ci ```ts 1891e41f4b71Sopenharmony_ci // xxx.ets 1892e41f4b71Sopenharmony_ci @Entry 1893e41f4b71Sopenharmony_ci @Component 1894e41f4b71Sopenharmony_ci struct CanvasExample { 1895e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1896e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1897e41f4b71Sopenharmony_ci 1898e41f4b71Sopenharmony_ci build() { 1899e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1900e41f4b71Sopenharmony_ci Canvas(this.context) 1901e41f4b71Sopenharmony_ci .width('100%') 1902e41f4b71Sopenharmony_ci .height('100%') 1903e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1904e41f4b71Sopenharmony_ci .onReady(() =>{ 1905e41f4b71Sopenharmony_ci this.context.beginPath() 1906e41f4b71Sopenharmony_ci this.context.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2, false) 1907e41f4b71Sopenharmony_ci this.context.stroke() 1908e41f4b71Sopenharmony_ci this.context.beginPath() 1909e41f4b71Sopenharmony_ci this.context.ellipse(200, 300, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI * 2, true) 1910e41f4b71Sopenharmony_ci this.context.stroke() 1911e41f4b71Sopenharmony_ci }) 1912e41f4b71Sopenharmony_ci } 1913e41f4b71Sopenharmony_ci .width('100%') 1914e41f4b71Sopenharmony_ci .height('100%') 1915e41f4b71Sopenharmony_ci } 1916e41f4b71Sopenharmony_ci } 1917e41f4b71Sopenharmony_ci ``` 1918e41f4b71Sopenharmony_ci 1919e41f4b71Sopenharmony_ci  1920e41f4b71Sopenharmony_ci 1921e41f4b71Sopenharmony_ci 1922e41f4b71Sopenharmony_ci### rect 1923e41f4b71Sopenharmony_ci 1924e41f4b71Sopenharmony_cirect(x: number, y: number, w: number, h: number): void 1925e41f4b71Sopenharmony_ci 1926e41f4b71Sopenharmony_ci创建矩形路径。 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1929e41f4b71Sopenharmony_ci 1930e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1933e41f4b71Sopenharmony_ci 1934e41f4b71Sopenharmony_ci**参数:** 1935e41f4b71Sopenharmony_ci 1936e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1937e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------- | 1938e41f4b71Sopenharmony_ci| x | number | 是 | 指定矩形的左上角x坐标值。<br>默认单位:vp。 | 1939e41f4b71Sopenharmony_ci| y | number | 是 | 指定矩形的左上角y坐标值。<br>默认单位:vp。 | 1940e41f4b71Sopenharmony_ci| w | number | 是 | 指定矩形的宽度。<br>默认单位:vp。 | 1941e41f4b71Sopenharmony_ci| h | number | 是 | 指定矩形的高度。<br>默认单位:vp。 | 1942e41f4b71Sopenharmony_ci 1943e41f4b71Sopenharmony_ci**示例:** 1944e41f4b71Sopenharmony_ci 1945e41f4b71Sopenharmony_ci ```ts 1946e41f4b71Sopenharmony_ci // xxx.ets 1947e41f4b71Sopenharmony_ci @Entry 1948e41f4b71Sopenharmony_ci @Component 1949e41f4b71Sopenharmony_ci struct CanvasExample { 1950e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1951e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 1952e41f4b71Sopenharmony_ci 1953e41f4b71Sopenharmony_ci build() { 1954e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1955e41f4b71Sopenharmony_ci Canvas(this.context) 1956e41f4b71Sopenharmony_ci .width('100%') 1957e41f4b71Sopenharmony_ci .height('100%') 1958e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 1959e41f4b71Sopenharmony_ci .onReady(() =>{ 1960e41f4b71Sopenharmony_ci this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20) 1961e41f4b71Sopenharmony_ci this.context.stroke() 1962e41f4b71Sopenharmony_ci }) 1963e41f4b71Sopenharmony_ci } 1964e41f4b71Sopenharmony_ci .width('100%') 1965e41f4b71Sopenharmony_ci .height('100%') 1966e41f4b71Sopenharmony_ci } 1967e41f4b71Sopenharmony_ci } 1968e41f4b71Sopenharmony_ci ``` 1969e41f4b71Sopenharmony_ci 1970e41f4b71Sopenharmony_ci  1971e41f4b71Sopenharmony_ci 1972e41f4b71Sopenharmony_ci 1973e41f4b71Sopenharmony_ci### fill 1974e41f4b71Sopenharmony_ci 1975e41f4b71Sopenharmony_cifill(fillRule?: CanvasFillRule): void 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_ci对封闭路径进行填充。 1978e41f4b71Sopenharmony_ci 1979e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 1980e41f4b71Sopenharmony_ci 1981e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1982e41f4b71Sopenharmony_ci 1983e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1984e41f4b71Sopenharmony_ci 1985e41f4b71Sopenharmony_ci**参数:** 1986e41f4b71Sopenharmony_ci 1987e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1988e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------------- | 1989e41f4b71Sopenharmony_ci| fillRule | [CanvasFillRule](ts-canvasrenderingcontext2d.md#canvasfillrule) | 否 | 指定要填充对象的规则。<br/>可选参数为:"nonzero", "evenodd"。<br>默认值:"nonzero"。 | 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_ci 1992e41f4b71Sopenharmony_ci**示例:** 1993e41f4b71Sopenharmony_ci 1994e41f4b71Sopenharmony_ci ```ts 1995e41f4b71Sopenharmony_ci // xxx.ets 1996e41f4b71Sopenharmony_ci @Entry 1997e41f4b71Sopenharmony_ci @Component 1998e41f4b71Sopenharmony_ci struct Fill { 1999e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2000e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2001e41f4b71Sopenharmony_ci 2002e41f4b71Sopenharmony_ci build() { 2003e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2004e41f4b71Sopenharmony_ci Canvas(this.context) 2005e41f4b71Sopenharmony_ci .width('100%') 2006e41f4b71Sopenharmony_ci .height('100%') 2007e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2008e41f4b71Sopenharmony_ci .onReady(() =>{ 2009e41f4b71Sopenharmony_ci this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20) 2010e41f4b71Sopenharmony_ci this.context.fill() 2011e41f4b71Sopenharmony_ci }) 2012e41f4b71Sopenharmony_ci } 2013e41f4b71Sopenharmony_ci .width('100%') 2014e41f4b71Sopenharmony_ci .height('100%') 2015e41f4b71Sopenharmony_ci } 2016e41f4b71Sopenharmony_ci } 2017e41f4b71Sopenharmony_ci ``` 2018e41f4b71Sopenharmony_ci 2019e41f4b71Sopenharmony_ci  2020e41f4b71Sopenharmony_ci 2021e41f4b71Sopenharmony_ci 2022e41f4b71Sopenharmony_cifill(path: Path2D, fillRule?: CanvasFillRule): void 2023e41f4b71Sopenharmony_ci 2024e41f4b71Sopenharmony_ci对封闭路径进行填充。 2025e41f4b71Sopenharmony_ci 2026e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2027e41f4b71Sopenharmony_ci 2028e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2029e41f4b71Sopenharmony_ci 2030e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2031e41f4b71Sopenharmony_ci 2032e41f4b71Sopenharmony_ci**参数:** 2033e41f4b71Sopenharmony_ci 2034e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2035e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------------- | 2036e41f4b71Sopenharmony_ci| path | [Path2D](ts-components-canvas-path2d.md) | 是 | Path2D填充路径。 | 2037e41f4b71Sopenharmony_ci| fillRule | [CanvasFillRule](ts-canvasrenderingcontext2d.md#canvasfillrule) | 否 | 指定要填充对象的规则。<br/>可选参数为:"nonzero", "evenodd"。<br>默认值:"nonzero"。 | 2038e41f4b71Sopenharmony_ci 2039e41f4b71Sopenharmony_ci 2040e41f4b71Sopenharmony_ci**示例:** 2041e41f4b71Sopenharmony_ci 2042e41f4b71Sopenharmony_ci```ts 2043e41f4b71Sopenharmony_ci// xxx.ets 2044e41f4b71Sopenharmony_ci@Entry 2045e41f4b71Sopenharmony_ci@Component 2046e41f4b71Sopenharmony_cistruct Fill { 2047e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2048e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2049e41f4b71Sopenharmony_ci 2050e41f4b71Sopenharmony_ci build() { 2051e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2052e41f4b71Sopenharmony_ci Canvas(this.context) 2053e41f4b71Sopenharmony_ci .width('100%') 2054e41f4b71Sopenharmony_ci .height('100%') 2055e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2056e41f4b71Sopenharmony_ci .onReady(() =>{ 2057e41f4b71Sopenharmony_ci let region = new Path2D() 2058e41f4b71Sopenharmony_ci region.moveTo(30, 90) 2059e41f4b71Sopenharmony_ci region.lineTo(110, 20) 2060e41f4b71Sopenharmony_ci region.lineTo(240, 130) 2061e41f4b71Sopenharmony_ci region.lineTo(60, 130) 2062e41f4b71Sopenharmony_ci region.lineTo(190, 20) 2063e41f4b71Sopenharmony_ci region.lineTo(270, 90) 2064e41f4b71Sopenharmony_ci region.closePath() 2065e41f4b71Sopenharmony_ci // Fill path 2066e41f4b71Sopenharmony_ci this.context.fillStyle = '#00ff00' 2067e41f4b71Sopenharmony_ci this.context.fill(region, "evenodd") 2068e41f4b71Sopenharmony_ci }) 2069e41f4b71Sopenharmony_ci } 2070e41f4b71Sopenharmony_ci .width('100%') 2071e41f4b71Sopenharmony_ci .height('100%') 2072e41f4b71Sopenharmony_ci } 2073e41f4b71Sopenharmony_ci} 2074e41f4b71Sopenharmony_ci``` 2075e41f4b71Sopenharmony_ci 2076e41f4b71Sopenharmony_ci  2077e41f4b71Sopenharmony_ci 2078e41f4b71Sopenharmony_ci 2079e41f4b71Sopenharmony_ci### clip 2080e41f4b71Sopenharmony_ci 2081e41f4b71Sopenharmony_ciclip(fillRule?: CanvasFillRule): void 2082e41f4b71Sopenharmony_ci 2083e41f4b71Sopenharmony_ci设置当前路径为剪切路径。 2084e41f4b71Sopenharmony_ci 2085e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2086e41f4b71Sopenharmony_ci 2087e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2088e41f4b71Sopenharmony_ci 2089e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2090e41f4b71Sopenharmony_ci 2091e41f4b71Sopenharmony_ci**参数:** 2092e41f4b71Sopenharmony_ci 2093e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2094e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------------- | 2095e41f4b71Sopenharmony_ci| fillRule | [CanvasFillRule](#canvasfillrule) | 否 | 指定要剪切对象的规则。<br/>可选参数为:"nonzero", "evenodd"。 <br>默认值:"nonzero"。 | 2096e41f4b71Sopenharmony_ci 2097e41f4b71Sopenharmony_ci**示例:** 2098e41f4b71Sopenharmony_ci 2099e41f4b71Sopenharmony_ci ```ts 2100e41f4b71Sopenharmony_ci // xxx.ets 2101e41f4b71Sopenharmony_ci @Entry 2102e41f4b71Sopenharmony_ci @Component 2103e41f4b71Sopenharmony_ci struct Clip { 2104e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2105e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2106e41f4b71Sopenharmony_ci 2107e41f4b71Sopenharmony_ci build() { 2108e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2109e41f4b71Sopenharmony_ci Canvas(this.context) 2110e41f4b71Sopenharmony_ci .width('100%') 2111e41f4b71Sopenharmony_ci .height('100%') 2112e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2113e41f4b71Sopenharmony_ci .onReady(() =>{ 2114e41f4b71Sopenharmony_ci this.context.rect(0, 0, 100, 200) 2115e41f4b71Sopenharmony_ci this.context.stroke() 2116e41f4b71Sopenharmony_ci this.context.clip() 2117e41f4b71Sopenharmony_ci this.context.fillStyle = "rgb(255,0,0)" 2118e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 200, 200) 2119e41f4b71Sopenharmony_ci }) 2120e41f4b71Sopenharmony_ci } 2121e41f4b71Sopenharmony_ci .width('100%') 2122e41f4b71Sopenharmony_ci .height('100%') 2123e41f4b71Sopenharmony_ci } 2124e41f4b71Sopenharmony_ci } 2125e41f4b71Sopenharmony_ci ``` 2126e41f4b71Sopenharmony_ci 2127e41f4b71Sopenharmony_ci  2128e41f4b71Sopenharmony_ci 2129e41f4b71Sopenharmony_ci 2130e41f4b71Sopenharmony_ciclip(path: Path2D, fillRule?: CanvasFillRule): void 2131e41f4b71Sopenharmony_ci 2132e41f4b71Sopenharmony_ci设置当前路径为剪切路径 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2135e41f4b71Sopenharmony_ci 2136e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2137e41f4b71Sopenharmony_ci 2138e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2139e41f4b71Sopenharmony_ci 2140e41f4b71Sopenharmony_ci**参数:** 2141e41f4b71Sopenharmony_ci 2142e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2143e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------------- | 2144e41f4b71Sopenharmony_ci| path | [Path2D](ts-components-canvas-path2d.md) | 是 | Path2D剪切路径。 | 2145e41f4b71Sopenharmony_ci| fillRule | [CanvasFillRule](#canvasfillrule) | 否 | 指定要剪切对象的规则。<br/>可选参数为:"nonzero", "evenodd"。 <br>默认值:"nonzero"。 | 2146e41f4b71Sopenharmony_ci 2147e41f4b71Sopenharmony_ci 2148e41f4b71Sopenharmony_ci**示例:** 2149e41f4b71Sopenharmony_ci 2150e41f4b71Sopenharmony_ci ```ts 2151e41f4b71Sopenharmony_ci // xxx.ets 2152e41f4b71Sopenharmony_ci @Entry 2153e41f4b71Sopenharmony_ci @Component 2154e41f4b71Sopenharmony_ci struct Clip { 2155e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2156e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2157e41f4b71Sopenharmony_ci build() { 2158e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2159e41f4b71Sopenharmony_ci Canvas(this.context) 2160e41f4b71Sopenharmony_ci .width('100%') 2161e41f4b71Sopenharmony_ci .height('100%') 2162e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2163e41f4b71Sopenharmony_ci .onReady(() =>{ 2164e41f4b71Sopenharmony_ci let region = new Path2D() 2165e41f4b71Sopenharmony_ci region.moveTo(30, 90) 2166e41f4b71Sopenharmony_ci region.lineTo(110, 20) 2167e41f4b71Sopenharmony_ci region.lineTo(240, 130) 2168e41f4b71Sopenharmony_ci region.lineTo(60, 130) 2169e41f4b71Sopenharmony_ci region.lineTo(190, 20) 2170e41f4b71Sopenharmony_ci region.lineTo(270, 90) 2171e41f4b71Sopenharmony_ci region.closePath() 2172e41f4b71Sopenharmony_ci this.context.clip(region,"evenodd") 2173e41f4b71Sopenharmony_ci this.context.fillStyle = "rgb(0,255,0)" 2174e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, this.context.width, this.context.height) 2175e41f4b71Sopenharmony_ci }) 2176e41f4b71Sopenharmony_ci } 2177e41f4b71Sopenharmony_ci .width('100%') 2178e41f4b71Sopenharmony_ci .height('100%') 2179e41f4b71Sopenharmony_ci } 2180e41f4b71Sopenharmony_ci } 2181e41f4b71Sopenharmony_ci ``` 2182e41f4b71Sopenharmony_ci 2183e41f4b71Sopenharmony_ci  2184e41f4b71Sopenharmony_ci 2185e41f4b71Sopenharmony_ci 2186e41f4b71Sopenharmony_ci### reset<sup>12+</sup> 2187e41f4b71Sopenharmony_ci 2188e41f4b71Sopenharmony_cireset(): void 2189e41f4b71Sopenharmony_ci 2190e41f4b71Sopenharmony_ci将CanvasRenderingContext2D重置为其默认状态,清除后台缓冲区、绘制状态栈、绘制路径和样式。 2191e41f4b71Sopenharmony_ci 2192e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 2193e41f4b71Sopenharmony_ci 2194e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2195e41f4b71Sopenharmony_ci 2196e41f4b71Sopenharmony_ci**示例:** 2197e41f4b71Sopenharmony_ci 2198e41f4b71Sopenharmony_ci ```ts 2199e41f4b71Sopenharmony_ci // xxx.ets 2200e41f4b71Sopenharmony_ci @Entry 2201e41f4b71Sopenharmony_ci @Component 2202e41f4b71Sopenharmony_ci struct Reset { 2203e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2204e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2205e41f4b71Sopenharmony_ci 2206e41f4b71Sopenharmony_ci build() { 2207e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2208e41f4b71Sopenharmony_ci Canvas(this.context) 2209e41f4b71Sopenharmony_ci .width('100%') 2210e41f4b71Sopenharmony_ci .height('100%') 2211e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2212e41f4b71Sopenharmony_ci .onReady(() =>{ 2213e41f4b71Sopenharmony_ci this.context.fillStyle = '#0000ff' 2214e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 150, 100) 2215e41f4b71Sopenharmony_ci this.context.reset() 2216e41f4b71Sopenharmony_ci this.context.fillRect(20, 150, 150, 100) 2217e41f4b71Sopenharmony_ci }) 2218e41f4b71Sopenharmony_ci } 2219e41f4b71Sopenharmony_ci .width('100%') 2220e41f4b71Sopenharmony_ci .height('100%') 2221e41f4b71Sopenharmony_ci } 2222e41f4b71Sopenharmony_ci } 2223e41f4b71Sopenharmony_ci ``` 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci  2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci 2228e41f4b71Sopenharmony_ci### saveLayer<sup>12+</sup> 2229e41f4b71Sopenharmony_ci 2230e41f4b71Sopenharmony_cisaveLayer(): void 2231e41f4b71Sopenharmony_ci 2232e41f4b71Sopenharmony_ci创建一个图层。 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 2235e41f4b71Sopenharmony_ci 2236e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2237e41f4b71Sopenharmony_ci 2238e41f4b71Sopenharmony_ci**示例:** 2239e41f4b71Sopenharmony_ci 2240e41f4b71Sopenharmony_ci ```ts 2241e41f4b71Sopenharmony_ci // xxx.ets 2242e41f4b71Sopenharmony_ci @Entry 2243e41f4b71Sopenharmony_ci @Component 2244e41f4b71Sopenharmony_ci struct saveLayer { 2245e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2246e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2247e41f4b71Sopenharmony_ci 2248e41f4b71Sopenharmony_ci build() { 2249e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2250e41f4b71Sopenharmony_ci Canvas(this.context) 2251e41f4b71Sopenharmony_ci .width('100%') 2252e41f4b71Sopenharmony_ci .height('100%') 2253e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2254e41f4b71Sopenharmony_ci .onReady(() =>{ 2255e41f4b71Sopenharmony_ci this.context.fillStyle = "#0000ff" 2256e41f4b71Sopenharmony_ci this.context.fillRect(50,100,300,100) 2257e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ffff" 2258e41f4b71Sopenharmony_ci this.context.fillRect(50,150,300,100) 2259e41f4b71Sopenharmony_ci this.context.globalCompositeOperation = 'destination-over' 2260e41f4b71Sopenharmony_ci this.context.saveLayer() 2261e41f4b71Sopenharmony_ci this.context.globalCompositeOperation = 'source-over' 2262e41f4b71Sopenharmony_ci this.context.fillStyle = "#ff0000" 2263e41f4b71Sopenharmony_ci this.context.fillRect(100,50,100,300) 2264e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ff00" 2265e41f4b71Sopenharmony_ci this.context.fillRect(150,50,100,300) 2266e41f4b71Sopenharmony_ci this.context.restoreLayer() 2267e41f4b71Sopenharmony_ci }) 2268e41f4b71Sopenharmony_ci } 2269e41f4b71Sopenharmony_ci .width('100%') 2270e41f4b71Sopenharmony_ci .height('100%') 2271e41f4b71Sopenharmony_ci } 2272e41f4b71Sopenharmony_ci } 2273e41f4b71Sopenharmony_ci 2274e41f4b71Sopenharmony_ci ``` 2275e41f4b71Sopenharmony_ci  2276e41f4b71Sopenharmony_ci 2277e41f4b71Sopenharmony_ci### restoreLayer<sup>12+</sup> 2278e41f4b71Sopenharmony_ci 2279e41f4b71Sopenharmony_cirestoreLayer(): void 2280e41f4b71Sopenharmony_ci 2281e41f4b71Sopenharmony_ci恢复图像变换和裁剪状态至saveLayer前的状态,并将图层绘制在canvas上。restoreLayer示例代码同saveLayer。 2282e41f4b71Sopenharmony_ci 2283e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 2284e41f4b71Sopenharmony_ci 2285e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2286e41f4b71Sopenharmony_ci 2287e41f4b71Sopenharmony_ci### resetTransform 2288e41f4b71Sopenharmony_ci 2289e41f4b71Sopenharmony_ciresetTransform(): void 2290e41f4b71Sopenharmony_ci 2291e41f4b71Sopenharmony_ci使用单位矩阵重新设置当前矩阵。 2292e41f4b71Sopenharmony_ci 2293e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2294e41f4b71Sopenharmony_ci 2295e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2296e41f4b71Sopenharmony_ci 2297e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2298e41f4b71Sopenharmony_ci 2299e41f4b71Sopenharmony_ci**示例:** 2300e41f4b71Sopenharmony_ci 2301e41f4b71Sopenharmony_ci ```ts 2302e41f4b71Sopenharmony_ci // xxx.ets 2303e41f4b71Sopenharmony_ci @Entry 2304e41f4b71Sopenharmony_ci @Component 2305e41f4b71Sopenharmony_ci struct ResetTransform { 2306e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2307e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2308e41f4b71Sopenharmony_ci 2309e41f4b71Sopenharmony_ci build() { 2310e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2311e41f4b71Sopenharmony_ci Canvas(this.context) 2312e41f4b71Sopenharmony_ci .width('100%') 2313e41f4b71Sopenharmony_ci .height('100%') 2314e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2315e41f4b71Sopenharmony_ci .onReady(() =>{ 2316e41f4b71Sopenharmony_ci this.context.setTransform(1,0.5, -0.5, 1, 10, 10) 2317e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 2318e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2319e41f4b71Sopenharmony_ci this.context.resetTransform() 2320e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 2321e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2322e41f4b71Sopenharmony_ci }) 2323e41f4b71Sopenharmony_ci } 2324e41f4b71Sopenharmony_ci .width('100%') 2325e41f4b71Sopenharmony_ci .height('100%') 2326e41f4b71Sopenharmony_ci } 2327e41f4b71Sopenharmony_ci } 2328e41f4b71Sopenharmony_ci ``` 2329e41f4b71Sopenharmony_ci 2330e41f4b71Sopenharmony_ci  2331e41f4b71Sopenharmony_ci 2332e41f4b71Sopenharmony_ci### rotate 2333e41f4b71Sopenharmony_ci 2334e41f4b71Sopenharmony_cirotate(angle: number): void 2335e41f4b71Sopenharmony_ci 2336e41f4b71Sopenharmony_ci针对当前坐标轴进行顺时针旋转。 2337e41f4b71Sopenharmony_ci 2338e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2339e41f4b71Sopenharmony_ci 2340e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2341e41f4b71Sopenharmony_ci 2342e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2343e41f4b71Sopenharmony_ci 2344e41f4b71Sopenharmony_ci**参数:** 2345e41f4b71Sopenharmony_ci 2346e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2347e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ---------------------------------------- | 2348e41f4b71Sopenharmony_ci| angle | number | 是 | 设置顺时针旋转的弧度值,可以通过 degree * Math.PI / 180 将角度转换为弧度值。<br>单位:弧度。 | 2349e41f4b71Sopenharmony_ci 2350e41f4b71Sopenharmony_ci**示例:** 2351e41f4b71Sopenharmony_ci 2352e41f4b71Sopenharmony_ci ```ts 2353e41f4b71Sopenharmony_ci // xxx.ets 2354e41f4b71Sopenharmony_ci @Entry 2355e41f4b71Sopenharmony_ci @Component 2356e41f4b71Sopenharmony_ci struct Rotate { 2357e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2358e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2359e41f4b71Sopenharmony_ci 2360e41f4b71Sopenharmony_ci build() { 2361e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2362e41f4b71Sopenharmony_ci Canvas(this.context) 2363e41f4b71Sopenharmony_ci .width('100%') 2364e41f4b71Sopenharmony_ci .height('100%') 2365e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2366e41f4b71Sopenharmony_ci .onReady(() =>{ 2367e41f4b71Sopenharmony_ci this.context.rotate(45 * Math.PI / 180) 2368e41f4b71Sopenharmony_ci this.context.fillRect(70, 20, 50, 50) 2369e41f4b71Sopenharmony_ci }) 2370e41f4b71Sopenharmony_ci } 2371e41f4b71Sopenharmony_ci .width('100%') 2372e41f4b71Sopenharmony_ci .height('100%') 2373e41f4b71Sopenharmony_ci } 2374e41f4b71Sopenharmony_ci } 2375e41f4b71Sopenharmony_ci ``` 2376e41f4b71Sopenharmony_ci 2377e41f4b71Sopenharmony_ci  2378e41f4b71Sopenharmony_ci 2379e41f4b71Sopenharmony_ci 2380e41f4b71Sopenharmony_ci### scale 2381e41f4b71Sopenharmony_ci 2382e41f4b71Sopenharmony_ciscale(x: number, y: number): void 2383e41f4b71Sopenharmony_ci 2384e41f4b71Sopenharmony_ci设置canvas画布的缩放变换属性,后续的绘制操作将按照缩放比例进行缩放。 2385e41f4b71Sopenharmony_ci 2386e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2387e41f4b71Sopenharmony_ci 2388e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2389e41f4b71Sopenharmony_ci 2390e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2391e41f4b71Sopenharmony_ci 2392e41f4b71Sopenharmony_ci**参数:** 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2395e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----------- | 2396e41f4b71Sopenharmony_ci| x | number | 是 | 设置水平方向的缩放值。 | 2397e41f4b71Sopenharmony_ci| y | number | 是 | 设置垂直方向的缩放值。 | 2398e41f4b71Sopenharmony_ci 2399e41f4b71Sopenharmony_ci**示例:** 2400e41f4b71Sopenharmony_ci 2401e41f4b71Sopenharmony_ci ```ts 2402e41f4b71Sopenharmony_ci // xxx.ets 2403e41f4b71Sopenharmony_ci @Entry 2404e41f4b71Sopenharmony_ci @Component 2405e41f4b71Sopenharmony_ci struct Scale { 2406e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2407e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2408e41f4b71Sopenharmony_ci 2409e41f4b71Sopenharmony_ci build() { 2410e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2411e41f4b71Sopenharmony_ci Canvas(this.context) 2412e41f4b71Sopenharmony_ci .width('100%') 2413e41f4b71Sopenharmony_ci .height('100%') 2414e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2415e41f4b71Sopenharmony_ci .onReady(() =>{ 2416e41f4b71Sopenharmony_ci this.context.lineWidth = 3 2417e41f4b71Sopenharmony_ci this.context.strokeRect(30, 30, 50, 50) 2418e41f4b71Sopenharmony_ci this.context.scale(2, 2) // Scale to 200% 2419e41f4b71Sopenharmony_ci this.context.strokeRect(30, 30, 50, 50) 2420e41f4b71Sopenharmony_ci }) 2421e41f4b71Sopenharmony_ci } 2422e41f4b71Sopenharmony_ci .width('100%') 2423e41f4b71Sopenharmony_ci .height('100%') 2424e41f4b71Sopenharmony_ci } 2425e41f4b71Sopenharmony_ci } 2426e41f4b71Sopenharmony_ci ``` 2427e41f4b71Sopenharmony_ci 2428e41f4b71Sopenharmony_ci  2429e41f4b71Sopenharmony_ci 2430e41f4b71Sopenharmony_ci 2431e41f4b71Sopenharmony_ci### transform 2432e41f4b71Sopenharmony_ci 2433e41f4b71Sopenharmony_citransform(a: number, b: number, c: number, d: number, e: number, f: number): void 2434e41f4b71Sopenharmony_ci 2435e41f4b71Sopenharmony_citransform方法对应一个变换矩阵,想对一个图形进行变化的时候,只要设置此变换矩阵相应的参数,对图形的各个定点的坐标分别乘以这个矩阵,就能得到新的定点的坐标。矩阵变换效果可叠加。 2436e41f4b71Sopenharmony_ci 2437e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2438e41f4b71Sopenharmony_ci 2439e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2440e41f4b71Sopenharmony_ci 2441e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2442e41f4b71Sopenharmony_ci 2443e41f4b71Sopenharmony_ci> **说明:** 2444e41f4b71Sopenharmony_ci> 变换后的坐标计算方式(x和y为变换前坐标,x'和y'为变换后坐标): 2445e41f4b71Sopenharmony_ci> 2446e41f4b71Sopenharmony_ci> - x' = scaleX \* x + skewY \* y + translateX 2447e41f4b71Sopenharmony_ci> 2448e41f4b71Sopenharmony_ci> - y' = skewX \* x + scaleY \* y + translateY 2449e41f4b71Sopenharmony_ci 2450e41f4b71Sopenharmony_ci**参数:** 2451e41f4b71Sopenharmony_ci 2452e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2453e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------------- | 2454e41f4b71Sopenharmony_ci| a | number | 是 | scaleX: 指定水平缩放值。 | 2455e41f4b71Sopenharmony_ci| b | number | 是 | skewY: 指定垂直倾斜值。 | 2456e41f4b71Sopenharmony_ci| c | number | 是 | skewX: 指定水平倾斜值。 | 2457e41f4b71Sopenharmony_ci| d | number | 是 | scaleY: 指定垂直缩放值。 | 2458e41f4b71Sopenharmony_ci| e | number | 是 | translateX: 指定水平移动值。<br>默认单位:vp。 | 2459e41f4b71Sopenharmony_ci| f | number | 是 | translateY: 指定垂直移动值。<br>默认单位:vp。 | 2460e41f4b71Sopenharmony_ci 2461e41f4b71Sopenharmony_ci**示例:** 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_ci ```ts 2464e41f4b71Sopenharmony_ci // xxx.ets 2465e41f4b71Sopenharmony_ci @Entry 2466e41f4b71Sopenharmony_ci @Component 2467e41f4b71Sopenharmony_ci struct Transform { 2468e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2469e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2470e41f4b71Sopenharmony_ci 2471e41f4b71Sopenharmony_ci build() { 2472e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2473e41f4b71Sopenharmony_ci Canvas(this.context) 2474e41f4b71Sopenharmony_ci .width('100%') 2475e41f4b71Sopenharmony_ci .height('100%') 2476e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2477e41f4b71Sopenharmony_ci .onReady(() =>{ 2478e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,0)' 2479e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2480e41f4b71Sopenharmony_ci this.context.transform(1, 0.5, -0.5, 1, 10, 10) 2481e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 2482e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2483e41f4b71Sopenharmony_ci this.context.transform(1, 0.5, -0.5, 1, 10, 10) 2484e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 2485e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2486e41f4b71Sopenharmony_ci }) 2487e41f4b71Sopenharmony_ci } 2488e41f4b71Sopenharmony_ci .width('100%') 2489e41f4b71Sopenharmony_ci .height('100%') 2490e41f4b71Sopenharmony_ci } 2491e41f4b71Sopenharmony_ci } 2492e41f4b71Sopenharmony_ci ``` 2493e41f4b71Sopenharmony_ci 2494e41f4b71Sopenharmony_ci  2495e41f4b71Sopenharmony_ci 2496e41f4b71Sopenharmony_ci 2497e41f4b71Sopenharmony_ci### setTransform 2498e41f4b71Sopenharmony_ci 2499e41f4b71Sopenharmony_cisetTransform(a: number, b: number, c: number, d: number, e: number, f: number): void 2500e41f4b71Sopenharmony_ci 2501e41f4b71Sopenharmony_cisetTransform方法使用的参数和transform()方法相同,但setTransform()方法会重置现有的变换矩阵并创建新的变换矩阵。 2502e41f4b71Sopenharmony_ci 2503e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2504e41f4b71Sopenharmony_ci 2505e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2506e41f4b71Sopenharmony_ci 2507e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2508e41f4b71Sopenharmony_ci 2509e41f4b71Sopenharmony_ci**参数:** 2510e41f4b71Sopenharmony_ci 2511e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2512e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------------- | 2513e41f4b71Sopenharmony_ci| a | number | 是 | scaleX: 指定水平缩放值。 | 2514e41f4b71Sopenharmony_ci| b | number | 是 | skewY: 指定垂直倾斜值。 | 2515e41f4b71Sopenharmony_ci| c | number | 是 | skewX: 指定水平倾斜值。 | 2516e41f4b71Sopenharmony_ci| d | number | 是 | scaleY: 指定垂直缩放值。 | 2517e41f4b71Sopenharmony_ci| e | number | 是 | translateX: 指定水平移动值。<br>默认单位:vp。 | 2518e41f4b71Sopenharmony_ci| f | number | 是 | translateY: 指定垂直移动值。<br>默认单位:vp。 | 2519e41f4b71Sopenharmony_ci 2520e41f4b71Sopenharmony_ci**示例:** 2521e41f4b71Sopenharmony_ci 2522e41f4b71Sopenharmony_ci ```ts 2523e41f4b71Sopenharmony_ci // xxx.ets 2524e41f4b71Sopenharmony_ci @Entry 2525e41f4b71Sopenharmony_ci @Component 2526e41f4b71Sopenharmony_ci struct SetTransform { 2527e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2528e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2529e41f4b71Sopenharmony_ci 2530e41f4b71Sopenharmony_ci build() { 2531e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2532e41f4b71Sopenharmony_ci Canvas(this.context) 2533e41f4b71Sopenharmony_ci .width('100%') 2534e41f4b71Sopenharmony_ci .height('100%') 2535e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2536e41f4b71Sopenharmony_ci .onReady(() =>{ 2537e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(255,0,0)' 2538e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2539e41f4b71Sopenharmony_ci this.context.setTransform(1,0.5, -0.5, 1, 10, 10) 2540e41f4b71Sopenharmony_ci this.context.fillStyle = 'rgb(0,0,255)' 2541e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 2542e41f4b71Sopenharmony_ci }) 2543e41f4b71Sopenharmony_ci } 2544e41f4b71Sopenharmony_ci .width('100%') 2545e41f4b71Sopenharmony_ci .height('100%') 2546e41f4b71Sopenharmony_ci } 2547e41f4b71Sopenharmony_ci } 2548e41f4b71Sopenharmony_ci ``` 2549e41f4b71Sopenharmony_ci 2550e41f4b71Sopenharmony_ci  2551e41f4b71Sopenharmony_ci 2552e41f4b71Sopenharmony_ci### setTransform 2553e41f4b71Sopenharmony_ci 2554e41f4b71Sopenharmony_cisetTransform(transform?: Matrix2D): void 2555e41f4b71Sopenharmony_ci 2556e41f4b71Sopenharmony_ci以Matrix2D对象为模板重置现有的变换矩阵并创建新的变换矩阵。 2557e41f4b71Sopenharmony_ci 2558e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2559e41f4b71Sopenharmony_ci 2560e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2561e41f4b71Sopenharmony_ci 2562e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2563e41f4b71Sopenharmony_ci 2564e41f4b71Sopenharmony_ci**参数:** 2565e41f4b71Sopenharmony_ci 2566e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2567e41f4b71Sopenharmony_ci| --------- | ---------------------------------------- | ---- | ----- | 2568e41f4b71Sopenharmony_ci| transform | [Matrix2D](ts-components-canvas-matrix2d.md#Matrix2D) | 否 | 变换矩阵。<br>默认值:null。 | 2569e41f4b71Sopenharmony_ci 2570e41f4b71Sopenharmony_ci**示例:** 2571e41f4b71Sopenharmony_ci 2572e41f4b71Sopenharmony_ci ```ts 2573e41f4b71Sopenharmony_ci // xxx.ets 2574e41f4b71Sopenharmony_ci @Entry 2575e41f4b71Sopenharmony_ci @Component 2576e41f4b71Sopenharmony_ci struct TransFormDemo { 2577e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 2578e41f4b71Sopenharmony_ci private context1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this. settings); 2579e41f4b71Sopenharmony_ci private context2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 2580e41f4b71Sopenharmony_ci 2581e41f4b71Sopenharmony_ci build() { 2582e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2583e41f4b71Sopenharmony_ci Text('context1'); 2584e41f4b71Sopenharmony_ci Canvas(this.context1) 2585e41f4b71Sopenharmony_ci .width('230vp') 2586e41f4b71Sopenharmony_ci .height('160vp') 2587e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2588e41f4b71Sopenharmony_ci .onReady(() =>{ 2589e41f4b71Sopenharmony_ci this.context1.fillRect(100, 20, 50, 50); 2590e41f4b71Sopenharmony_ci this.context1.setTransform(1, 0.5, -0.5, 1, 10, 10); 2591e41f4b71Sopenharmony_ci this.context1.fillRect(100, 20, 50, 50); 2592e41f4b71Sopenharmony_ci }) 2593e41f4b71Sopenharmony_ci Text('context2'); 2594e41f4b71Sopenharmony_ci Canvas(this.context2) 2595e41f4b71Sopenharmony_ci .width('230vp') 2596e41f4b71Sopenharmony_ci .height('160vp') 2597e41f4b71Sopenharmony_ci .backgroundColor('#0ffff0') 2598e41f4b71Sopenharmony_ci .onReady(() =>{ 2599e41f4b71Sopenharmony_ci this.context2.fillRect(100, 20, 50, 50); 2600e41f4b71Sopenharmony_ci let storedTransform = this.context1.getTransform(); 2601e41f4b71Sopenharmony_ci this.context2.setTransform(storedTransform); 2602e41f4b71Sopenharmony_ci this.context2.fillRect(100, 20, 50, 50); 2603e41f4b71Sopenharmony_ci }) 2604e41f4b71Sopenharmony_ci } 2605e41f4b71Sopenharmony_ci .width('100%') 2606e41f4b71Sopenharmony_ci .height('100%') 2607e41f4b71Sopenharmony_ci } 2608e41f4b71Sopenharmony_ci } 2609e41f4b71Sopenharmony_ci ``` 2610e41f4b71Sopenharmony_ci 2611e41f4b71Sopenharmony_ci  2612e41f4b71Sopenharmony_ci 2613e41f4b71Sopenharmony_ci### getTransform 2614e41f4b71Sopenharmony_ci 2615e41f4b71Sopenharmony_cigetTransform(): Matrix2D 2616e41f4b71Sopenharmony_ci 2617e41f4b71Sopenharmony_ci获取当前被应用到上下文的转换矩阵。 2618e41f4b71Sopenharmony_ci 2619e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2620e41f4b71Sopenharmony_ci 2621e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2622e41f4b71Sopenharmony_ci 2623e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2624e41f4b71Sopenharmony_ci 2625e41f4b71Sopenharmony_ci**返回值:** 2626e41f4b71Sopenharmony_ci 2627e41f4b71Sopenharmony_ci| 类型 | 说明 | 2628e41f4b71Sopenharmony_ci| ---------------------------------------- | ----- | 2629e41f4b71Sopenharmony_ci| [Matrix2D](ts-components-canvas-matrix2d.md#Matrix2D) | 矩阵对象。 | 2630e41f4b71Sopenharmony_ci 2631e41f4b71Sopenharmony_ci**示例:** 2632e41f4b71Sopenharmony_ci 2633e41f4b71Sopenharmony_ci ```ts 2634e41f4b71Sopenharmony_ci // xxx.ets 2635e41f4b71Sopenharmony_ci @Entry 2636e41f4b71Sopenharmony_ci @Component 2637e41f4b71Sopenharmony_ci struct TransFormDemo { 2638e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 2639e41f4b71Sopenharmony_ci private context1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 2640e41f4b71Sopenharmony_ci private context2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 2641e41f4b71Sopenharmony_ci 2642e41f4b71Sopenharmony_ci build() { 2643e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2644e41f4b71Sopenharmony_ci Text('context1'); 2645e41f4b71Sopenharmony_ci Canvas(this.context1) 2646e41f4b71Sopenharmony_ci .width('230vp') 2647e41f4b71Sopenharmony_ci .height('120vp') 2648e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2649e41f4b71Sopenharmony_ci .onReady(() =>{ 2650e41f4b71Sopenharmony_ci this.context1.fillRect(50, 50, 50, 50); 2651e41f4b71Sopenharmony_ci this.context1.setTransform(1.2, Math.PI/8, Math.PI/6, 0.5, 30, -25); 2652e41f4b71Sopenharmony_ci this.context1.fillRect(50, 50, 50, 50); 2653e41f4b71Sopenharmony_ci }) 2654e41f4b71Sopenharmony_ci Text('context2'); 2655e41f4b71Sopenharmony_ci Canvas(this.context2) 2656e41f4b71Sopenharmony_ci .width('230vp') 2657e41f4b71Sopenharmony_ci .height('120vp') 2658e41f4b71Sopenharmony_ci .backgroundColor('#0ffff0') 2659e41f4b71Sopenharmony_ci .onReady(() =>{ 2660e41f4b71Sopenharmony_ci this.context2.fillRect(50, 50, 50, 50); 2661e41f4b71Sopenharmony_ci let storedTransform = this.context1.getTransform(); 2662e41f4b71Sopenharmony_ci console.log("Matrix [scaleX = " + storedTransform.scaleX + ", scaleY = " + storedTransform.scaleY + 2663e41f4b71Sopenharmony_ci ", rotateX = " + storedTransform.rotateX + ", rotateY = " + storedTransform.rotateY + 2664e41f4b71Sopenharmony_ci ", translateX = " + storedTransform.translateX + ", translateY = " + storedTransform.translateY + "]") 2665e41f4b71Sopenharmony_ci this.context2.setTransform(storedTransform); 2666e41f4b71Sopenharmony_ci this.context2.fillRect(50,50,50,50); 2667e41f4b71Sopenharmony_ci }) 2668e41f4b71Sopenharmony_ci } 2669e41f4b71Sopenharmony_ci .width('100%') 2670e41f4b71Sopenharmony_ci .height('100%') 2671e41f4b71Sopenharmony_ci } 2672e41f4b71Sopenharmony_ci } 2673e41f4b71Sopenharmony_ci ``` 2674e41f4b71Sopenharmony_ci 2675e41f4b71Sopenharmony_ci  2676e41f4b71Sopenharmony_ci 2677e41f4b71Sopenharmony_ci### translate 2678e41f4b71Sopenharmony_ci 2679e41f4b71Sopenharmony_citranslate(x: number, y: number): void 2680e41f4b71Sopenharmony_ci 2681e41f4b71Sopenharmony_ci移动当前坐标系的原点。 2682e41f4b71Sopenharmony_ci 2683e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2684e41f4b71Sopenharmony_ci 2685e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2686e41f4b71Sopenharmony_ci 2687e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2688e41f4b71Sopenharmony_ci 2689e41f4b71Sopenharmony_ci**参数:** 2690e41f4b71Sopenharmony_ci 2691e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2692e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------- | 2693e41f4b71Sopenharmony_ci| x | number | 是 | 设置水平平移量。<br>默认单位:vp。 | 2694e41f4b71Sopenharmony_ci| y | number | 是 | 设置竖直平移量。<br>默认单位:vp。 | 2695e41f4b71Sopenharmony_ci 2696e41f4b71Sopenharmony_ci**示例:** 2697e41f4b71Sopenharmony_ci 2698e41f4b71Sopenharmony_ci ```ts 2699e41f4b71Sopenharmony_ci // xxx.ets 2700e41f4b71Sopenharmony_ci @Entry 2701e41f4b71Sopenharmony_ci @Component 2702e41f4b71Sopenharmony_ci struct Translate { 2703e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2704e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2705e41f4b71Sopenharmony_ci 2706e41f4b71Sopenharmony_ci build() { 2707e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2708e41f4b71Sopenharmony_ci Canvas(this.context) 2709e41f4b71Sopenharmony_ci .width('100%') 2710e41f4b71Sopenharmony_ci .height('100%') 2711e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2712e41f4b71Sopenharmony_ci .onReady(() =>{ 2713e41f4b71Sopenharmony_ci this.context.fillRect(10, 10, 50, 50) 2714e41f4b71Sopenharmony_ci this.context.translate(70, 70) 2715e41f4b71Sopenharmony_ci this.context.fillRect(10, 10, 50, 50) 2716e41f4b71Sopenharmony_ci }) 2717e41f4b71Sopenharmony_ci } 2718e41f4b71Sopenharmony_ci .width('100%') 2719e41f4b71Sopenharmony_ci .height('100%') 2720e41f4b71Sopenharmony_ci } 2721e41f4b71Sopenharmony_ci } 2722e41f4b71Sopenharmony_ci ``` 2723e41f4b71Sopenharmony_ci 2724e41f4b71Sopenharmony_ci  2725e41f4b71Sopenharmony_ci 2726e41f4b71Sopenharmony_ci 2727e41f4b71Sopenharmony_ci### drawImage 2728e41f4b71Sopenharmony_ci 2729e41f4b71Sopenharmony_cidrawImage(image: ImageBitmap | PixelMap, dx: number, dy: number): void 2730e41f4b71Sopenharmony_ci 2731e41f4b71Sopenharmony_ci进行图像绘制。 2732e41f4b71Sopenharmony_ci 2733e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用,卡片中不支持PixelMap对象。 2734e41f4b71Sopenharmony_ci 2735e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2736e41f4b71Sopenharmony_ci 2737e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2738e41f4b71Sopenharmony_ci 2739e41f4b71Sopenharmony_ci**参数:** 2740e41f4b71Sopenharmony_ci 2741e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2742e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | ---- | ---------------------------------------- | 2743e41f4b71Sopenharmony_ci| image | [ImageBitmap](ts-components-canvas-imagebitmap.md)或[PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 是 | 图片资源,请参考ImageBitmap或PixelMap。 | 2744e41f4b71Sopenharmony_ci| dx | number | 是 | 绘制区域左上角在x轴的位置。<br>默认单位:vp。| 2745e41f4b71Sopenharmony_ci| dy | number | 是 | 绘制区域左上角在y轴的位置。<br>默认单位:vp。| 2746e41f4b71Sopenharmony_ci 2747e41f4b71Sopenharmony_cidrawImage(image: ImageBitmap | PixelMap, dx: number, dy: number, dw: number, dh: number): void 2748e41f4b71Sopenharmony_ci 2749e41f4b71Sopenharmony_ci进行图像绘制。 2750e41f4b71Sopenharmony_ci 2751e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用,卡片中不支持PixelMap对象。 2752e41f4b71Sopenharmony_ci 2753e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2754e41f4b71Sopenharmony_ci 2755e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2756e41f4b71Sopenharmony_ci 2757e41f4b71Sopenharmony_ci**参数:** 2758e41f4b71Sopenharmony_ci 2759e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2760e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | ---- | ---------------------------------------- | 2761e41f4b71Sopenharmony_ci| image | [ImageBitmap](ts-components-canvas-imagebitmap.md)或[PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 是 | 图片资源,请参考ImageBitmap或PixelMap。 | 2762e41f4b71Sopenharmony_ci| dx | number | 是 | 绘制区域左上角在x轴的位置。<br>默认单位:vp。| 2763e41f4b71Sopenharmony_ci| dy | number | 是 | 绘制区域左上角在y轴的位置。<br>默认单位:vp。| 2764e41f4b71Sopenharmony_ci| dw | number | 是 | 绘制区域的宽度。当绘制区域的宽度和裁剪图像的宽度不一致时,将图像宽度拉伸或压缩为绘制区域的宽度。<br>默认单位:vp。 | 2765e41f4b71Sopenharmony_ci| dh | number | 是 | 绘制区域的高度。当绘制区域的高度和裁剪图像的高度不一致时,将图像高度拉伸或压缩为绘制区域的高度。<br>默认单位:vp。 | 2766e41f4b71Sopenharmony_ci 2767e41f4b71Sopenharmony_cidrawImage(image: ImageBitmap | PixelMap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void 2768e41f4b71Sopenharmony_ci 2769e41f4b71Sopenharmony_ci进行图像绘制。 2770e41f4b71Sopenharmony_ci 2771e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用,卡片中不支持PixelMap对象。 2772e41f4b71Sopenharmony_ci 2773e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2774e41f4b71Sopenharmony_ci 2775e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2776e41f4b71Sopenharmony_ci 2777e41f4b71Sopenharmony_ci**参数:** 2778e41f4b71Sopenharmony_ci 2779e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2780e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | ---- | ---------------------------------------- | 2781e41f4b71Sopenharmony_ci| image | [ImageBitmap](ts-components-canvas-imagebitmap.md)或[PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 是 | 图片资源,请参考ImageBitmap或PixelMap。 | 2782e41f4b71Sopenharmony_ci| sx | number | 是 | 裁切源图像时距离源图像左上角的x坐标值。<br>image类型为ImageBitmap时,默认单位:vp。<br>image类型为PixelMap时,单位:px。 | 2783e41f4b71Sopenharmony_ci| sy | number | 是 | 裁切源图像时距离源图像左上角的y坐标值。<br>image类型为ImageBitmap时,默认单位:vp。<br>image类型为PixelMap时,单位:px。 | 2784e41f4b71Sopenharmony_ci| sw | number | 是 | 裁切源图像时需要裁切的宽度。<br>image类型为ImageBitmap时,默认单位:vp。<br>image类型为PixelMap时,单位:px。 | 2785e41f4b71Sopenharmony_ci| sh | number | 是 | 裁切源图像时需要裁切的高度。<br>image类型为ImageBitmap时,默认单位:vp。<br>image类型为PixelMap时,单位:px。 | 2786e41f4b71Sopenharmony_ci| dx | number | 是 | 绘制区域左上角在x轴的位置。<br>默认单位:vp。| 2787e41f4b71Sopenharmony_ci| dy | number | 是 | 绘制区域左上角在y轴的位置。<br>默认单位:vp。| 2788e41f4b71Sopenharmony_ci| dw | number | 是 | 绘制区域的宽度。当绘制区域的宽度和裁剪图像的宽度不一致时,将图像宽度拉伸或压缩为绘制区域的宽度。<br>默认单位:vp。 | 2789e41f4b71Sopenharmony_ci| dh | number | 是 | 绘制区域的高度。当绘制区域的高度和裁剪图像的高度不一致时,将图像高度拉伸或压缩为绘制区域的高度。<br>默认单位:vp。 | 2790e41f4b71Sopenharmony_ci 2791e41f4b71Sopenharmony_ci**示例:** 2792e41f4b71Sopenharmony_ci 2793e41f4b71Sopenharmony_ci ```ts 2794e41f4b71Sopenharmony_ci // xxx.ets 2795e41f4b71Sopenharmony_ci @Entry 2796e41f4b71Sopenharmony_ci @Component 2797e41f4b71Sopenharmony_ci struct ImageExample { 2798e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2799e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2800e41f4b71Sopenharmony_ci private img: ImageBitmap = new ImageBitmap("common/images/example.jpg") 2801e41f4b71Sopenharmony_ci 2802e41f4b71Sopenharmony_ci build() { 2803e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2804e41f4b71Sopenharmony_ci Canvas(this.context) 2805e41f4b71Sopenharmony_ci .width('100%') 2806e41f4b71Sopenharmony_ci .height('100%') 2807e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2808e41f4b71Sopenharmony_ci .onReady(() => { 2809e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 0, 0) 2810e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 0, 150, 300, 100) 2811e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 0, 0, 500, 500, 0, 300, 400, 200) 2812e41f4b71Sopenharmony_ci }) 2813e41f4b71Sopenharmony_ci } 2814e41f4b71Sopenharmony_ci .width('100%') 2815e41f4b71Sopenharmony_ci .height('100%') 2816e41f4b71Sopenharmony_ci } 2817e41f4b71Sopenharmony_ci } 2818e41f4b71Sopenharmony_ci ``` 2819e41f4b71Sopenharmony_ci 2820e41f4b71Sopenharmony_ci  2821e41f4b71Sopenharmony_ci 2822e41f4b71Sopenharmony_ci 2823e41f4b71Sopenharmony_ci### createImageData 2824e41f4b71Sopenharmony_ci 2825e41f4b71Sopenharmony_cicreateImageData(sw: number, sh: number): ImageData 2826e41f4b71Sopenharmony_ci 2827e41f4b71Sopenharmony_ci创建新的、空白的、指定大小的ImageData 对象,请参考[ImageData](ts-components-canvas-imagedata.md),该接口存在内存拷贝行为,高耗时,应避免频繁使用。createImageData示例同putImageData。 2828e41f4b71Sopenharmony_ci 2829e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2830e41f4b71Sopenharmony_ci 2831e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2832e41f4b71Sopenharmony_ci 2833e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2834e41f4b71Sopenharmony_ci 2835e41f4b71Sopenharmony_ci**参数:** 2836e41f4b71Sopenharmony_ci 2837e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2838e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------- | 2839e41f4b71Sopenharmony_ci| sw | number | 是 | ImageData的宽度。<br>默认单位:vp。 | 2840e41f4b71Sopenharmony_ci| sh | number | 是 | ImageData的高度。<br>默认单位:vp。 | 2841e41f4b71Sopenharmony_ci 2842e41f4b71Sopenharmony_ci 2843e41f4b71Sopenharmony_cicreateImageData(imageData: ImageData): ImageData 2844e41f4b71Sopenharmony_ci 2845e41f4b71Sopenharmony_ci根据一个现有的ImageData对象重新创建一个宽、高相同的ImageData对象(不会复制图像数据),请参考[ImageData](ts-components-canvas-imagedata.md),该接口存在内存拷贝行为,高耗时,应避免频繁使用。createImageData示例同putImageData。 2846e41f4b71Sopenharmony_ci 2847e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2848e41f4b71Sopenharmony_ci 2849e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2850e41f4b71Sopenharmony_ci 2851e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2852e41f4b71Sopenharmony_ci 2853e41f4b71Sopenharmony_ci**参数:** 2854e41f4b71Sopenharmony_ci 2855e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2856e41f4b71Sopenharmony_ci| --------- | ---------------------------------------- | ---- | ----------------- | 2857e41f4b71Sopenharmony_ci| imagedata | [ImageData](ts-components-canvas-imagedata.md) | 是 | 现有的ImageData对象。 | 2858e41f4b71Sopenharmony_ci 2859e41f4b71Sopenharmony_ci **返回值:** 2860e41f4b71Sopenharmony_ci 2861e41f4b71Sopenharmony_ci| 类型 | 说明 | 2862e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------- | 2863e41f4b71Sopenharmony_ci| [ImageData](ts-components-canvas-imagedata.md) | 新的ImageData对象。 | 2864e41f4b71Sopenharmony_ci 2865e41f4b71Sopenharmony_ci 2866e41f4b71Sopenharmony_ci### getPixelMap 2867e41f4b71Sopenharmony_ci 2868e41f4b71Sopenharmony_cigetPixelMap(sx: number, sy: number, sw: number, sh: number): PixelMap 2869e41f4b71Sopenharmony_ci 2870e41f4b71Sopenharmony_ci以当前canvas指定区域内的像素创建[PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7)对象,该接口存在内存拷贝行为,高耗时,应避免频繁使用。 2871e41f4b71Sopenharmony_ci 2872e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2873e41f4b71Sopenharmony_ci 2874e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2875e41f4b71Sopenharmony_ci 2876e41f4b71Sopenharmony_ci**参数:** 2877e41f4b71Sopenharmony_ci 2878e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2879e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------------- | 2880e41f4b71Sopenharmony_ci| sx | number | 是 | 需要输出的区域的左上角x坐标。<br>默认单位:vp。 | 2881e41f4b71Sopenharmony_ci| sy | number | 是 | 需要输出的区域的左上角y坐标。<br>默认单位:vp。 | 2882e41f4b71Sopenharmony_ci| sw | number | 是 | 需要输出的区域的宽度。<br>默认单位:vp。 | 2883e41f4b71Sopenharmony_ci| sh | number | 是 | 需要输出的区域的高度。<br>默认单位:vp。 | 2884e41f4b71Sopenharmony_ci 2885e41f4b71Sopenharmony_ci**返回值:** 2886e41f4b71Sopenharmony_ci 2887e41f4b71Sopenharmony_ci| 类型 | 说明 | 2888e41f4b71Sopenharmony_ci| ---------------------------------------- | ------------- | 2889e41f4b71Sopenharmony_ci| [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 新的PixelMap对象。 | 2890e41f4b71Sopenharmony_ci 2891e41f4b71Sopenharmony_ci**示例:** 2892e41f4b71Sopenharmony_ci 2893e41f4b71Sopenharmony_ci ```ts 2894e41f4b71Sopenharmony_ci // xxx.ets 2895e41f4b71Sopenharmony_ci @Entry 2896e41f4b71Sopenharmony_ci @Component 2897e41f4b71Sopenharmony_ci struct GetPixelMap { 2898e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2899e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2900e41f4b71Sopenharmony_ci private img: ImageBitmap = new ImageBitmap("common/images/example.jpg") 2901e41f4b71Sopenharmony_ci 2902e41f4b71Sopenharmony_ci build() { 2903e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2904e41f4b71Sopenharmony_ci Canvas(this.context) 2905e41f4b71Sopenharmony_ci .width('100%') 2906e41f4b71Sopenharmony_ci .height('100%') 2907e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2908e41f4b71Sopenharmony_ci .onReady(() => { 2909e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 100, 100, 130, 130) 2910e41f4b71Sopenharmony_ci let pixelmap = this.context.getPixelMap(150, 150, 130, 130) 2911e41f4b71Sopenharmony_ci this.context.setPixelMap(pixelmap) 2912e41f4b71Sopenharmony_ci }) 2913e41f4b71Sopenharmony_ci } 2914e41f4b71Sopenharmony_ci .width('100%') 2915e41f4b71Sopenharmony_ci .height('100%') 2916e41f4b71Sopenharmony_ci } 2917e41f4b71Sopenharmony_ci } 2918e41f4b71Sopenharmony_ci ``` 2919e41f4b71Sopenharmony_ci 2920e41f4b71Sopenharmony_ci  2921e41f4b71Sopenharmony_ci 2922e41f4b71Sopenharmony_ci### setPixelMap 2923e41f4b71Sopenharmony_ci 2924e41f4b71Sopenharmony_cisetPixelMap(value?: PixelMap): void 2925e41f4b71Sopenharmony_ci 2926e41f4b71Sopenharmony_ci将当前传入[PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7)对象绘制在画布上。setPixelMap示例同getPixelMap。 2927e41f4b71Sopenharmony_ci 2928e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2929e41f4b71Sopenharmony_ci 2930e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2931e41f4b71Sopenharmony_ci 2932e41f4b71Sopenharmony_ci **参数:** 2933e41f4b71Sopenharmony_ci 2934e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2935e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------------- | 2936e41f4b71Sopenharmony_ci| value | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 否 | 包含像素值的PixelMap对象<br>默认值:null。 | 2937e41f4b71Sopenharmony_ci 2938e41f4b71Sopenharmony_ci### getImageData 2939e41f4b71Sopenharmony_ci 2940e41f4b71Sopenharmony_cigetImageData(sx: number, sy: number, sw: number, sh: number): ImageData 2941e41f4b71Sopenharmony_ci 2942e41f4b71Sopenharmony_ci以当前canvas指定区域内的像素创建[ImageData](ts-components-canvas-imagedata.md)对象,该接口存在内存拷贝行为,高耗时,应避免频繁使用。 2943e41f4b71Sopenharmony_ci 2944e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 2945e41f4b71Sopenharmony_ci 2946e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2947e41f4b71Sopenharmony_ci 2948e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 2949e41f4b71Sopenharmony_ci 2950e41f4b71Sopenharmony_ci**参数:** 2951e41f4b71Sopenharmony_ci 2952e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2953e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------------- | 2954e41f4b71Sopenharmony_ci| sx | number | 是 | 需要输出的区域的左上角x坐标。<br>默认单位:vp。 | 2955e41f4b71Sopenharmony_ci| sy | number | 是 | 需要输出的区域的左上角y坐标。<br>默认单位:vp。 | 2956e41f4b71Sopenharmony_ci| sw | number | 是 | 需要输出的区域的宽度。<br>默认单位:vp。 | 2957e41f4b71Sopenharmony_ci| sh | number | 是 | 需要输出的区域的高度。<br>默认单位:vp。 | 2958e41f4b71Sopenharmony_ci 2959e41f4b71Sopenharmony_ci **返回值:** 2960e41f4b71Sopenharmony_ci 2961e41f4b71Sopenharmony_ci| 类型 | 说明 | 2962e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------- | 2963e41f4b71Sopenharmony_ci| [ImageData](ts-components-canvas-imagedata.md) | 新的ImageData对象。 | 2964e41f4b71Sopenharmony_ci 2965e41f4b71Sopenharmony_ci 2966e41f4b71Sopenharmony_ci**示例:** 2967e41f4b71Sopenharmony_ci 2968e41f4b71Sopenharmony_ci ```ts 2969e41f4b71Sopenharmony_ci // xxx.ets 2970e41f4b71Sopenharmony_ci @Entry 2971e41f4b71Sopenharmony_ci @Component 2972e41f4b71Sopenharmony_ci struct GetImageData { 2973e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 2974e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 2975e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("/common/images/1234.png") 2976e41f4b71Sopenharmony_ci 2977e41f4b71Sopenharmony_ci build() { 2978e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 2979e41f4b71Sopenharmony_ci Canvas(this.context) 2980e41f4b71Sopenharmony_ci .width('100%') 2981e41f4b71Sopenharmony_ci .height('100%') 2982e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 2983e41f4b71Sopenharmony_ci .onReady(() =>{ 2984e41f4b71Sopenharmony_ci this.context.drawImage(this.img,0,0,130,130) 2985e41f4b71Sopenharmony_ci let imagedata = this.context.getImageData(50,50,130,130) 2986e41f4b71Sopenharmony_ci this.context.putImageData(imagedata,150,150) 2987e41f4b71Sopenharmony_ci }) 2988e41f4b71Sopenharmony_ci } 2989e41f4b71Sopenharmony_ci .width('100%') 2990e41f4b71Sopenharmony_ci .height('100%') 2991e41f4b71Sopenharmony_ci } 2992e41f4b71Sopenharmony_ci } 2993e41f4b71Sopenharmony_ci ``` 2994e41f4b71Sopenharmony_ci 2995e41f4b71Sopenharmony_ci  2996e41f4b71Sopenharmony_ci 2997e41f4b71Sopenharmony_ci 2998e41f4b71Sopenharmony_ci### putImageData 2999e41f4b71Sopenharmony_ci 3000e41f4b71Sopenharmony_ciputImageData(imageData: ImageData, dx: number | string, dy: number | string): void 3001e41f4b71Sopenharmony_ci 3002e41f4b71Sopenharmony_ci使用[ImageData](ts-components-canvas-imagedata.md)数据填充新的矩形区域。 3003e41f4b71Sopenharmony_ci 3004e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3005e41f4b71Sopenharmony_ci 3006e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3007e41f4b71Sopenharmony_ci 3008e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3009e41f4b71Sopenharmony_ci 3010e41f4b71Sopenharmony_ci**参数:** 3011e41f4b71Sopenharmony_ci 3012e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3013e41f4b71Sopenharmony_ci| ----------- | ---------------------------------------- | ---- | ----------------------------- | 3014e41f4b71Sopenharmony_ci| imagedata | [ImageData](ts-components-canvas-imagedata.md) | 是 | 包含像素值的ImageData对象。 | 3015e41f4b71Sopenharmony_ci| dx | number \| string<sup>10+</sup> | 是 | 填充区域在x轴方向的偏移量。<br>默认单位:vp。 | 3016e41f4b71Sopenharmony_ci| dy | number \| string<sup>10+</sup> | 是 | 填充区域在y轴方向的偏移量。<br>默认单位:vp。 | 3017e41f4b71Sopenharmony_ci 3018e41f4b71Sopenharmony_ciputImageData(imageData: ImageData, dx: number | string, dy: number | string, dirtyX: number | string, dirtyY: number | string, dirtyWidth: number | string, dirtyHeight: number | string): void 3019e41f4b71Sopenharmony_ci 3020e41f4b71Sopenharmony_ci使用[ImageData](ts-components-canvas-imagedata.md)数据填充新的矩形区域。 3021e41f4b71Sopenharmony_ci 3022e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3023e41f4b71Sopenharmony_ci 3024e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3025e41f4b71Sopenharmony_ci 3026e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3027e41f4b71Sopenharmony_ci 3028e41f4b71Sopenharmony_ci**参数:** 3029e41f4b71Sopenharmony_ci 3030e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3031e41f4b71Sopenharmony_ci| ----------- | ---------------------------------------- | ---- | ----------------------------- | 3032e41f4b71Sopenharmony_ci| imagedata | [ImageData](ts-components-canvas-imagedata.md) | 是 | 包含像素值的ImageData对象。 | 3033e41f4b71Sopenharmony_ci| dx | number \| string<sup>10+</sup> | 是 | 填充区域在x轴方向的偏移量。<br>默认单位:vp。 | 3034e41f4b71Sopenharmony_ci| dy | number \| string<sup>10+</sup> | 是 | 填充区域在y轴方向的偏移量。<br>默认单位:vp。 | 3035e41f4b71Sopenharmony_ci| dirtyX | number \| string<sup>10+</sup> | 是 | 源图像数据矩形裁切范围左上角距离源图像左上角的x轴偏移量。<br>默认单位:vp。 | 3036e41f4b71Sopenharmony_ci| dirtyY | number \| string<sup>10+</sup> | 是 | 源图像数据矩形裁切范围左上角距离源图像左上角的y轴偏移量。<br>默认单位:vp。 | 3037e41f4b71Sopenharmony_ci| dirtyWidth | number \| string<sup>10+</sup> | 是 | 源图像数据矩形裁切范围的宽度。<br>默认单位:vp。 | 3038e41f4b71Sopenharmony_ci| dirtyHeight | number \| string<sup>10+</sup> | 是 | 源图像数据矩形裁切范围的高度。<br>默认单位:vp。 | 3039e41f4b71Sopenharmony_ci 3040e41f4b71Sopenharmony_ci**示例:** 3041e41f4b71Sopenharmony_ci 3042e41f4b71Sopenharmony_ci ```ts 3043e41f4b71Sopenharmony_ci // xxx.ets 3044e41f4b71Sopenharmony_ci @Entry 3045e41f4b71Sopenharmony_ci @Component 3046e41f4b71Sopenharmony_ci struct PutImageData { 3047e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3048e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3049e41f4b71Sopenharmony_ci 3050e41f4b71Sopenharmony_ci build() { 3051e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3052e41f4b71Sopenharmony_ci Canvas(this.context) 3053e41f4b71Sopenharmony_ci .width('100%') 3054e41f4b71Sopenharmony_ci .height('100%') 3055e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3056e41f4b71Sopenharmony_ci .onReady(() => { 3057e41f4b71Sopenharmony_ci let imageDataNum = this.context.createImageData(100, 100) 3058e41f4b71Sopenharmony_ci let imageData = this.context.createImageData(imageDataNum) 3059e41f4b71Sopenharmony_ci for (let i = 0; i < imageData.data.length; i += 4) { 3060e41f4b71Sopenharmony_ci imageData.data[i + 0] = 255 3061e41f4b71Sopenharmony_ci imageData.data[i + 1] = 0 3062e41f4b71Sopenharmony_ci imageData.data[i + 2] = 255 3063e41f4b71Sopenharmony_ci imageData.data[i + 3] = 255 3064e41f4b71Sopenharmony_ci } 3065e41f4b71Sopenharmony_ci this.context.putImageData(imageData, 10, 10) 3066e41f4b71Sopenharmony_ci this.context.putImageData(imageData, 150, 10, 0, 0, 50, 50) 3067e41f4b71Sopenharmony_ci }) 3068e41f4b71Sopenharmony_ci } 3069e41f4b71Sopenharmony_ci .width('100%') 3070e41f4b71Sopenharmony_ci .height('100%') 3071e41f4b71Sopenharmony_ci } 3072e41f4b71Sopenharmony_ci } 3073e41f4b71Sopenharmony_ci ``` 3074e41f4b71Sopenharmony_ci 3075e41f4b71Sopenharmony_ci  3076e41f4b71Sopenharmony_ci 3077e41f4b71Sopenharmony_ci 3078e41f4b71Sopenharmony_ci### setLineDash 3079e41f4b71Sopenharmony_ci 3080e41f4b71Sopenharmony_cisetLineDash(segments: number[]): void 3081e41f4b71Sopenharmony_ci 3082e41f4b71Sopenharmony_ci设置画布的虚线样式。 3083e41f4b71Sopenharmony_ci 3084e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3085e41f4b71Sopenharmony_ci 3086e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3087e41f4b71Sopenharmony_ci 3088e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3089e41f4b71Sopenharmony_ci 3090e41f4b71Sopenharmony_ci**参数:** 3091e41f4b71Sopenharmony_ci 3092e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3093e41f4b71Sopenharmony_ci| -------- | -------- | ------- | ------------ | 3094e41f4b71Sopenharmony_ci| segments | number[] | 是 | 描述线段如何交替和线段间距长度的数组。<br>默认单位:vp。 | 3095e41f4b71Sopenharmony_ci 3096e41f4b71Sopenharmony_ci**示例:** 3097e41f4b71Sopenharmony_ci 3098e41f4b71Sopenharmony_ci ```ts 3099e41f4b71Sopenharmony_ci // xxx.ets 3100e41f4b71Sopenharmony_ci @Entry 3101e41f4b71Sopenharmony_ci @Component 3102e41f4b71Sopenharmony_ci struct SetLineDash { 3103e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3104e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3105e41f4b71Sopenharmony_ci 3106e41f4b71Sopenharmony_ci build() { 3107e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3108e41f4b71Sopenharmony_ci Canvas(this.context) 3109e41f4b71Sopenharmony_ci .width('100%') 3110e41f4b71Sopenharmony_ci .height('100%') 3111e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3112e41f4b71Sopenharmony_ci .onReady(() =>{ 3113e41f4b71Sopenharmony_ci this.context.arc(100, 75, 50, 0, 6.28) 3114e41f4b71Sopenharmony_ci this.context.setLineDash([10,20]) 3115e41f4b71Sopenharmony_ci this.context.stroke() 3116e41f4b71Sopenharmony_ci }) 3117e41f4b71Sopenharmony_ci } 3118e41f4b71Sopenharmony_ci .width('100%') 3119e41f4b71Sopenharmony_ci .height('100%') 3120e41f4b71Sopenharmony_ci } 3121e41f4b71Sopenharmony_ci } 3122e41f4b71Sopenharmony_ci ``` 3123e41f4b71Sopenharmony_ci 3124e41f4b71Sopenharmony_ci  3125e41f4b71Sopenharmony_ci 3126e41f4b71Sopenharmony_ci 3127e41f4b71Sopenharmony_ci### getLineDash 3128e41f4b71Sopenharmony_ci 3129e41f4b71Sopenharmony_cigetLineDash(): number[] 3130e41f4b71Sopenharmony_ci 3131e41f4b71Sopenharmony_ci获得当前画布的虚线样式。 3132e41f4b71Sopenharmony_ci 3133e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3134e41f4b71Sopenharmony_ci 3135e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3136e41f4b71Sopenharmony_ci 3137e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3138e41f4b71Sopenharmony_ci 3139e41f4b71Sopenharmony_ci**返回值:** 3140e41f4b71Sopenharmony_ci 3141e41f4b71Sopenharmony_ci| 类型 | 说明 | 3142e41f4b71Sopenharmony_ci| -------- | ------------------------ | 3143e41f4b71Sopenharmony_ci| number[] | 返回数组,该数组用来描述线段如何交替和间距长度。<br>默认单位:vp。 | 3144e41f4b71Sopenharmony_ci 3145e41f4b71Sopenharmony_ci 3146e41f4b71Sopenharmony_ci**示例:** 3147e41f4b71Sopenharmony_ci 3148e41f4b71Sopenharmony_ci ```ts 3149e41f4b71Sopenharmony_ci // xxx.ets 3150e41f4b71Sopenharmony_ci @Entry 3151e41f4b71Sopenharmony_ci @Component 3152e41f4b71Sopenharmony_ci struct CanvasGetLineDash { 3153e41f4b71Sopenharmony_ci @State message: string = 'Hello World' 3154e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3155e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3156e41f4b71Sopenharmony_ci 3157e41f4b71Sopenharmony_ci build() { 3158e41f4b71Sopenharmony_ci Row() { 3159e41f4b71Sopenharmony_ci Column() { 3160e41f4b71Sopenharmony_ci Text(this.message) 3161e41f4b71Sopenharmony_ci .fontSize(50) 3162e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 3163e41f4b71Sopenharmony_ci .onClick(()=>{ 3164e41f4b71Sopenharmony_ci console.error('before getlinedash clicked') 3165e41f4b71Sopenharmony_ci let res = this.context.getLineDash() 3166e41f4b71Sopenharmony_ci console.error(JSON.stringify(res)) 3167e41f4b71Sopenharmony_ci }) 3168e41f4b71Sopenharmony_ci Canvas(this.context) 3169e41f4b71Sopenharmony_ci .width('100%') 3170e41f4b71Sopenharmony_ci .height('100%') 3171e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3172e41f4b71Sopenharmony_ci .onReady(() => { 3173e41f4b71Sopenharmony_ci this.context.arc(100, 75, 50, 0, 6.28) 3174e41f4b71Sopenharmony_ci this.context.setLineDash([10,20]) 3175e41f4b71Sopenharmony_ci this.context.stroke() 3176e41f4b71Sopenharmony_ci }) 3177e41f4b71Sopenharmony_ci } 3178e41f4b71Sopenharmony_ci .width('100%') 3179e41f4b71Sopenharmony_ci } 3180e41f4b71Sopenharmony_ci .height('100%') 3181e41f4b71Sopenharmony_ci } 3182e41f4b71Sopenharmony_ci } 3183e41f4b71Sopenharmony_ci ``` 3184e41f4b71Sopenharmony_ci 3185e41f4b71Sopenharmony_ci 3186e41f4b71Sopenharmony_ci 3187e41f4b71Sopenharmony_ci### transferFromImageBitmap 3188e41f4b71Sopenharmony_ci 3189e41f4b71Sopenharmony_citransferFromImageBitmap(bitmap: ImageBitmap): void 3190e41f4b71Sopenharmony_ci 3191e41f4b71Sopenharmony_ci显示给定的ImageBitmap对象。 3192e41f4b71Sopenharmony_ci 3193e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3194e41f4b71Sopenharmony_ci 3195e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3196e41f4b71Sopenharmony_ci 3197e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3198e41f4b71Sopenharmony_ci 3199e41f4b71Sopenharmony_ci**参数:** 3200e41f4b71Sopenharmony_ci 3201e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3202e41f4b71Sopenharmony_ci| ------ | ----------------------- | ----------------- | ------------------ | 3203e41f4b71Sopenharmony_ci| bitmap | [ImageBitmap](ts-components-canvas-imagebitmap.md) | 是 | 待显示的ImageBitmap对象。 | 3204e41f4b71Sopenharmony_ci 3205e41f4b71Sopenharmony_ci**示例:** 3206e41f4b71Sopenharmony_ci 3207e41f4b71Sopenharmony_ci ```ts 3208e41f4b71Sopenharmony_ci // xxx.ets 3209e41f4b71Sopenharmony_ci @Entry 3210e41f4b71Sopenharmony_ci @Component 3211e41f4b71Sopenharmony_ci struct TransferFromImageBitmap { 3212e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3213e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3214e41f4b71Sopenharmony_ci private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings) 3215e41f4b71Sopenharmony_ci 3216e41f4b71Sopenharmony_ci build() { 3217e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3218e41f4b71Sopenharmony_ci Canvas(this.context) 3219e41f4b71Sopenharmony_ci .width('100%') 3220e41f4b71Sopenharmony_ci .height('100%') 3221e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3222e41f4b71Sopenharmony_ci .onReady(() =>{ 3223e41f4b71Sopenharmony_ci let imageData = this.offContext.createImageData(100, 100) 3224e41f4b71Sopenharmony_ci for (let i = 0; i < imageData.data.length; i += 4) { 3225e41f4b71Sopenharmony_ci imageData.data[i + 0] = 255 3226e41f4b71Sopenharmony_ci imageData.data[i + 1] = 0 3227e41f4b71Sopenharmony_ci imageData.data[i + 2] = 255 3228e41f4b71Sopenharmony_ci imageData.data[i + 3] = 255 3229e41f4b71Sopenharmony_ci } 3230e41f4b71Sopenharmony_ci this.offContext.putImageData(imageData, 10, 10) 3231e41f4b71Sopenharmony_ci let image = this.offContext.transferToImageBitmap() 3232e41f4b71Sopenharmony_ci this.context.transferFromImageBitmap(image) 3233e41f4b71Sopenharmony_ci }) 3234e41f4b71Sopenharmony_ci } 3235e41f4b71Sopenharmony_ci .width('100%') 3236e41f4b71Sopenharmony_ci .height('100%') 3237e41f4b71Sopenharmony_ci } 3238e41f4b71Sopenharmony_ci } 3239e41f4b71Sopenharmony_ci ``` 3240e41f4b71Sopenharmony_ci  3241e41f4b71Sopenharmony_ci 3242e41f4b71Sopenharmony_ci 3243e41f4b71Sopenharmony_ci### toDataURL 3244e41f4b71Sopenharmony_ci 3245e41f4b71Sopenharmony_citoDataURL(type?: string, quality?: any): string 3246e41f4b71Sopenharmony_ci 3247e41f4b71Sopenharmony_ci生成一个包含图片展示的URL,该接口存在内存拷贝行为,高耗时,应避免频繁使用。 3248e41f4b71Sopenharmony_ci 3249e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3250e41f4b71Sopenharmony_ci 3251e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3252e41f4b71Sopenharmony_ci 3253e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3254e41f4b71Sopenharmony_ci 3255e41f4b71Sopenharmony_ci**参数:** 3256e41f4b71Sopenharmony_ci 3257e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3258e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------------------- | 3259e41f4b71Sopenharmony_ci| type | string | 否 | 用于指定图像格式。<br/>可选参数为:"image/png", "image/jpeg", "image/webp"。。<br>默认值:image/png。 | 3260e41f4b71Sopenharmony_ci| quality | any | 否 | 在指定图片格式为image/jpeg或image/webp的情况下,可以从0到1的区间内选择图片的质量。如果超出取值范围,将会使用默认值0.92。<br>默认值:0.92。 | 3261e41f4b71Sopenharmony_ci 3262e41f4b71Sopenharmony_ci**返回值:** 3263e41f4b71Sopenharmony_ci 3264e41f4b71Sopenharmony_ci| 类型 | 说明 | 3265e41f4b71Sopenharmony_ci| ------ | --------- | 3266e41f4b71Sopenharmony_ci| string | 图像的URL地址。 | 3267e41f4b71Sopenharmony_ci 3268e41f4b71Sopenharmony_ci**示例:** 3269e41f4b71Sopenharmony_ci 3270e41f4b71Sopenharmony_ci ```ts 3271e41f4b71Sopenharmony_ci // xxx.ets 3272e41f4b71Sopenharmony_ci @Entry 3273e41f4b71Sopenharmony_ci @Component 3274e41f4b71Sopenharmony_ci struct CanvasExample { 3275e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3276e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3277e41f4b71Sopenharmony_ci @State toDataURL: string = "" 3278e41f4b71Sopenharmony_ci 3279e41f4b71Sopenharmony_ci build() { 3280e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3281e41f4b71Sopenharmony_ci Canvas(this.context) 3282e41f4b71Sopenharmony_ci .width(100) 3283e41f4b71Sopenharmony_ci .height(100) 3284e41f4b71Sopenharmony_ci .onReady(() =>{ 3285e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ff00" 3286e41f4b71Sopenharmony_ci this.context.fillRect(0,0,100,100) 3287e41f4b71Sopenharmony_ci this.toDataURL = this.context.toDataURL("image/png", 0.92) 3288e41f4b71Sopenharmony_ci }) 3289e41f4b71Sopenharmony_ci Text(this.toDataURL) 3290e41f4b71Sopenharmony_ci } 3291e41f4b71Sopenharmony_ci .width('100%') 3292e41f4b71Sopenharmony_ci .height('100%') 3293e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3294e41f4b71Sopenharmony_ci } 3295e41f4b71Sopenharmony_ci } 3296e41f4b71Sopenharmony_ci ``` 3297e41f4b71Sopenharmony_ci  3298e41f4b71Sopenharmony_ci 3299e41f4b71Sopenharmony_ci 3300e41f4b71Sopenharmony_ci### restore 3301e41f4b71Sopenharmony_ci 3302e41f4b71Sopenharmony_cirestore(): void 3303e41f4b71Sopenharmony_ci 3304e41f4b71Sopenharmony_ci对保存的绘图上下文进行恢复。 3305e41f4b71Sopenharmony_ci 3306e41f4b71Sopenharmony_ci> **说明:** 3307e41f4b71Sopenharmony_ci> 3308e41f4b71Sopenharmony_ci> 当restore()次数未超出save()次数时,从栈中弹出存储的绘制状态并恢复CanvasRenderingContext2D对象的属性、剪切路径和变换矩阵的值。</br> 3309e41f4b71Sopenharmony_ci> 当restore()次数超出save()次数时,此方法不做任何改变。</br> 3310e41f4b71Sopenharmony_ci> 当没有保存状态时,此方法不做任何改变。 3311e41f4b71Sopenharmony_ci 3312e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3313e41f4b71Sopenharmony_ci 3314e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3315e41f4b71Sopenharmony_ci 3316e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3317e41f4b71Sopenharmony_ci 3318e41f4b71Sopenharmony_ci**示例:** 3319e41f4b71Sopenharmony_ci 3320e41f4b71Sopenharmony_ci ```ts 3321e41f4b71Sopenharmony_ci // xxx.ets 3322e41f4b71Sopenharmony_ci @Entry 3323e41f4b71Sopenharmony_ci @Component 3324e41f4b71Sopenharmony_ci struct CanvasExample { 3325e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3326e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3327e41f4b71Sopenharmony_ci 3328e41f4b71Sopenharmony_ci build() { 3329e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3330e41f4b71Sopenharmony_ci Canvas(this.context) 3331e41f4b71Sopenharmony_ci .width('100%') 3332e41f4b71Sopenharmony_ci .height('100%') 3333e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3334e41f4b71Sopenharmony_ci .onReady(() =>{ 3335e41f4b71Sopenharmony_ci this.context.save() // save the default state 3336e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ff00" 3337e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 100, 100) 3338e41f4b71Sopenharmony_ci this.context.restore() // restore to the default state 3339e41f4b71Sopenharmony_ci this.context.fillRect(150, 75, 100, 100) 3340e41f4b71Sopenharmony_ci }) 3341e41f4b71Sopenharmony_ci } 3342e41f4b71Sopenharmony_ci .width('100%') 3343e41f4b71Sopenharmony_ci .height('100%') 3344e41f4b71Sopenharmony_ci } 3345e41f4b71Sopenharmony_ci } 3346e41f4b71Sopenharmony_ci ``` 3347e41f4b71Sopenharmony_ci  3348e41f4b71Sopenharmony_ci 3349e41f4b71Sopenharmony_ci 3350e41f4b71Sopenharmony_ci### save 3351e41f4b71Sopenharmony_ci 3352e41f4b71Sopenharmony_cisave(): void 3353e41f4b71Sopenharmony_ci 3354e41f4b71Sopenharmony_ci将当前状态放入栈中,保存canvas的全部状态,通常在需要保存绘制状态时调用。 3355e41f4b71Sopenharmony_ci 3356e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3357e41f4b71Sopenharmony_ci 3358e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3359e41f4b71Sopenharmony_ci 3360e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3361e41f4b71Sopenharmony_ci 3362e41f4b71Sopenharmony_ci**示例:** 3363e41f4b71Sopenharmony_ci 3364e41f4b71Sopenharmony_ci ```ts 3365e41f4b71Sopenharmony_ci // xxx.ets 3366e41f4b71Sopenharmony_ci @Entry 3367e41f4b71Sopenharmony_ci @Component 3368e41f4b71Sopenharmony_ci struct CanvasExample { 3369e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3370e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3371e41f4b71Sopenharmony_ci 3372e41f4b71Sopenharmony_ci build() { 3373e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3374e41f4b71Sopenharmony_ci Canvas(this.context) 3375e41f4b71Sopenharmony_ci .width('100%') 3376e41f4b71Sopenharmony_ci .height('100%') 3377e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3378e41f4b71Sopenharmony_ci .onReady(() =>{ 3379e41f4b71Sopenharmony_ci this.context.save() // save the default state 3380e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ff00" 3381e41f4b71Sopenharmony_ci this.context.fillRect(20, 20, 100, 100) 3382e41f4b71Sopenharmony_ci this.context.restore() // restore to the default state 3383e41f4b71Sopenharmony_ci this.context.fillRect(150, 75, 100, 100) 3384e41f4b71Sopenharmony_ci }) 3385e41f4b71Sopenharmony_ci } 3386e41f4b71Sopenharmony_ci .width('100%') 3387e41f4b71Sopenharmony_ci .height('100%') 3388e41f4b71Sopenharmony_ci } 3389e41f4b71Sopenharmony_ci } 3390e41f4b71Sopenharmony_ci ``` 3391e41f4b71Sopenharmony_ci  3392e41f4b71Sopenharmony_ci 3393e41f4b71Sopenharmony_ci 3394e41f4b71Sopenharmony_ci### createLinearGradient 3395e41f4b71Sopenharmony_ci 3396e41f4b71Sopenharmony_cicreateLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradient 3397e41f4b71Sopenharmony_ci 3398e41f4b71Sopenharmony_ci创建一个线性渐变色。 3399e41f4b71Sopenharmony_ci 3400e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3401e41f4b71Sopenharmony_ci 3402e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3403e41f4b71Sopenharmony_ci 3404e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3405e41f4b71Sopenharmony_ci 3406e41f4b71Sopenharmony_ci**参数:** 3407e41f4b71Sopenharmony_ci 3408e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3409e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------- | 3410e41f4b71Sopenharmony_ci| x0 | number | 是 | 起点的x轴坐标。<br>默认单位:vp。 | 3411e41f4b71Sopenharmony_ci| y0 | number | 是 | 起点的y轴坐标。<br>默认单位:vp。 | 3412e41f4b71Sopenharmony_ci| x1 | number | 是 | 终点的x轴坐标。<br>默认单位:vp。 | 3413e41f4b71Sopenharmony_ci| y1 | number | 是 | 终点的y轴坐标。<br>默认单位:vp。 | 3414e41f4b71Sopenharmony_ci 3415e41f4b71Sopenharmony_ci**返回值:** 3416e41f4b71Sopenharmony_ci 3417e41f4b71Sopenharmony_ci| 类型 | 说明 | 3418e41f4b71Sopenharmony_ci| ------ | --------- | 3419e41f4b71Sopenharmony_ci| [CanvasGradient](ts-components-canvas-canvasgradient.md) | 新的CanvasGradient对象,用于在canvas上创建渐变效果。 | 3420e41f4b71Sopenharmony_ci 3421e41f4b71Sopenharmony_ci**示例:** 3422e41f4b71Sopenharmony_ci 3423e41f4b71Sopenharmony_ci ```ts 3424e41f4b71Sopenharmony_ci // xxx.ets 3425e41f4b71Sopenharmony_ci @Entry 3426e41f4b71Sopenharmony_ci @Component 3427e41f4b71Sopenharmony_ci struct CreateLinearGradient { 3428e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3429e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3430e41f4b71Sopenharmony_ci 3431e41f4b71Sopenharmony_ci build() { 3432e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3433e41f4b71Sopenharmony_ci Canvas(this.context) 3434e41f4b71Sopenharmony_ci .width('100%') 3435e41f4b71Sopenharmony_ci .height('100%') 3436e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3437e41f4b71Sopenharmony_ci .onReady(() =>{ 3438e41f4b71Sopenharmony_ci let grad = this.context.createLinearGradient(50,0, 300,100) 3439e41f4b71Sopenharmony_ci grad.addColorStop(0.0, '#ff0000') 3440e41f4b71Sopenharmony_ci grad.addColorStop(0.5, '#ffffff') 3441e41f4b71Sopenharmony_ci grad.addColorStop(1.0, '#00ff00') 3442e41f4b71Sopenharmony_ci this.context.fillStyle = grad 3443e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 400, 400) 3444e41f4b71Sopenharmony_ci }) 3445e41f4b71Sopenharmony_ci } 3446e41f4b71Sopenharmony_ci .width('100%') 3447e41f4b71Sopenharmony_ci .height('100%') 3448e41f4b71Sopenharmony_ci } 3449e41f4b71Sopenharmony_ci } 3450e41f4b71Sopenharmony_ci ``` 3451e41f4b71Sopenharmony_ci 3452e41f4b71Sopenharmony_ci  3453e41f4b71Sopenharmony_ci 3454e41f4b71Sopenharmony_ci 3455e41f4b71Sopenharmony_ci### createRadialGradient 3456e41f4b71Sopenharmony_ci 3457e41f4b71Sopenharmony_cicreateRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradient 3458e41f4b71Sopenharmony_ci 3459e41f4b71Sopenharmony_ci创建一个径向渐变色。 3460e41f4b71Sopenharmony_ci 3461e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3462e41f4b71Sopenharmony_ci 3463e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3464e41f4b71Sopenharmony_ci 3465e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3466e41f4b71Sopenharmony_ci 3467e41f4b71Sopenharmony_ci**参数:** 3468e41f4b71Sopenharmony_ci 3469e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3470e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----------------- | 3471e41f4b71Sopenharmony_ci| x0 | number | 是 | 起始圆的x轴坐标。<br>默认单位:vp。 | 3472e41f4b71Sopenharmony_ci| y0 | number | 是 | 起始圆的y轴坐标。<br>默认单位:vp。 | 3473e41f4b71Sopenharmony_ci| r0 | number | 是 | 起始圆的半径。必须是非负且有限的。<br>默认单位:vp。 | 3474e41f4b71Sopenharmony_ci| x1 | number | 是 | 终点圆的x轴坐标。<br>默认单位:vp。 | 3475e41f4b71Sopenharmony_ci| y1 | number | 是 | 终点圆的y轴坐标。<br>默认单位:vp。 | 3476e41f4b71Sopenharmony_ci| r1 | number | 是 | 终点圆的半径。必须为非负且有限的。<br>默认单位:vp。 | 3477e41f4b71Sopenharmony_ci 3478e41f4b71Sopenharmony_ci**返回值:** 3479e41f4b71Sopenharmony_ci 3480e41f4b71Sopenharmony_ci| 类型 | 说明 | 3481e41f4b71Sopenharmony_ci| ------ | --------- | 3482e41f4b71Sopenharmony_ci| [CanvasGradient](ts-components-canvas-canvasgradient.md) | 新的CanvasGradient对象,用于在canvas上创建渐变效果。 | 3483e41f4b71Sopenharmony_ci 3484e41f4b71Sopenharmony_ci**示例:** 3485e41f4b71Sopenharmony_ci 3486e41f4b71Sopenharmony_ci ```ts 3487e41f4b71Sopenharmony_ci // xxx.ets 3488e41f4b71Sopenharmony_ci @Entry 3489e41f4b71Sopenharmony_ci @Component 3490e41f4b71Sopenharmony_ci struct CreateRadialGradient { 3491e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3492e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3493e41f4b71Sopenharmony_ci 3494e41f4b71Sopenharmony_ci build() { 3495e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3496e41f4b71Sopenharmony_ci Canvas(this.context) 3497e41f4b71Sopenharmony_ci .width('100%') 3498e41f4b71Sopenharmony_ci .height('100%') 3499e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3500e41f4b71Sopenharmony_ci .onReady(() =>{ 3501e41f4b71Sopenharmony_ci let grad = this.context.createRadialGradient(200,200,50, 200,200,200) 3502e41f4b71Sopenharmony_ci grad.addColorStop(0.0, '#ff0000') 3503e41f4b71Sopenharmony_ci grad.addColorStop(0.5, '#ffffff') 3504e41f4b71Sopenharmony_ci grad.addColorStop(1.0, '#00ff00') 3505e41f4b71Sopenharmony_ci this.context.fillStyle = grad 3506e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 440, 440) 3507e41f4b71Sopenharmony_ci }) 3508e41f4b71Sopenharmony_ci } 3509e41f4b71Sopenharmony_ci .width('100%') 3510e41f4b71Sopenharmony_ci .height('100%') 3511e41f4b71Sopenharmony_ci } 3512e41f4b71Sopenharmony_ci } 3513e41f4b71Sopenharmony_ci ``` 3514e41f4b71Sopenharmony_ci 3515e41f4b71Sopenharmony_ci  3516e41f4b71Sopenharmony_ci 3517e41f4b71Sopenharmony_ci### createConicGradient<sup>10+</sup> 3518e41f4b71Sopenharmony_ci 3519e41f4b71Sopenharmony_cicreateConicGradient(startAngle: number, x: number, y: number): CanvasGradient 3520e41f4b71Sopenharmony_ci 3521e41f4b71Sopenharmony_ci创建一个圆锥渐变色。 3522e41f4b71Sopenharmony_ci 3523e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3524e41f4b71Sopenharmony_ci 3525e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3526e41f4b71Sopenharmony_ci 3527e41f4b71Sopenharmony_ci**参数:** 3528e41f4b71Sopenharmony_ci 3529e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3530e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ----------------------------------- | 3531e41f4b71Sopenharmony_ci| startAngle | number | 是 | 开始渐变的角度。角度测量从中心右侧水平开始,顺时针移动。<br>单位:弧度。 | 3532e41f4b71Sopenharmony_ci| x | number | 是 | 圆锥渐变的中心x轴坐标。<br>默认单位:vp。 | 3533e41f4b71Sopenharmony_ci| y | number | 是 | 圆锥渐变的中心y轴坐标。<br>默认单位:vp。 | 3534e41f4b71Sopenharmony_ci 3535e41f4b71Sopenharmony_ci**返回值:** 3536e41f4b71Sopenharmony_ci 3537e41f4b71Sopenharmony_ci| 类型 | 说明 | 3538e41f4b71Sopenharmony_ci| ------ | --------- | 3539e41f4b71Sopenharmony_ci| [CanvasGradient](ts-components-canvas-canvasgradient.md) | 新的CanvasGradient对象,用于在canvas上创建渐变效果。 | 3540e41f4b71Sopenharmony_ci 3541e41f4b71Sopenharmony_ci**示例:** 3542e41f4b71Sopenharmony_ci 3543e41f4b71Sopenharmony_ci```ts 3544e41f4b71Sopenharmony_ci// xxx.ets 3545e41f4b71Sopenharmony_ci@Entry 3546e41f4b71Sopenharmony_ci@Component 3547e41f4b71Sopenharmony_cistruct CanvasExample { 3548e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3549e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3550e41f4b71Sopenharmony_ci 3551e41f4b71Sopenharmony_ci build() { 3552e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3553e41f4b71Sopenharmony_ci Canvas(this.context) 3554e41f4b71Sopenharmony_ci .width('100%') 3555e41f4b71Sopenharmony_ci .height('100%') 3556e41f4b71Sopenharmony_ci .backgroundColor('#ffffff') 3557e41f4b71Sopenharmony_ci .onReady(() => { 3558e41f4b71Sopenharmony_ci let grad = this.context.createConicGradient(0, 50, 80) 3559e41f4b71Sopenharmony_ci grad.addColorStop(0.0, '#ff0000') 3560e41f4b71Sopenharmony_ci grad.addColorStop(0.5, '#ffffff') 3561e41f4b71Sopenharmony_ci grad.addColorStop(1.0, '#00ff00') 3562e41f4b71Sopenharmony_ci this.context.fillStyle = grad 3563e41f4b71Sopenharmony_ci this.context.fillRect(0, 30, 100, 100) 3564e41f4b71Sopenharmony_ci }) 3565e41f4b71Sopenharmony_ci } 3566e41f4b71Sopenharmony_ci .width('100%') 3567e41f4b71Sopenharmony_ci .height('100%') 3568e41f4b71Sopenharmony_ci } 3569e41f4b71Sopenharmony_ci} 3570e41f4b71Sopenharmony_ci``` 3571e41f4b71Sopenharmony_ci 3572e41f4b71Sopenharmony_ci  3573e41f4b71Sopenharmony_ci 3574e41f4b71Sopenharmony_ci### on('onAttach')<sup>13+</sup> 3575e41f4b71Sopenharmony_ci 3576e41f4b71Sopenharmony_cion(type: 'onAttach', callback: () => void): void 3577e41f4b71Sopenharmony_ci 3578e41f4b71Sopenharmony_ci订阅CanvasRenderingContext2D与Canvas组件发生绑定的场景。 3579e41f4b71Sopenharmony_ci 3580e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 3581e41f4b71Sopenharmony_ci 3582e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3583e41f4b71Sopenharmony_ci 3584e41f4b71Sopenharmony_ci**参数:** 3585e41f4b71Sopenharmony_ci 3586e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3587e41f4b71Sopenharmony_ci| ------ | --------- | ---- | ---------------------------------------------------------------------- | 3588e41f4b71Sopenharmony_ci| type | string | 是 | 订阅CanvasRenderingContext2D与Canvas组件发生绑定的回调 | 3589e41f4b71Sopenharmony_ci| callback | () => void | 是 | 订阅CanvasRenderingContext2D与Canvas组件发生绑定后触发的回调 | 3590e41f4b71Sopenharmony_ci 3591e41f4b71Sopenharmony_ci### on('onDetach')<sup>13+</sup> 3592e41f4b71Sopenharmony_ci 3593e41f4b71Sopenharmony_cion(type: 'onDetach', callback: () => void): void 3594e41f4b71Sopenharmony_ci 3595e41f4b71Sopenharmony_ci订阅CanvasRenderingContext2D与Canvas组件解除绑定的场景。 3596e41f4b71Sopenharmony_ci 3597e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 3598e41f4b71Sopenharmony_ci 3599e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3600e41f4b71Sopenharmony_ci 3601e41f4b71Sopenharmony_ci**参数:** 3602e41f4b71Sopenharmony_ci 3603e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3604e41f4b71Sopenharmony_ci| ------ | --------- | ---- | ---------------------------------------------------------------------- | 3605e41f4b71Sopenharmony_ci| type | string | 是 | 订阅CanvasRenderingContext2D与Canvas组件解除绑定的回调 | 3606e41f4b71Sopenharmony_ci| callback | () => void | 是 | 订阅CanvasRenderingContext2D与Canvas组件解除绑定后触发的回调 | 3607e41f4b71Sopenharmony_ci 3608e41f4b71Sopenharmony_ci### off('onAttach')<sup>13+</sup> 3609e41f4b71Sopenharmony_ci 3610e41f4b71Sopenharmony_cioff(type: 'onAttach', callback?: () => void): void 3611e41f4b71Sopenharmony_ci 3612e41f4b71Sopenharmony_ci取消订阅CanvasRenderingContext2D与Canvas组件发生绑定的场景。 3613e41f4b71Sopenharmony_ci 3614e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 3615e41f4b71Sopenharmony_ci 3616e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3617e41f4b71Sopenharmony_ci 3618e41f4b71Sopenharmony_ci**参数:** 3619e41f4b71Sopenharmony_ci 3620e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3621e41f4b71Sopenharmony_ci| ------ | --------- | ---- | ---------------------------------------------------------------------- | 3622e41f4b71Sopenharmony_ci| type | string | 是 | 取消订阅CanvasRenderingContext2D与Canvas组件发生绑定的回调 | 3623e41f4b71Sopenharmony_ci| callback | () => void | 否 | 为空代表取消所有订阅CanvasRenderingContext2D与Canvas组件发生绑定后触发的回调。<br>非空代表取消订阅发生绑定对应的回调。 | 3624e41f4b71Sopenharmony_ci 3625e41f4b71Sopenharmony_ci### off('onDetach')<sup>13+</sup> 3626e41f4b71Sopenharmony_ci 3627e41f4b71Sopenharmony_cioff(type: 'onDetach', callback?: () => void): void 3628e41f4b71Sopenharmony_ci 3629e41f4b71Sopenharmony_ci取消订阅CanvasRenderingContext2D与Canvas组件解除绑定的场景。 3630e41f4b71Sopenharmony_ci 3631e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 3632e41f4b71Sopenharmony_ci 3633e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3634e41f4b71Sopenharmony_ci 3635e41f4b71Sopenharmony_ci**参数:** 3636e41f4b71Sopenharmony_ci 3637e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3638e41f4b71Sopenharmony_ci| ------ | --------- | ---- | ---------------------------------------------------------------------- | 3639e41f4b71Sopenharmony_ci| type | string | 是 | 取消订阅CanvasRenderingContext2D与Canvas组件解除绑定的回调 | 3640e41f4b71Sopenharmony_ci| callback | () => void | 否 | 为空代表取消所有订阅CanvasRenderingContext2D与Canvas组件解除绑定后触发的回调。<br>非空代表取消订阅接触绑定对应的回调。 | 3641e41f4b71Sopenharmony_ci 3642e41f4b71Sopenharmony_ci**示例:** 3643e41f4b71Sopenharmony_ci 3644e41f4b71Sopenharmony_ci```ts 3645e41f4b71Sopenharmony_ciimport { FrameNode } from '@kit.ArkUI' 3646e41f4b71Sopenharmony_ci// xxx.ets 3647e41f4b71Sopenharmony_ci@Entry 3648e41f4b71Sopenharmony_ci@Component 3649e41f4b71Sopenharmony_cistruct AttachDetachExample { 3650e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3651e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3652e41f4b71Sopenharmony_ci private scroller: Scroller = new Scroller() 3653e41f4b71Sopenharmony_ci private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 3654e41f4b71Sopenharmony_ci private node: FrameNode | null = null 3655e41f4b71Sopenharmony_ci 3656e41f4b71Sopenharmony_ci attachCallback(): void { 3657e41f4b71Sopenharmony_ci console.info('CanvasRenderingContext2D attached to the canvas frame node.') 3658e41f4b71Sopenharmony_ci this.node = this.context.canvas 3659e41f4b71Sopenharmony_ci } 3660e41f4b71Sopenharmony_ci detachCallback(): void { 3661e41f4b71Sopenharmony_ci console.info('CanvasRenderingContext2D detach from the canvas frame node.') 3662e41f4b71Sopenharmony_ci this.node = null 3663e41f4b71Sopenharmony_ci } 3664e41f4b71Sopenharmony_ci aboutToAppear(): void { 3665e41f4b71Sopenharmony_ci this.context.on('onAttach', this.attachCallback.bind(this)) 3666e41f4b71Sopenharmony_ci this.context.on('onDetach', this.detachCallback.bind(this)) 3667e41f4b71Sopenharmony_ci } 3668e41f4b71Sopenharmony_ci aboutToDisappear(): void { 3669e41f4b71Sopenharmony_ci this.context.off('onAttach', this.attachCallback) 3670e41f4b71Sopenharmony_ci this.context.off('onDetach', this.detachCallback) 3671e41f4b71Sopenharmony_ci } 3672e41f4b71Sopenharmony_ci 3673e41f4b71Sopenharmony_ci build() { 3674e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3675e41f4b71Sopenharmony_ci Scroll(this.scroller) { 3676e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column}) { 3677e41f4b71Sopenharmony_ci ForEach(this.arr, (item: number) => { 3678e41f4b71Sopenharmony_ci Row() { 3679e41f4b71Sopenharmony_ci if (item == 3) { 3680e41f4b71Sopenharmony_ci Canvas(this.context) 3681e41f4b71Sopenharmony_ci .width('100%') 3682e41f4b71Sopenharmony_ci .height(150) 3683e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 3684e41f4b71Sopenharmony_ci .onReady(() => { 3685e41f4b71Sopenharmony_ci this.context.font = '30vp sans-serif' 3686e41f4b71Sopenharmony_ci this.node?.commonEvent.setOnVisibleAreaApproximateChange( 3687e41f4b71Sopenharmony_ci { ratios: [0, 1], expectedUpdateInterval: 10}, 3688e41f4b71Sopenharmony_ci (isVisible: boolean, currentRatio: number) => { 3689e41f4b71Sopenharmony_ci if (!isVisible && currentRatio <= 0.0) { 3690e41f4b71Sopenharmony_ci console.info('Canvas is completely invisible.') 3691e41f4b71Sopenharmony_ci } 3692e41f4b71Sopenharmony_ci if (isVisible && currentRatio >= 1.0) { 3693e41f4b71Sopenharmony_ci console.info('Canvas is fully visible.') 3694e41f4b71Sopenharmony_ci } 3695e41f4b71Sopenharmony_ci } 3696e41f4b71Sopenharmony_ci ) 3697e41f4b71Sopenharmony_ci }) 3698e41f4b71Sopenharmony_ci } else { 3699e41f4b71Sopenharmony_ci Text(item.toString()) 3700e41f4b71Sopenharmony_ci .width('100%') 3701e41f4b71Sopenharmony_ci .height(150) 3702e41f4b71Sopenharmony_ci .backgroundColor(Color.Blue) 3703e41f4b71Sopenharmony_ci .borderRadius(15) 3704e41f4b71Sopenharmony_ci .fontSize(16) 3705e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 3706e41f4b71Sopenharmony_ci .margin({ top: 5 }) 3707e41f4b71Sopenharmony_ci } 3708e41f4b71Sopenharmony_ci } 3709e41f4b71Sopenharmony_ci }, (item: number) => item.toString()) 3710e41f4b71Sopenharmony_ci } 3711e41f4b71Sopenharmony_ci } 3712e41f4b71Sopenharmony_ci .width('90%') 3713e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 3714e41f4b71Sopenharmony_ci .scrollable(ScrollDirection.Vertical) 3715e41f4b71Sopenharmony_ci } 3716e41f4b71Sopenharmony_ci .width('100%') 3717e41f4b71Sopenharmony_ci .height('100%') 3718e41f4b71Sopenharmony_ci } 3719e41f4b71Sopenharmony_ci} 3720e41f4b71Sopenharmony_ci``` 3721e41f4b71Sopenharmony_ci 3722e41f4b71Sopenharmony_ci### startImageAnalyzer<sup>12+</sup> 3723e41f4b71Sopenharmony_ci 3724e41f4b71Sopenharmony_cistartImageAnalyzer(config: ImageAnalyzerConfig): Promise\<void> 3725e41f4b71Sopenharmony_ci 3726e41f4b71Sopenharmony_ci配置AI分析并启动AI分析功能,使用前需先[使能](ts-components-canvas-canvas.md#enableanalyzer12)图像AI分析能力。<br>该方法调用时,将截取调用时刻的画面帧进行分析,使用时需注意启动分析的时机,避免出现画面和分析内容不一致的情况。<br>未执行完重复调用该方法会触发错误回调。示例代码同stopImageAnalyzer。 3727e41f4b71Sopenharmony_ci 3728e41f4b71Sopenharmony_ci> **说明:** 3729e41f4b71Sopenharmony_ci> 3730e41f4b71Sopenharmony_ci> 分析类型不支持动态修改。 3731e41f4b71Sopenharmony_ci> 当检测到画面有变化时,分析结果将自动销毁,可重新调用本接口启动分析。 3732e41f4b71Sopenharmony_ci> 该特性依赖设备能力,不支持该能力的情况下,将返回错误码。 3733e41f4b71Sopenharmony_ci 3734e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3735e41f4b71Sopenharmony_ci 3736e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3737e41f4b71Sopenharmony_ci 3738e41f4b71Sopenharmony_ci**参数:** 3739e41f4b71Sopenharmony_ci 3740e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3741e41f4b71Sopenharmony_ci| ------ | --------- | ---- | ---------------------------------------------------------------------- | 3742e41f4b71Sopenharmony_ci| config | [ImageAnalyzerConfig](ts-image-common.md#imageanalyzerconfig) | 是 | 执行AI分析所需要的入参,用于配置AI分析功能。 | 3743e41f4b71Sopenharmony_ci 3744e41f4b71Sopenharmony_ci**返回值:** 3745e41f4b71Sopenharmony_ci 3746e41f4b71Sopenharmony_ci| 类型 | 说明 | 3747e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------ | 3748e41f4b71Sopenharmony_ci| Promise\<void> | Promise对象,用于获取AI分析是否成功执行。 | 3749e41f4b71Sopenharmony_ci 3750e41f4b71Sopenharmony_ci**错误码:** 3751e41f4b71Sopenharmony_ci 3752e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[AI分析类库错误码](../errorcode-image-analyzer.md)。 3753e41f4b71Sopenharmony_ci 3754e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3755e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 3756e41f4b71Sopenharmony_ci| 110001 | AI analysis is unsupported. | 3757e41f4b71Sopenharmony_ci| 110002 | AI analysis is ongoing. | 3758e41f4b71Sopenharmony_ci 3759e41f4b71Sopenharmony_ci### stopImageAnalyzer<sup>12+</sup> 3760e41f4b71Sopenharmony_ci 3761e41f4b71Sopenharmony_cistopImageAnalyzer(): void 3762e41f4b71Sopenharmony_ci 3763e41f4b71Sopenharmony_ci停止AI分析功能,AI分析展示的内容将被销毁。 3764e41f4b71Sopenharmony_ci 3765e41f4b71Sopenharmony_ci> **说明:** 3766e41f4b71Sopenharmony_ci> 3767e41f4b71Sopenharmony_ci> 在startImageAnalyzer方法未返回结果时调用本方法,会触发其错误回调。 3768e41f4b71Sopenharmony_ci> 该特性依赖设备能力。 3769e41f4b71Sopenharmony_ci 3770e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3771e41f4b71Sopenharmony_ci 3772e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3773e41f4b71Sopenharmony_ci 3774e41f4b71Sopenharmony_ci**示例:** 3775e41f4b71Sopenharmony_ci 3776e41f4b71Sopenharmony_ci```ts 3777e41f4b71Sopenharmony_ci// xxx.ets 3778e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3779e41f4b71Sopenharmony_ci 3780e41f4b71Sopenharmony_ci@Entry 3781e41f4b71Sopenharmony_ci@Component 3782e41f4b71Sopenharmony_cistruct ImageAnalyzerExample { 3783e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 3784e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 3785e41f4b71Sopenharmony_ci private config: ImageAnalyzerConfig = { 3786e41f4b71Sopenharmony_ci types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT] 3787e41f4b71Sopenharmony_ci } 3788e41f4b71Sopenharmony_ci private img = new ImageBitmap('page/common/test.jpg') 3789e41f4b71Sopenharmony_ci private aiController: ImageAnalyzerController = new ImageAnalyzerController() 3790e41f4b71Sopenharmony_ci private options: ImageAIOptions = { 3791e41f4b71Sopenharmony_ci types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT], 3792e41f4b71Sopenharmony_ci aiController: this.aiController 3793e41f4b71Sopenharmony_ci } 3794e41f4b71Sopenharmony_ci 3795e41f4b71Sopenharmony_ci build() { 3796e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 3797e41f4b71Sopenharmony_ci Button('start') 3798e41f4b71Sopenharmony_ci .width(80) 3799e41f4b71Sopenharmony_ci .height(80) 3800e41f4b71Sopenharmony_ci .onClick(() => { 3801e41f4b71Sopenharmony_ci this.context.startImageAnalyzer(this.config) 3802e41f4b71Sopenharmony_ci .then(() => { 3803e41f4b71Sopenharmony_ci console.log("analysis complete") 3804e41f4b71Sopenharmony_ci }) 3805e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 3806e41f4b71Sopenharmony_ci console.log("error code: " + error.code) 3807e41f4b71Sopenharmony_ci }) 3808e41f4b71Sopenharmony_ci }) 3809e41f4b71Sopenharmony_ci Button('stop') 3810e41f4b71Sopenharmony_ci .width(80) 3811e41f4b71Sopenharmony_ci .height(80) 3812e41f4b71Sopenharmony_ci .onClick(() => { 3813e41f4b71Sopenharmony_ci this.context.stopImageAnalyzer() 3814e41f4b71Sopenharmony_ci }) 3815e41f4b71Sopenharmony_ci Button('getTypes') 3816e41f4b71Sopenharmony_ci .width(80) 3817e41f4b71Sopenharmony_ci .height(80) 3818e41f4b71Sopenharmony_ci .onClick(() => { 3819e41f4b71Sopenharmony_ci this.aiController.getImageAnalyzerSupportTypes() 3820e41f4b71Sopenharmony_ci }) 3821e41f4b71Sopenharmony_ci Canvas(this.context, this.options) 3822e41f4b71Sopenharmony_ci .width(200) 3823e41f4b71Sopenharmony_ci .height(200) 3824e41f4b71Sopenharmony_ci .enableAnalyzer(true) 3825e41f4b71Sopenharmony_ci .onReady(() => { 3826e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 0, 0, 200, 200) 3827e41f4b71Sopenharmony_ci }) 3828e41f4b71Sopenharmony_ci } 3829e41f4b71Sopenharmony_ci .width('100%') 3830e41f4b71Sopenharmony_ci .height('100%') 3831e41f4b71Sopenharmony_ci } 3832e41f4b71Sopenharmony_ci} 3833e41f4b71Sopenharmony_ci``` 3834e41f4b71Sopenharmony_ci 3835e41f4b71Sopenharmony_ci## CanvasDirection 3836e41f4b71Sopenharmony_ci 3837e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3838e41f4b71Sopenharmony_ci 3839e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3840e41f4b71Sopenharmony_ci 3841e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3842e41f4b71Sopenharmony_ci 3843e41f4b71Sopenharmony_ci| 类型 | 说明 | 3844e41f4b71Sopenharmony_ci| ------- | ------------------- | 3845e41f4b71Sopenharmony_ci| inherit | 继承canvas组件通用属性已设定的文本方向。 | 3846e41f4b71Sopenharmony_ci| ltr | 从左往右。 | 3847e41f4b71Sopenharmony_ci| rtl | 从右往左。 | 3848e41f4b71Sopenharmony_ci 3849e41f4b71Sopenharmony_ci## CanvasFillRule 3850e41f4b71Sopenharmony_ci 3851e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3852e41f4b71Sopenharmony_ci 3853e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3854e41f4b71Sopenharmony_ci 3855e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3856e41f4b71Sopenharmony_ci 3857e41f4b71Sopenharmony_ci| 类型 | 说明 | 3858e41f4b71Sopenharmony_ci| ------- | ----- | 3859e41f4b71Sopenharmony_ci| evenodd | 奇偶规则。 | 3860e41f4b71Sopenharmony_ci| nonzero | 非零规则。 | 3861e41f4b71Sopenharmony_ci 3862e41f4b71Sopenharmony_ci## CanvasLineCap 3863e41f4b71Sopenharmony_ci 3864e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3865e41f4b71Sopenharmony_ci 3866e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3867e41f4b71Sopenharmony_ci 3868e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3869e41f4b71Sopenharmony_ci 3870e41f4b71Sopenharmony_ci| 类型 | 说明 | 3871e41f4b71Sopenharmony_ci| ------ | ----------------------------- | 3872e41f4b71Sopenharmony_ci| butt | 线条两端为平行线,不额外扩展。 | 3873e41f4b71Sopenharmony_ci| round | 在线条两端延伸半个圆,直径等于线宽。 | 3874e41f4b71Sopenharmony_ci| square | 在线条两端延伸一个矩形,宽度等于线宽的一半,高度等于线宽。 | 3875e41f4b71Sopenharmony_ci 3876e41f4b71Sopenharmony_ci## CanvasLineJoin 3877e41f4b71Sopenharmony_ci 3878e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3879e41f4b71Sopenharmony_ci 3880e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3881e41f4b71Sopenharmony_ci 3882e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3883e41f4b71Sopenharmony_ci 3884e41f4b71Sopenharmony_ci| 类型 | 说明 | 3885e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | 3886e41f4b71Sopenharmony_ci| bevel | 在线段相连处使用三角形为底填充, 每个部分矩形拐角独立。 | 3887e41f4b71Sopenharmony_ci| miter | 在相连部分的外边缘处进行延伸,使其相交于一点,形成一个菱形区域,该属性可以通过设置miterLimit属性展现效果。 | 3888e41f4b71Sopenharmony_ci| round | 在线段相连处绘制一个扇形,扇形的圆角半径是线段的宽度。 | 3889e41f4b71Sopenharmony_ci 3890e41f4b71Sopenharmony_ci## CanvasTextAlign 3891e41f4b71Sopenharmony_ci 3892e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3893e41f4b71Sopenharmony_ci 3894e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3895e41f4b71Sopenharmony_ci 3896e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3897e41f4b71Sopenharmony_ci 3898e41f4b71Sopenharmony_ci| 类型 | 说明 | 3899e41f4b71Sopenharmony_ci| ------ | ------------ | 3900e41f4b71Sopenharmony_ci| center | 文本居中对齐。 | 3901e41f4b71Sopenharmony_ci| start | 文本对齐界线开始的地方。 | 3902e41f4b71Sopenharmony_ci| end | 文本对齐界线结束的地方。 | 3903e41f4b71Sopenharmony_ci| left | 文本左对齐。 | 3904e41f4b71Sopenharmony_ci| right | 文本右对齐。 | 3905e41f4b71Sopenharmony_ci 3906e41f4b71Sopenharmony_ci## CanvasTextBaseline 3907e41f4b71Sopenharmony_ci 3908e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3909e41f4b71Sopenharmony_ci 3910e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3911e41f4b71Sopenharmony_ci 3912e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3913e41f4b71Sopenharmony_ci 3914e41f4b71Sopenharmony_ci| 类型 | 说明 | 3915e41f4b71Sopenharmony_ci| ----------- | ---------------------------------------- | 3916e41f4b71Sopenharmony_ci| alphabetic | 文本基线是标准的字母基线。 | 3917e41f4b71Sopenharmony_ci| bottom | 文本基线在文本块的底部。 与ideographic基线的区别在于ideographic基线不需要考虑下行字母。 | 3918e41f4b71Sopenharmony_ci| hanging | 文本基线是悬挂基线。 | 3919e41f4b71Sopenharmony_ci| ideographic | 文字基线是表意字基线;如果字符本身超出了alphabetic基线,那么ideograhpic基线位置在字符本身的底部。 | 3920e41f4b71Sopenharmony_ci| middle | 文本基线在文本块的中间。 | 3921e41f4b71Sopenharmony_ci| top | 文本基线在文本块的顶部。 | 3922e41f4b71Sopenharmony_ci 3923e41f4b71Sopenharmony_ci## ImageSmoothingQuality 3924e41f4b71Sopenharmony_ci 3925e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3926e41f4b71Sopenharmony_ci 3927e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3928e41f4b71Sopenharmony_ci 3929e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3930e41f4b71Sopenharmony_ci 3931e41f4b71Sopenharmony_ci| 类型 | 说明 | 3932e41f4b71Sopenharmony_ci| ------ | ---- | 3933e41f4b71Sopenharmony_ci| low | 低画质 | 3934e41f4b71Sopenharmony_ci| medium | 中画质 | 3935e41f4b71Sopenharmony_ci| high | 高画质 | 3936e41f4b71Sopenharmony_ci 3937e41f4b71Sopenharmony_ci## TextMetrics 3938e41f4b71Sopenharmony_ci 3939e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 3940e41f4b71Sopenharmony_ci 3941e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3942e41f4b71Sopenharmony_ci 3943e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 3944e41f4b71Sopenharmony_ci 3945e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 3946e41f4b71Sopenharmony_ci| ---------- | -------------- | ------ | ---------------- | ------------------------ | 3947e41f4b71Sopenharmony_ci| width | number | 是 | 否 | 只读属性,文本方块的宽度。 | 3948e41f4b71Sopenharmony_ci| height | number | 是 | 否 | 只读属性,文本方块的高度。 | 3949e41f4b71Sopenharmony_ci| actualBoundingBoxAscent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到渲染文本的矩形边界顶部的距离。 | 3950e41f4b71Sopenharmony_ci| actualBoundingBoxDescent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到渲染文本的矩形边界底部的距离。 | 3951e41f4b71Sopenharmony_ci| actualBoundingBoxLeft | number | 是 | 否 | 只读属性,平行于基线,从[CanvasRenderingContext2D.textAlign](#canvastextalign)属性确定的对齐点到文本矩形边界左侧的距离。 | 3952e41f4b71Sopenharmony_ci| actualBoundingBoxRight | number | 是 | 否 | 只读属性,平行于基线,从[CanvasRenderingContext2D.textAlign](#canvastextalign)属性确定的对齐点到文本矩形边界右侧的距离。 | 3953e41f4b71Sopenharmony_ci| alphabeticBaseline | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到线框的 alphabetic 基线的距离。 | 3954e41f4b71Sopenharmony_ci| emHeightAscent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到线框中 em 方块顶部的距离。 | 3955e41f4b71Sopenharmony_ci| emHeightDescent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到线框中 em 方块底部的距离。 | 3956e41f4b71Sopenharmony_ci| fontBoundingBoxAscent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到渲染文本的所有字体的矩形最高边界顶部的距离。 | 3957e41f4b71Sopenharmony_ci| fontBoundingBoxDescent | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到渲染文本的所有字体的矩形边界最底部的距离。 | 3958e41f4b71Sopenharmony_ci| hangingBaseline | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到线框的 hanging 基线的距离。 | 3959e41f4b71Sopenharmony_ci| ideographicBaseline | number | 是 | 否 | 只读属性,从[CanvasRenderingContext2D.textBaseline](#canvastextbaseline)属性标明的水平线到线框的 ideographic 基线的距离。 |