# core/component/mask (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/component/mask/

Mask component. (clips child drawing to the attached node's content_size area)
- The actual clipping is done by NodeTree::draw_node calling begin_clip / end_clip before and after drawing the children.

## Mask (struct) (Default, Component)

Mask component. (clips child node drawing that goes outside the attached node's content_size area)
- The node's own components are not clipped. Clipping applies only while drawing the children after all components are drawn.
- Giving a round size clips to a rounded rectangle. Giving the same round size as the background Paint clips exactly to the background shape.

Example: Clip children to a rounded window

```rust
use vanilla::{Mask, NodeTree, Paint, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let window_id = node_tree.create_child(root_id, "Window");
node_tree[window_id].set_content_size(Vector2::create(200.0, 200.0));
let paint = node_tree.add_component(window_id, Paint::create_with_color("#223344")).unwrap();
paint.set_round_size(24.0);
let mask = node_tree.add_component(window_id, Mask::new()).unwrap();
mask.set_round_size(24.0);

let child_id = node_tree.create_child(window_id, "Child");
node_tree[child_id].set_content_size(Vector2::create(160.0, 160.0));
node_tree[child_id].set_local_position(Vector2::create(120.0, 120.0));
node_tree.add_component(child_id, Paint::create_with_color("#ff6633"));

let mask = node_tree[window_id].get_component::<Mask>().unwrap();
assert_eq!(mask.get_round_size(), 24.0);
```

- `pub fn new() -> Mask` (constructor, since 0.3.2): Constructor.
  - returns Mask: New mask with round size 0 (clipping to a sharp rectangle).
- `pub fn begin_clip(transform: &NodeTransform, round_size: f64, graphic: &mut Graphic)` (static, since 0.3.2): Begins clipping.
  - Sets the (0, 0, content_size width, height) rectangle in node content coordinates as the Graphic's clip area. (it is a stencil, so it clips exactly even with rotation and scale)
  - Called by NodeTree right before drawing the children of a node with a mask. Called in pairs with end_clip, and only what is drawn in between is clipped.
  - `transform` &NodeTransform: Node transform that decides the clip area. (reads content_size)
  - `round_size` f64: Corner round size. (node coordinates, 0 is a sharp rectangle)
  - `graphic` &mut Graphic: Graphic to set the clip area on.

  Example: Draw its own picture clipped to the node area

  ```rust
  use vanilla::{Color, Graphic, Mask, NodeTransform, Rect};
  
  fn draw_clipped(transform: &NodeTransform, graphic: &mut Graphic) {
  	Mask::begin_clip(transform, 8.0, graphic);
  	graphic.set_fill_color(Color::white());
  	graphic.draw_rect(Rect::create(-20.0, -20.0, 400.0, 400.0));
  	Mask::end_clip(graphic);
  }
  ```

- `pub fn end_clip(graphic: &mut Graphic)` (static, since 0.3.2): Ends clipping.
  - Releases the clip area set with begin_clip. (called by NodeTree after drawing all children)
  - `graphic` &mut Graphic: The graphic passed to begin_clip.
- `pub fn set_round_size(&mut self, round_size: f64)` (method, since 0.3.2): Sets the round size. (the same value as Paint's round_size matches the background exactly)
  - `round_size` f64: Corner round size of the clip rectangle. (node coordinates, 0 is a sharp rectangle, initially 0)
- `pub fn get_round_size(&self) -> f64` (method, since 0.3.2): Returns the round size.
  - returns f64: Corner round size of the clip rectangle. (node coordinates)
