# @pixi-essentials/mixin-smart-mask

> Smarter masking to reduce redundant draw-calls & improve batching efficiency

Latest version **1.0.4** (published 2021-03-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @pixi-essentials/mixin-smart-mask
pnpm add @pixi-essentials/mixin-smart-mask
yarn add @pixi-essentials/mixin-smart-mask
bun add @pixi-essentials/mixin-smart-mask
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-03-28 |
| First published | 2020-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 305 |
| Author | Shukant Pal |
| Maintainers | shukantpal |
| Keywords | smart-mask, pixi.js, masking, webgl, stencil, scissor, sprite-mask, perf, performance |

## Links

- npm: https://www.npmjs.com/package/@pixi-essentials/mixin-smart-mask
- Repository: https://github.com/SukantPal/pixi-essentials
- Homepage: https://github.com/SukantPal/pixi-essentials/tree/master/packages/smart-mask#readme
- Issues: https://github.com/SukantPal/pixi-essentials/issues
- npm.io page: https://npm.io/package/@pixi-essentials/mixin-smart-mask

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2021-03-28
- 1.0.2 — 2020-08-09
- 1.0.1 — 2020-07-20
- 1.0.0 — 2020-07-03

## README

# @pixi-essentials/smart-mask

This package is a mixin for `DisplayObject` that optimizes mask rendering. It is used to prevent masking when the contents of a
display-object already lie inside the mask's bounds. It should **only be used with rectangular masks**.

## Installation :package:

```bash
npm install @pixi-essentials/mixin-smart-mask
```

## Usage :page_facing_up:

In order for to use smart-masks, you must set masks on the `smartMask` property of display-objects instead; before each rendering
frame, you must call `updateSmartMask` to re-evalulate which masks are essential for rendering correctly.

```js
import * as PIXI from 'pixi.js';
import '@pixi-essentials/mixin-smart-mask';

const app = new PIXI.Application();

const maskedContainer = new PIXI.Container();
const mask = new PIXI.Graphics().drawRect(0, 0, 100, 100);
const child = new PIXI.Graphics().beginFill(0xff).drawRect(0, 0, 50, 50).endFill();

maskedContainer.addChild(mask, child);

// Set smartMask instead of mask
maskedContainer.smartMask = mask;

/*
 * Before rendering your scene each frame, you must update the smart-masks.
 */
app.renderer.on('prerender', function() {
    /* 
     * This will recalculate the world transforms in the scene graph. You don't need to do this if you
     * are already doing it somewhere else OR you could pass skipUpdate=false to updateSmartMask() instead.
     */
    app.stage.getBounds();

    /*
     * This will set display-object masks only if they are required, based on whether their smart-mask bounds cover
     * their contents fully.
     */
    app.stage.updateSmartMask();
});

/*
 * Since the 50x50 child lies entirely inside the 100x100 mask, updateSmartMask() will not set the mask of the
 * masked-container.
 */
```

---
_Source: https://npm.io/package/@pixi-essentials/mixin-smart-mask · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
