• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Shape Clipping
2
3Shape clipping changes the visible portion of a component through clipping or masking.
4
5>  **NOTE**
6>
7> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9## clip<sup>12+</sup>
10
11clip(value: boolean)
12
13Sets whether to clip the areas of child components that extend beyond this component's boundaries, that is, whether to perform clipping based on the edge contour of the parent container
14
15**System capability**: SystemCapability.ArkUI.ArkUI.Full
16
17**Widget capability**: This API can be used in ArkTS widgets since API version 12.
18
19**Atomic service API**: This API can be used in atomic services since API version 12.
20
21**Parameters**
22
23| Name| Type   | Mandatory| Description                                                        |
24| ------ | ------- | ---- | ------------------------------------------------------------ |
25| value  | boolean | Yes  | Whether to perform clipping based on the edge contour of the parent container.<br>Default value: **false**<br>**true**: Perform clipping.<br>**false**: Do not perform clipping.<br>**NOTE**<br>If this parameter is set to **true**, child components exceeding the current component's bounds will not respond to bound gesture events.|
26
27## clip<sup>18+</sup>
28
29clip(clip: Optional\<boolean>)
30
31Sets whether to clip the areas of child components that extend beyond this component's boundaries, that is, whether to perform clipping based on the edge contour of the parent container Compared to [clip<sup>12+</sup>](#clip12), this API supports the **undefined** type for the **clip** parameter.
32
33**System capability**: SystemCapability.ArkUI.ArkUI.Full
34
35**Widget capability**: This API can be used in ArkTS widgets since API version 18.
36
37**Atomic service API**: This API can be used in atomic services since API version 18.
38
39**Parameters**
40
41| Name| Type              | Mandatory                                                        | Description|
42| ------ | ------------------ | ------------------------------------------------------------ | ---- |
43| clip   | Optional\<boolean> | Yes|  Whether to perform clipping based on the edge contour of the parent container.<br>Default value: **false**<br>**NOTE**<br>If this parameter is set to **true**, child components exceeding the current component's bounds will not respond to bound gesture events.<br>If **clip** is set to **undefined**, clipping is disabled, and child components are not clipped.   |
44
45## clip<sup>(deprecated)</sup>
46
47clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute)
48
49Sets whether to clip this component based on the given shape.
50
51> **NOTE**
52>
53> This API is supported since API version 7 and deprecated since API version 12. You are advised to use [clip](#clip12) and [clipShape](#clipshape12) instead.
54
55**Widget capability**: This API can be used in ArkTS widgets since API version 9.
56
57**System capability**: SystemCapability.ArkUI.ArkUI.Full
58
59**Parameters**
60
61| Name| Type                                                        | Mandatory| Description                                                        |
62| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
63| value  | boolean \| [CircleAttribute](ts-drawing-components-circle.md) \| [EllipseAttribute](ts-drawing-components-ellipse.md) \| [PathAttribute](ts-drawing-components-path.md) \| [RectAttribute](ts-drawing-components-rect.md) | Yes  | Clip mode. If the value is a shape attribute, the component is clipped based on the specified shape. If the value is of the Boolean type, it specifies whether to clip the component based on the boundaries of the parent container.<br>Default value: **false**<br>**NOTE**<br>If the value is a shape attribute, the clipped area can still respond to bound gesture events. If the value is of the Boolean type, the clipped area will not respond to bound gesture events.|
64
65## clipShape<sup>12+</sup>
66
67clipShape(value: CircleShape | EllipseShape | PathShape | RectShape)
68
69Clips this component based on the given shape.
70
71**System capability**: SystemCapability.ArkUI.ArkUI.Full
72
73**Widget capability**: This API can be used in ArkTS widgets since API version 12.
74
75**Atomic service API**: This API can be used in atomic services since API version 12.
76
77**Parameters**
78
79| Name| Type                                                        | Mandatory| Description                                                        |
80| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
81| value  | [CircleShape](../js-apis-arkui-shape.md#circleshape) \| [EllipseShape](../js-apis-arkui-shape.md#ellipseshape) \| [PathShape](../js-apis-arkui-shape.md#pathshape) \| [RectShape](../js-apis-arkui-shape.md#rectshape) | Yes  | Shape that the component to be clipped into.<br>**NOTE**<br>The clipped area remains responsive to bound gesture events.|
82
83## clipShape<sup>18+</sup>
84
85clipShape(shape: Optional\<CircleShape | EllipseShape | PathShape | RectShape>)
86
87Sets whether to clip this component based on the given shape. Compared to [clipShape<sup>12+</sup>](#clipshape12), this API supports the **undefined** type for the **shape** parameter.
88
89**System capability**: SystemCapability.ArkUI.ArkUI.Full
90
91**Widget capability**: This API can be used in ArkTS widgets since API version 18.
92
93**Atomic service API**: This API can be used in atomic services since API version 18.
94
95**Parameters**
96
97| Name| Type                                                        | Mandatory| Description                                                        |
98| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
99| shape  | Optional\<[CircleShape](../js-apis-arkui-shape.md#circleshape) \| [EllipseShape](../js-apis-arkui-shape.md#ellipseshape) \| [PathShape](../js-apis-arkui-shape.md#pathshape) \| [RectShape](../js-apis-arkui-shape.md#rectshape)> | Yes  | Shape that the component to be clipped into.<br>**NOTE**<br>The clipped area remains responsive to bound gesture events.<br>If **shape** is set to **undefined**, the previous value is retained.|
100
101## mask<sup>12+</sup>
102
103mask(value: ProgressMask)
104
105Adds a mask to the component to indicate the progress.
106
107**System capability**: SystemCapability.ArkUI.ArkUI.Full
108
109**Atomic service API**: This API can be used in atomic services since API version 12.
110
111**Parameters**
112
113| Name| Type                           | Mandatory| Description                                                |
114| ------ | ------------------------------- | ---- | ---------------------------------------------------- |
115| value  | [ProgressMask](#progressmask10) | Yes  | Mask to add to the component, which allows for dynamic adjustment of progress, maximum value, and color settings.|
116
117## mask<sup>18+</sup>
118
119mask(mask: Optional\<ProgressMask>)
120
121Adds a mask to the component to indicate the progress. Compared to [mask<sup>12+</sup>](#mask12), this API supports the **undefined** type for the **mask** parameter.
122
123**System capability**: SystemCapability.ArkUI.ArkUI.Full
124
125**Atomic service API**: This API can be used in atomic services since API version 18.
126
127**Parameters**
128
129| Name| Type                                                        | Mandatory| Description                            |
130| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
131| mask | Optional\<[ProgressMask](#progressmask10)> | Yes| Mask to add to the component, which allows for dynamic adjustment of progress, maximum value, and color settings.<br>If **mask** is set to **undefined**, the component to revert to its original effect without the mask to indicate the progress.    |
132
133## mask<sup>(deprecated)</sup>
134
135mask(value: CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute | ProgressMask)
136
137Adds a mask of the specified shape to the component.
138
139> **NOTE**
140>
141> This API is supported since API version 7 and deprecated since API version 12. You are advised to use [mask](#mask12) and [maskShape](#maskshape12) instead.
142
143**Widget capability**: This API can be used in ArkTS widgets since API version 9.
144
145**System capability**: SystemCapability.ArkUI.ArkUI.Full
146
147**Parameters**
148
149| Name| Type                                                        | Mandatory| Description                            |
150| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
151| value  | [CircleAttribute](ts-drawing-components-circle.md) \| [EllipseAttribute](ts-drawing-components-ellipse.md) \| [PathAttribute](ts-drawing-components-path.md) \| [RectAttribute](ts-drawing-components-rect.md) \| [ProgressMask](#progressmask10)<sup>10+</sup> | Yes  | Mask of the specified shape to add to the component.|
152
153## maskShape<sup>12+</sup>
154
155maskShape(value: CircleShape | EllipseShape | PathShape | RectShape)
156
157Adds a mask of the specified shape to the component.
158
159**System capability**: SystemCapability.ArkUI.ArkUI.Full
160
161**Widget capability**: This API can be used in ArkTS widgets since API version 12.
162
163**Atomic service API**: This API can be used in atomic services since API version 12.
164
165**Parameters**
166
167| Name| Type                                                        | Mandatory| Description                            |
168| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
169| value  | [CircleShape](../js-apis-arkui-shape.md#circleshape) \| [EllipseShape](../js-apis-arkui-shape.md#ellipseshape) \| [PathShape](../js-apis-arkui-shape.md#pathshape) \| [RectShape](../js-apis-arkui-shape.md#rectshape) | Yes  | Mask of the specified shape to add to the component.|
170
171## maskShape<sup>18+</sup>
172
173maskShape(shape: Optional\<CircleShape | EllipseShape | PathShape | RectShape>)
174
175Adds a mask of the specified shape to the component. Compared to [maskShape<sup>12+</sup>](#maskshape12), this API supports the **undefined** type for the **shape** parameter.
176
177**System capability**: SystemCapability.ArkUI.ArkUI.Full
178
179**Widget capability**: This API can be used in ArkTS widgets since API version 18.
180
181**Atomic service API**: This API can be used in atomic services since API version 18.
182
183**Parameters**
184
185| Name| Type                                                        | Mandatory| Description                                                        |
186| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
187| shape  | Optional\<[CircleShape](../js-apis-arkui-shape.md#circleshape) \| [EllipseShape](../js-apis-arkui-shape.md#ellipseshape) \| [PathShape](../js-apis-arkui-shape.md#pathshape) \| [RectShape](../js-apis-arkui-shape.md#rectshape)> | Yes  | Mask of the specified shape to add to the component.<br>If **shape** is set to **undefined**, the previous value is retained.|
188
189## ProgressMask<sup>10+</sup>
190
191Implements a **ProgressMask** object to set the progress, maximum value, and color of the mask.
192
193**Atomic service API**: This API can be used in atomic services since API version 11.
194
195**System capability**: SystemCapability.ArkUI.ArkUI.Full
196
197### constructor<sup>10+</sup>
198
199constructor(value: number, total: number, color: ResourceColor)
200
201Constructs a **ProgressMask** object.
202
203**Atomic service API**: This API can be used in atomic services since API version 11.
204
205**System capability**: SystemCapability.ArkUI.ArkUI.Full
206
207**Parameters**
208
209| Name| Type                                  | Mandatory| Description          |
210| ------ | ------------------------------------------ | ---- | ------------------ |
211| value  | number                                     | Yes  | Current value of the progress mask.<br> Value range: [0.0, +∞)|
212| total  | number                                     | Yes  | Maximum value of the progress mask.<br> Value range: [0.0, +∞)|
213| color  | [ResourceColor](ts-types.md#resourcecolor) | Yes  | Color of the progress mask.  |
214
215### updateProgress<sup>10+</sup>
216
217updateProgress(value: number): void
218
219Updates the progress value of the progress mask.
220
221**Atomic service API**: This API can be used in atomic services since API version 11.
222
223**System capability**: SystemCapability.ArkUI.ArkUI.Full
224
225**Parameters**
226
227| Name| Type| Mandatory| Description          |
228| ------ | -------- | ---- | ------------------ |
229| value  | number   | Yes  | Current value of the progress mask.|
230
231### updateColor<sup>10+</sup>
232
233updateColor(value: ResourceColor): void
234
235Updates the color of the progress mask.
236
237**Atomic service API**: This API can be used in atomic services since API version 11.
238
239**System capability**: SystemCapability.ArkUI.ArkUI.Full
240
241**Parameters**
242
243| Name| Type                                  | Mandatory| Description        |
244| ------ | ------------------------------------------ | ---- | ---------------- |
245| value  | [ResourceColor](ts-types.md#resourcecolor) | Yes  | Color of the progress mask.|
246
247### enableBreathingAnimation<sup>12+</sup>
248
249enableBreathingAnimation(value: boolean): void
250
251Sets whether to enable the breathing animation when the progress indicator is full. By default, the breathing animation is disabled.
252
253**Atomic service API**: This API can be used in atomic services since API version 12.
254
255**System capability**: SystemCapability.ArkUI.ArkUI.Full
256
257**Parameters**
258
259| Name| Type                                  | Mandatory| Description        |
260| ------ | ------------------------------------------ | ---- | ---------------- |
261| value  | boolean | Yes  | Whether to enable the breathing animation. The value **true** means to enable the breathing animation, and **false** means the opposite. Default value: **false**|
262
263
264## Example
265
266### Example 1: Using Different Clipping Attributes
267
268```ts
269// xxx.ets
270import { CircleShape, RectShape } from '@kit.ArkUI'
271
272@Entry
273@Component
274struct ClipAndMaskExample {
275  build() {
276    Column({ space: 15 }) {
277      Text('clip').fontSize(12).width('75%').fontColor('#DCDCDC')
278      Row() {
279        Image($r('app.media.testImg')).width('500px').height('280px')
280      }
281      .clip(true) // If clip is not set to true, the image is not confined by the rounded corners of the <Row> component and may extend beyond the <Row> component.
282      .borderRadius(20)
283
284      // Clip the image based on a circle with a diameter of 280 px.
285      Image($r('app.media.testImg'))
286        .clipShape(new CircleShape({ width: '280px', height: '280px' }))
287        .width('500px').height('280px')
288
289      Text('mask').fontSize(12).width('75%').fontColor('#DCDCDC')
290      // Add a 500 px x 280 px square mask to the image.
291      Image($r('app.media.testImg'))
292        .maskShape(new RectShape({ width: '500px', height: '280px' }).fill(Color.Gray))
293        .width('500px').height('280px')
294
295      // Add a 280 px x 280 px circular mask to the image.
296      Image($r('app.media.testImg'))
297        .maskShape(new CircleShape({ width: '280px', height: '280px' }).fill(Color.Gray))
298        .width('500px').height('280px')
299    }
300    .width('100%')
301    .margin({ top: 15 })
302  }
303}
304```
305
306![clipAndMask](figures/clipAndMask.PNG)
307
308### Example 2: Clipping and Masking with Animation
309
310```ts
311@Entry
312@Component
313struct ProgressMaskExample {
314  @State progressflag1: boolean = true;
315  @State color: Color = 0x01006CDE;
316  @State value: number = 10.0;
317  @State enableBreathingAnimation: boolean = false;
318  @State progress: ProgressMask = new ProgressMask(10.0, 100.0, Color.Gray);
319
320  build() {
321    Column({ space: 15 }) {
322      Text('progress mask').fontSize(12).width('75%').fontColor('#DCDCDC')
323      // Add a 280 px x 280 px progress mask to the image.
324      Image($r('app.media.testImg'))
325        .width('500px').height('280px')
326        .mask(this.progress)
327        .animation({
328          duration: 2000, // Animation duration.
329          .curve(Curve.Linear) // Animation curve.
330          delay: 0, // Animation delay.
331          iterations: 1, // Number of playback times.
332          playMode: PlayMode.Normal // Animation playback mode.
333        }) // Animation configuration for the width and height attributes of the <Button> component.
334
335      // Update the progress value of the progress mask.
336      Button('updateProgress')
337        .onClick((event?: ClickEvent) => {
338          this.value += 10;
339          this.progress.updateProgress(this.value);
340        }).width(200).height(50).margin(20)
341
342      // Update the color of the progress mask.
343      Button('updateColor')
344        .onClick((event?: ClickEvent) => {
345          if (this.progressflag1) {
346            this.progress.updateColor(0x9fff0000);
347          } else {
348            this.progress.updateColor(0x9f0000ff);
349          }
350          this.progressflag1 = !this.progressflag1
351        }).width(200).height(50).margin(20)
352
353      // Enable or disable the breathing animation.
354      Button('enableBreathingAnimation:' + this.enableBreathingAnimation)
355        .onClick((event?: ClickEvent) => {
356          this.enableBreathingAnimation = !this.enableBreathingAnimation
357          this.progress.enableBreathingAnimation(this.enableBreathingAnimation);
358        }).width(200).height(50).margin(20)
359
360      // Restore the progress mask.
361      Button('click reset!')
362        .onClick((event?: ClickEvent) => {
363          this.value = 0;
364          this.progress.updateProgress(this.value);
365        }).width(200).height(50).margin(20)
366    }
367    .width('100%')
368    .margin({ top: 15 })
369  }
370}
371```
372
373![progressMask](figures/progressMask.gif)
374