• 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 initial 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): T
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**Return value**
28
29| Type  | Description                    |
30| ------ | ------------------------ |
31| T | Current component.|
32
33## clip<sup>18+</sup>
34
35clip(clip: Optional\<boolean>): T
36
37Sets 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 with [clip<sup>12+</sup>](#clip12), this API supports the **undefined** type for the **clip** parameter.
38
39**System capability**: SystemCapability.ArkUI.ArkUI.Full
40
41**Widget capability**: This API can be used in ArkTS widgets since API version 18.
42
43**Atomic service API**: This API can be used in atomic services since API version 18.
44
45**Parameters**
46
47| Name| Type              | Mandatory                                                        | Description|
48| ------ | ------------------ | ------------------------------------------------------------ | ---- |
49| 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.   |
50
51**Return value**
52
53| Type  | Description                    |
54| ------ | ------------------------ |
55| T | Current component.|
56
57## clip<sup>(deprecated)</sup>
58
59clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): T
60
61Sets whether to clip this component based on the given shape.
62
63> **NOTE**
64>
65> 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.
66
67**Widget capability**: This API can be used in ArkTS widgets since API version 9.
68
69**System capability**: SystemCapability.ArkUI.ArkUI.Full
70
71**Parameters**
72
73| Name| Type                                                        | Mandatory| Description                                                        |
74| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
75| 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.|
76
77**Return value**
78
79| Type  | Description                    |
80| ------ | ------------------------ |
81| T | Current component.|
82
83## clipShape<sup>12+</sup>
84
85clipShape(value: CircleShape | EllipseShape | PathShape | RectShape): T
86
87Clips this component according to the specified shape (which may include position information).
88
89> **NOTE**
90>
91> Different shapes support different ranges of attributes. A path is one type of shape, along with others like ellipses and rectangles.
92>
93> Path shapes do not support setting width and height attributes. For details about the supported attributes, see the specific shape documentation.
94>
95> The [fill](../js-apis-arkui-shape.md#fill) attribute of shapes has no effect on the **clipShape** API.
96
97**System capability**: SystemCapability.ArkUI.ArkUI.Full
98
99**Widget capability**: This API can be used in ArkTS widgets since API version 12.
100
101**Atomic service API**: This API can be used in atomic services since API version 12.
102
103**Parameters**
104
105| Name| Type                                                        | Mandatory| Description                                                        |
106| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
107| 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 (which may include position information) to clip the current component.<br>**NOTE**<br>The clipped area remains responsive to bound gesture events.|
108
109**Return value**
110
111| Type  | Description                    |
112| ------ | ------------------------ |
113| T | Current component.|
114
115## clipShape<sup>18+</sup>
116
117clipShape(shape: Optional\<CircleShape | EllipseShape | PathShape | RectShape>): T
118
119Clips this component according to the specified shape (which may include position information). Compared with [clipShape<sup>12+</sup>](#clipshape12), this API supports the **undefined** type for the **shape** parameter.
120
121> **NOTE**
122>
123> Different shapes support different ranges of attributes. A path is one type of shape, along with others like ellipses and rectangles.
124>
125> Path shapes do not support setting width and height attributes. For details about the supported attributes, see the specific shape documentation.
126>
127> The [fill](../js-apis-arkui-shape.md#fill) attribute of shapes has no effect on the **clipShape** API.
128
129**System capability**: SystemCapability.ArkUI.ArkUI.Full
130
131**Widget capability**: This API can be used in ArkTS widgets since API version 18.
132
133**Atomic service API**: This API can be used in atomic services since API version 18.
134
135**Parameters**
136
137| Name| Type                                                        | Mandatory| Description                                                        |
138| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
139| 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 (which may include position information) to clip the current component.<br>**NOTE**<br>The clipped area remains responsive to bound gesture events.<br>If **shape** is set to **undefined**, the previous value is retained.|
140
141**Return value**
142
143| Type  | Description                    |
144| ------ | ------------------------ |
145| T | Current component.|
146
147## mask<sup>12+</sup>
148
149mask(value: ProgressMask): T
150
151Adds a mask to the component to indicate the progress.
152
153**System capability**: SystemCapability.ArkUI.ArkUI.Full
154
155**Atomic service API**: This API can be used in atomic services since API version 12.
156
157**Parameters**
158
159| Name| Type                           | Mandatory| Description                                                |
160| ------ | ------------------------------- | ---- | ---------------------------------------------------- |
161| value  | [ProgressMask](#progressmask10) | Yes  | Mask to add to the component, which allows for dynamic adjustment of progress, maximum value, and color settings.|
162
163**Return value**
164
165| Type  | Description                    |
166| ------ | ------------------------ |
167| T | Current component.|
168
169## mask<sup>18+</sup>
170
171mask(mask: Optional\<ProgressMask>): T
172
173Adds a mask to the component to indicate the progress. Compared with [mask<sup>12+</sup>](#mask12), this API supports the **undefined** type for the **mask** parameter.
174
175**System capability**: SystemCapability.ArkUI.ArkUI.Full
176
177**Atomic service API**: This API can be used in atomic services since API version 18.
178
179**Parameters**
180
181| Name| Type                                                        | Mandatory| Description                            |
182| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
183| 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.    |
184
185**Return value**
186
187| Type  | Description                    |
188| ------ | ------------------------ |
189| T | Current component.|
190
191## mask<sup>(deprecated)</sup>
192
193mask(value: CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute | ProgressMask): T
194
195Adds a mask of the specified shape to the component.
196
197> **NOTE**
198>
199> 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.
200
201**Widget capability**: This API can be used in ArkTS widgets since API version 9.
202
203**System capability**: SystemCapability.ArkUI.ArkUI.Full
204
205**Parameters**
206
207| Name| Type                                                        | Mandatory| Description                            |
208| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
209| 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.|
210
211**Return value**
212
213| Type  | Description                    |
214| ------ | ------------------------ |
215| T | Current component.|
216
217## maskShape<sup>12+</sup>
218
219maskShape(value: CircleShape | EllipseShape | PathShape | RectShape): T
220
221Adds a mask of the specified shape to the component.
222
223**System capability**: SystemCapability.ArkUI.ArkUI.Full
224
225**Widget capability**: This API can be used in ArkTS widgets since API version 12.
226
227**Atomic service API**: This API can be used in atomic services since API version 12.
228
229**Parameters**
230
231| Name| Type                                                        | Mandatory| Description                            |
232| ------ | ------------------------------------------------------------ | ---- | -------------------------------- |
233| 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.|
234
235**Return value**
236
237| Type  | Description                    |
238| ------ | ------------------------ |
239| T | Current component.|
240
241## maskShape<sup>18+</sup>
242
243maskShape(shape: Optional\<CircleShape | EllipseShape | PathShape | RectShape>): T
244
245Adds a mask of the specified shape to the component. Compared with [maskShape<sup>12+</sup>](#maskshape12), this API supports the **undefined** type for the **shape** parameter.
246
247**System capability**: SystemCapability.ArkUI.ArkUI.Full
248
249**Widget capability**: This API can be used in ArkTS widgets since API version 18.
250
251**Atomic service API**: This API can be used in atomic services since API version 18.
252
253**Parameters**
254
255| Name| Type                                                        | Mandatory| Description                                                        |
256| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
257| 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.|
258
259**Return value**
260
261| Type  | Description                    |
262| ------ | ------------------------ |
263| T | Current component.|
264
265## ProgressMask<sup>10+</sup>
266
267Implements a **ProgressMask** object to set the progress, maximum value, and color of the mask.
268
269**Atomic service API**: This API can be used in atomic services since API version 11.
270
271**System capability**: SystemCapability.ArkUI.ArkUI.Full
272
273### constructor<sup>10+</sup>
274
275constructor(value: number, total: number, color: ResourceColor)
276
277Constructs a **ProgressMask** object.
278
279**Atomic service API**: This API can be used in atomic services since API version 11.
280
281**System capability**: SystemCapability.ArkUI.ArkUI.Full
282
283**Parameters**
284
285| Name| Type                                  | Mandatory| Description          |
286| ------ | ------------------------------------------ | ---- | ------------------ |
287| value  | number                                     | Yes  | Current value of the progress mask.<br> Value range: [0.0, +∞)|
288| total  | number                                     | Yes  | Maximum value of the progress mask.<br> Value range: [0.0, +∞)|
289| color  | [ResourceColor](ts-types.md#resourcecolor) | Yes  | Color of the progress mask.  |
290
291### updateProgress<sup>10+</sup>
292
293updateProgress(value: number): void
294
295Updates the progress value of the progress mask.
296
297**Atomic service API**: This API can be used in atomic services since API version 11.
298
299**System capability**: SystemCapability.ArkUI.ArkUI.Full
300
301**Parameters**
302
303| Name| Type| Mandatory| Description          |
304| ------ | -------- | ---- | ------------------ |
305| value  | number   | Yes  | Current value of the progress mask.|
306
307### updateColor<sup>10+</sup>
308
309updateColor(value: ResourceColor): void
310
311Updates the color of the progress mask.
312
313**Atomic service API**: This API can be used in atomic services since API version 11.
314
315**System capability**: SystemCapability.ArkUI.ArkUI.Full
316
317**Parameters**
318
319| Name| Type                                  | Mandatory| Description        |
320| ------ | ------------------------------------------ | ---- | ---------------- |
321| value  | [ResourceColor](ts-types.md#resourcecolor) | Yes  | Color of the progress mask.|
322
323### enableBreathingAnimation<sup>12+</sup>
324
325enableBreathingAnimation(value: boolean): void
326
327Sets whether to enable the breathing animation when the progress indicator is full. By default, the breathing animation is disabled.
328
329**Atomic service API**: This API can be used in atomic services since API version 12.
330
331**System capability**: SystemCapability.ArkUI.ArkUI.Full
332
333**Parameters**
334
335| Name| Type                                  | Mandatory| Description        |
336| ------ | ------------------------------------------ | ---- | ---------------- |
337| 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**|
338
339
340## Example
341
342### Example 1: Using Different Clipping Attributes
343
344```ts
345// xxx.ets
346import { CircleShape, RectShape } from '@kit.ArkUI';
347
348@Entry
349@Component
350struct ClipAndMaskExample {
351  build() {
352    Column({ space: 15 }) {
353      Text('clip').fontSize(12).width('75%').fontColor('#DCDCDC')
354      Row() {
355        Image($r('app.media.testImg')).width('500px').height('280px')
356      }
357      .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.
358      .borderRadius(20)
359
360      // Clip the image based on a circle with a diameter of 280 px.
361      Image($r('app.media.testImg'))
362        .clipShape(new CircleShape({ width: '280px', height: '280px' }))
363        .width('500px').height('280px')
364
365      Text('mask').fontSize(12).width('75%').fontColor('#DCDCDC')
366      // Add a 500x280 px square mask to the image.
367      Image($r('app.media.testImg'))
368        .maskShape(new RectShape({ width: '500px', height: '280px' }).fill(Color.Gray))
369        .width('500px').height('280px')
370
371      // Add a 280x280 px circular mask to the image.
372      Image($r('app.media.testImg'))
373        .maskShape(new CircleShape({ width: '280px', height: '280px' }).fill(Color.Gray))
374        .width('500px').height('280px')
375    }
376    .width('100%')
377    .margin({ top: 15 })
378  }
379}
380```
381
382![clipAndMask](figures/clipAndMask.PNG)
383
384### Example 2: Clipping and Masking with Animation
385
386```ts
387@Entry
388@Component
389struct ProgressMaskExample {
390  @State progressFlag1: boolean = true;
391  @State color: Color = 0x01006CDE;
392  @State value: number = 10.0;
393  @State enableBreathingAnimation: boolean = false;
394  @State progress: ProgressMask = new ProgressMask(10.0, 100.0, Color.Gray);
395
396  build() {
397    Column({ space: 15 }) {
398      Text('progress mask').fontSize(12).width('75%').fontColor('#DCDCDC')
399      // Add a 280x280 px progress mask to the image.
400      Image($r('app.media.testImg'))
401        .width('500px').height('280px')
402        .mask(this.progress)
403        .animation({
404          duration: 2000, // Animation duration.
405          curve: Curve.Linear, // Animation curve.
406          delay: 0, // Animation delay.
407          iterations: 1, // Number of playback times.
408          playMode: PlayMode.Normal // Animation playback mode.
409        }) // Animation configuration for the width and height attributes of the <Button> component.
410
411      // Update the progress value of the progress mask.
412      Button('updateProgress')
413        .onClick((event?: ClickEvent) => {
414          this.value += 10;
415          this.progress.updateProgress(this.value);
416        }).width(200).height(50).margin(20)
417
418      // Update the color of the progress mask.
419      Button('updateColor')
420        .onClick((event?: ClickEvent) => {
421          if (this.progressFlag1) {
422            this.progress.updateColor(0x9fff0000);
423          } else {
424            this.progress.updateColor(0x9f0000ff);
425          }
426          this.progressFlag1 = !this.progressFlag1
427        }).width(200).height(50).margin(20)
428
429      // Enable or disable the breathing animation.
430      Button('enableBreathingAnimation:' + this.enableBreathingAnimation)
431        .onClick((event?: ClickEvent) => {
432          this.enableBreathingAnimation = !this.enableBreathingAnimation;
433          this.progress.enableBreathingAnimation(this.enableBreathingAnimation);
434        }).width(200).height(50).margin(20)
435
436      // Restore the progress mask.
437      Button('click reset!')
438        .onClick((event?: ClickEvent) => {
439          this.value = 0;
440          this.progress.updateProgress(this.value);
441        }).width(200).height(50).margin(20)
442    }
443    .width('100%')
444    .margin({ top: 15 })
445  }
446}
447```
448
449![progressMask](figures/progressMask.gif)
450