Skip to content

Commit 0927418

Browse files
authored
Merge pull request #37 from girayk/mask
clip masks
2 parents 25ff41c + 3df5e2a commit 0927418

File tree

11 files changed

+179
-1
lines changed

11 files changed

+179
-1
lines changed

playground/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const video = await composition.add(
2020
.offsetBy(30)
2121
);
2222

23+
const mask = new core.CircleMask({
24+
radius: 1080/2,
25+
position: {
26+
x: 1920 / 2,
27+
y: 1080 / 2,
28+
},
29+
});
30+
31+
video.mask = mask;
32+
2333
video.animate()
2434
.alpha(0.5).to(1, 120).to(0.5, 120).to(1, 60)
2535
.scale(0.1, 0, 'easeIn').to(1, 30)

src/clips/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from './video';
1414
export * from './audio';
1515
export * from './utils';
1616
export * from './html';
17+
export * from './mask';

src/clips/mask/circleMask.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Mask } from "./mask";
2+
import { CircleMaskProps } from "./mask.types";
3+
4+
/**
5+
* A circular mask of a given radius
6+
*/
7+
export class CircleMask extends Mask {
8+
9+
private _radius: number;
10+
11+
public constructor(props: CircleMaskProps){
12+
super(props);
13+
14+
this._radius = props.radius;
15+
16+
this.circle(this.position.x, this.position.y, this._radius)
17+
this.fill({color: '#FFF'})
18+
}
19+
}

src/clips/mask/ellipseMask.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EllipseMaskProps } from './mask.types';
2+
import { Mask } from './mask';
3+
4+
export class EllipseMask extends Mask {
5+
private _radius: { x: number; y: number };
6+
7+
public constructor(props: EllipseMaskProps) {
8+
super(props);
9+
10+
this._radius = props.radius;
11+
12+
this.ellipse(
13+
this.position.x,
14+
this.position.y,
15+
this._radius.x,
16+
this._radius.y,
17+
);
18+
this.fill({ color: '#FFF' });
19+
}
20+
}

src/clips/mask/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CircleMask } from "./circleMask";
2+
import { EllipseMask } from "./ellipseMask";
3+
import { RectangleMask } from "./rectangleMask";
4+
import { RoundRectangleMask } from "./roundRectangleMask";
5+
import { StarMask } from "./starMask";
6+
7+
export { CircleMask, EllipseMask, RectangleMask, RoundRectangleMask, StarMask };
8+

src/clips/mask/mask.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Graphics } from "pixi.js";
2+
import type { MaskProps } from "./mask.types";
3+
4+
export class Mask extends Graphics {
5+
constructor(props: MaskProps) {
6+
super();
7+
this.position = props.position ?? {x: 0, y: 0};
8+
}
9+
}

src/clips/mask/mask.types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
export interface MaskProps {
4+
/**
5+
* The position of the center of the mask for circular, elliptical and star masks and
6+
* the top left corner of the mask for rectangular masks
7+
*/
8+
position?: { x: number, y: number },
9+
}
10+
11+
export interface CircleMaskProps extends MaskProps {
12+
radius: number,
13+
}
14+
15+
export interface EllipseMaskProps extends MaskProps {
16+
radius: {
17+
x: number,
18+
y: number,
19+
}
20+
}
21+
22+
export interface RectangleMaskProps extends MaskProps {
23+
rectangleWidth: number;
24+
rectangleHeight: number;
25+
}
26+
27+
export interface RoundRectangleMaskProps extends RectangleMaskProps {
28+
borderRadius: number,
29+
}
30+
31+
export interface StarMaskProps extends MaskProps {
32+
numberOfPoints: number,
33+
radius: number,
34+
innerRadius?: number,
35+
}

src/clips/mask/rectangleMask.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Mask } from './mask';
2+
import { RectangleMaskProps } from './mask.types';
3+
4+
5+
6+
export class RectangleMask extends Mask {
7+
private _rectangleWidth: number;
8+
private _rectangleHeight: number;
9+
10+
public constructor(props: RectangleMaskProps) {
11+
super(props);
12+
13+
this._rectangleWidth = props.rectangleWidth;
14+
this._rectangleHeight = props.rectangleHeight;
15+
16+
this.rect(this.position.x, this.position.y, this._rectangleWidth, this._rectangleHeight);
17+
this.fill({ color: '#FFF' });
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Graphics} from "pixi.js";
2+
import { RoundRectangleMaskProps } from "./mask.types";
3+
4+
export class RoundRectangleMask extends Graphics {
5+
private _rectangleWidth: number;
6+
private _rectangleHeight: number;
7+
private _borderRadius: number;
8+
9+
10+
public constructor(props: RoundRectangleMaskProps) {
11+
super(props);
12+
13+
this._rectangleWidth = props.rectangleWidth;
14+
this._rectangleHeight = props.rectangleHeight;
15+
this._borderRadius = props.borderRadius;
16+
17+
this.roundRect(this.position.x, this.position.y, this._rectangleWidth, this._rectangleHeight, this._borderRadius)
18+
this.fill({color: '#FFF'})
19+
}
20+
}

src/clips/mask/starMask.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Graphics} from "pixi.js";
2+
import { StarMaskProps } from "./mask.types";
3+
import { Mask } from "./mask";
4+
5+
6+
7+
8+
export class StarMask extends Mask {
9+
10+
private _numberOfPoints: number;
11+
private _radius: number;
12+
private _innerRadius: number | undefined;
13+
public constructor(props: StarMaskProps){
14+
super(props);
15+
16+
this._numberOfPoints = props.numberOfPoints;
17+
this._radius = props.radius;
18+
this._innerRadius = props.innerRadius;
19+
20+
this.star(this.position.x, this.position.y, this._numberOfPoints, this._radius, this._innerRadius, this.rotation)
21+
this.fill({color: '#FFF'})
22+
}
23+
}

0 commit comments

Comments
 (0)