Skip to content

Commit 54a812c

Browse files
committed
updated masks
1 parent cf24fe2 commit 54a812c

File tree

11 files changed

+142
-110
lines changed

11 files changed

+142
-110
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import {Graphics} from "pixi.js";
1+
import { Mask } from "./mask";
2+
import { CircleMaskProps } from "./mask.types";
23

4+
/**
5+
* A circular mask of a given radius
6+
*/
7+
export class CircleMask extends Mask {
38

4-
export interface CircleMaskProps {
5-
radius?: number,
6-
}
7-
8-
export class CircleMask extends Graphics {
9+
private _radius: number;
910

10-
public radius = 30
1111
public constructor(props: CircleMaskProps){
12-
super()
12+
super(props);
1313

14-
Object.assign(this, props);
14+
this._radius = props.radius;
1515

16-
this.circle(this.position.x + this.radius, this.position.y + this.radius, this.radius)
16+
this.circle(this.position.x, this.position.y, this._radius)
1717
this.fill({color: '#FFF'})
1818
}
1919
}

src/clips/mask/ellipseMask.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
import {Graphics} from "pixi.js";
1+
import { EllipseMaskProps } from './mask.types';
2+
import { Mask } from './mask';
23

4+
export class EllipseMask extends Mask {
5+
private _radius: { x: number; y: number };
36

4-
export interface ellipseMaskProps {
5-
radius: {
6-
x?: number,
7-
y?: number,
8-
}
9-
}
10-
11-
export class ellipseMask extends Graphics {
12-
13-
public radius = {
14-
x: 30,
15-
y: 30
16-
}
17-
18-
public constructor(props: ellipseMaskProps){
19-
super()
7+
public constructor(props: EllipseMaskProps) {
8+
super(props);
209

21-
Object.assign(this, props);
10+
this._radius = props.radius;
2211

23-
this.ellipse(this.position.x + (this.radius.x /2), this.position.y + (this.radius.y /2), this.radius.x /2, this.radius.y /2 )
24-
this.fill({color: '#FFF'})
25-
}
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+
}
2620
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Graphics } from "pixi.js";
2+
import type { MaskProps } from "./mask.types";
3+
4+
/**
5+
* A mask is a clip that is used to mask another visual clip.
6+
*/
7+
export class Mask extends Graphics {
8+
constructor(props: MaskProps) {
9+
super();
10+
this.position = props.position ?? {x: 0, y: 0};
11+
}
12+
}

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: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
1-
import {Graphics} from "pixi.js";
2-
import { int } from "../../types";
1+
import { Mask } from './mask';
2+
import { RectangleMaskProps } from './mask.types';
33

44

5-
export interface rectangleMaskProps {
6-
_width?: number,
7-
_height?: number,
8-
}
9-
10-
export class rectangleMask extends Graphics {
11-
12-
public constructor(props: rectangleMaskProps) {
13-
super()
14-
15-
Object.assign(this, props);
16-
17-
this.rect(this.position.x, this.position.y, this._width, this._height)
18-
this.fill({color: '#FFF'})
19-
}
20-
21-
private _width = 100;
22-
private _height = 100;
23-
24-
set width(val: int){
25-
this._width = val
26-
}
275

28-
get width(){
29-
return this._width;
30-
}
6+
export class RectangleMask extends Mask {
7+
private _rectangleWidth: number;
8+
private _rectangleHeight: number;
319

32-
set height(val: int){
33-
this._height = val
34-
}
10+
public constructor(props: RectangleMaskProps) {
11+
super(props);
12+
13+
this._rectangleWidth = props.rectangleWidth;
14+
this._rectangleHeight = props.rectangleHeight;
3515

36-
get height(){
37-
return this._height;
38-
}
16+
this.rect(this.position.x, this.position.y, this._rectangleWidth, this._rectangleHeight);
17+
this.fill({ color: '#FFF' });
18+
}
3919
}
Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
import {Graphics} from "pixi.js";
2-
import { int } from "../../types";
2+
import { RoundRectangleMaskProps } from "./mask.types";
33

4+
export class RoundRectangleMask extends Graphics {
5+
private _rectangleWidth: number;
6+
private _rectangleHeight: number;
7+
private _borderRadius: number;
48

5-
export interface roundRectangleMaskProps {
6-
_width?: number,
7-
_height?: number,
8-
radius?: number,
9-
}
10-
11-
export class roundRectangleMask extends Graphics {
12-
13-
public radius = 50
149

15-
public constructor(props: roundRectangleMaskProps) {
16-
super()
10+
public constructor(props: RoundRectangleMaskProps) {
11+
super(props);
1712

18-
Object.assign(this, props);
13+
this._rectangleWidth = props.rectangleWidth;
14+
this._rectangleHeight = props.rectangleHeight;
15+
this._borderRadius = props.borderRadius;
1916

20-
this.roundRect(this.position.x, this.position.y, this._width, this._height, this.radius)
17+
this.roundRect(this.position.x, this.position.y, this._rectangleWidth, this._rectangleHeight, this._borderRadius)
2118
this.fill({color: '#FFF'})
2219
}
23-
24-
private _width = 100;
25-
private _height = 100;
26-
27-
set width(val: int){
28-
this._width = val
29-
}
30-
31-
get width(){
32-
return this._width;
33-
}
34-
35-
set height(val: int){
36-
this._height = val
37-
}
38-
39-
get height(){
40-
return this._height;
41-
}
4220
}

src/clips/mask/starMask.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import {Graphics} from "pixi.js";
2+
import { StarMaskProps } from "./mask.types";
3+
import { Mask } from "./mask";
4+
25

36

4-
export interface StarMaskProps {
5-
points?: number,
6-
radius?: number,
7-
InnerRadius?: number,
8-
}
97

10-
export class StarMask extends Graphics {
8+
export class StarMask extends Mask {
119

12-
public points = 5
13-
public radius = 30
14-
public InnerRadius = 0
10+
private _numberOfPoints: number;
11+
private _radius: number;
12+
private _innerRadius: number | undefined;
1513
public constructor(props: StarMaskProps){
16-
super()
14+
super(props);
1715

18-
Object.assign(this, props);
16+
this._numberOfPoints = props.numberOfPoints;
17+
this._radius = props.radius;
18+
this._innerRadius = props.innerRadius;
1919

20-
this.star(this.position.x + this.radius, this.position.y + this.radius, this.points, this.radius, this.InnerRadius, this.rotation)
20+
this.star(this.position.x, this.position.y, this._numberOfPoints, this._radius, this._innerRadius, this.rotation)
2121
this.fill({color: '#FFF'})
2222
}
2323
}

0 commit comments

Comments
 (0)