@vnejs/plugins.canvas.mask 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.canvas.mask.contract";
3
+ import { LayerMask } from "./modules/mask.js";
4
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [LayerMask]);
@@ -0,0 +1,14 @@
1
+ import { Module } from "@vnejs/module";
2
+ import type { MaskPluginConstants, MaskPluginEvents, MaskPluginParams } from "../types.js";
3
+ import type { MaskChangePayload, MaskSetPayload, MaskState, MaskUnsetPayload } from "../utils/mask.js";
4
+ export declare class LayerMask extends Module<MaskPluginEvents, MaskPluginConstants, Record<string, never>, MaskPluginParams, MaskState> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onMaskSet: ({ layer, value, name, duration, args }?: MaskSetPayload) => Promise<unknown[]> | undefined;
8
+ onMaskUnset: ({ layer, duration }?: MaskUnsetPayload) => Promise<unknown[]> | undefined;
9
+ onMaskChange: ({ layer, duration, args }?: MaskChangePayload) => Promise<void> | undefined;
10
+ onStateSet: ({ [this.name]: state }?: Record<string, MaskState>) => Promise<void>;
11
+ onStateClear: () => Promise<(unknown[] | undefined)[]>;
12
+ emitLayerPropsUpdate: (layer: string, mask: string, duration: number, withTransitionChange?: boolean) => Promise<unknown[]> | undefined;
13
+ getDefaultState: () => MaskState;
14
+ }
@@ -0,0 +1,51 @@
1
+ import { Module } from "@vnejs/module";
2
+ import { interpolateArray } from "../utils/mask.js";
3
+ export class LayerMask extends Module {
4
+ name = "layer.mask";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.LAYER_MASK.SET, this.onMaskSet);
7
+ this.on(this.EVENTS.LAYER_MASK.UNSET, this.onMaskUnset);
8
+ this.on(this.EVENTS.LAYER_MASK.CHANGE, this.onMaskChange);
9
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
10
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
11
+ };
12
+ onMaskSet = ({ layer = "", value = "", name = "", duration = 0, args = [] } = {}) => {
13
+ const maskFn = this.PARAMS.LAYER_MASK.MASKS[name];
14
+ const maskValue = value || (maskFn ? maskFn(...args) : "");
15
+ this.state[layer] = { value: maskValue, name, args };
16
+ return this.emitLayerPropsUpdate(layer, this.state[layer].value, duration);
17
+ };
18
+ onMaskUnset = ({ layer = "", duration = 0 } = {}) => {
19
+ delete this.state[layer];
20
+ return this.emitLayerPropsUpdate(layer, "", duration);
21
+ };
22
+ onMaskChange = ({ layer = "", duration = 0, args = [] } = {}) => {
23
+ const layerState = this.state[layer];
24
+ if (!layerState)
25
+ return;
26
+ const maskFunc = this.PARAMS.LAYER_MASK.MASKS[layerState.name];
27
+ const applyMask = (maskArgs) => maskFunc(...maskArgs);
28
+ const initialTs = Date.now();
29
+ return new Promise((resolve) => {
30
+ const onTick = () => {
31
+ const currentTs = Date.now();
32
+ if (currentTs - initialTs >= duration) {
33
+ Object.assign(layerState, { value: applyMask(args), args });
34
+ return void this.emitLayerPropsUpdate(layer, layerState.value, 0, false)?.then(() => resolve());
35
+ }
36
+ const nextTick = () => requestAnimationFrame(onTick);
37
+ const startArgs = layerState.args.map(Number);
38
+ const endArgs = args.map(Number);
39
+ void this.emitLayerPropsUpdate(layer, applyMask(interpolateArray(startArgs, endArgs, currentTs - initialTs, duration)), 0, false)?.then(nextTick);
40
+ };
41
+ onTick();
42
+ });
43
+ };
44
+ onStateSet = async ({ [this.name]: state = {} } = {}) => {
45
+ await Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
46
+ await Promise.all(Object.keys(state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.SET, { ...state[layer], layer, duration: 0 })));
47
+ };
48
+ onStateClear = () => Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
49
+ emitLayerPropsUpdate = (layer, mask, duration, withTransitionChange = true) => this.emit(this.EVENTS.LAYER.PROPS_UPDATE, { layer, props: { mask }, duration, withTransitionChange });
50
+ getDefaultState = () => ({});
51
+ }
@@ -0,0 +1,7 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry } from "@vnejs/shared";
2
+ import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/plugins.canvas.mask.contract";
3
+ import type { PluginName as LayerPluginName, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/plugins.canvas.layer.contract";
4
+ import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/plugins.core.state.contract";
5
+ export type MaskPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<LayerPluginName, LayerSubscribeEvents> & Record<StatePluginName, StateSubscribeEvents>;
6
+ export type MaskPluginConstants = VnePluginConstantsMapRegistry & Record<PluginName, Constants>;
7
+ export type MaskPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ export type MaskLayerState = {
2
+ value: string;
3
+ name: string;
4
+ args: unknown[];
5
+ };
6
+ export type MaskState = Record<string, MaskLayerState>;
7
+ export type MaskSetPayload = {
8
+ layer?: string;
9
+ value?: string;
10
+ name?: string;
11
+ duration?: number;
12
+ args?: unknown[];
13
+ };
14
+ export type MaskUnsetPayload = {
15
+ layer?: string;
16
+ duration?: number;
17
+ };
18
+ export type MaskChangePayload = {
19
+ layer?: string;
20
+ duration?: number;
21
+ args?: unknown[];
22
+ };
23
+ export declare const interpolateArray: (startArray: number[], endArray: number[], currentTime: number, duration: number) => number[];
@@ -0,0 +1,6 @@
1
+ export const interpolateArray = (startArray, endArray, currentTime, duration) => {
2
+ if (duration === 0 || currentTime >= duration)
3
+ return endArray;
4
+ const delta = Math.min(1, Math.max(0, currentTime / duration));
5
+ return startArray.map((startValue, index) => startValue + (endArray[index] - startValue) * delta);
6
+ };
package/package.json CHANGED
@@ -1,17 +1,44 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.canvas.mask",
3
- "version": "0.1.1",
4
- "main": "index.js",
3
+ "version": "0.1.2",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
5
20
  "scripts": {
6
21
  "test": "echo \"Error: no test specified\" && exit 1",
22
+ "build": "npx @vnejs/monorepo package",
7
23
  "publish:major:plugin": "npm run publish:major",
8
24
  "publish:minor:plugin": "npm run publish:minor",
9
25
  "publish:patch:plugin": "npm run publish:patch",
10
- "publish:major": "npm version major && npm publish --access public",
11
- "publish:minor": "npm version minor && npm publish --access public",
12
- "publish:patch": "npm version patch && npm publish --access public"
26
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
27
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
28
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
13
29
  },
14
30
  "author": "",
15
31
  "license": "ISC",
16
- "description": ""
32
+ "dependencies": {
33
+ "@vnejs/plugins.canvas.mask.contract": "~0.0.1"
34
+ },
35
+ "peerDependencies": {
36
+ "@vnejs/module": "~0.0.1",
37
+ "@vnejs/plugins.canvas.layer.contract": "~0.0.1",
38
+ "@vnejs/plugins.core.state.contract": "~0.0.1",
39
+ "@vnejs/shared": "~0.0.9"
40
+ },
41
+ "devDependencies": {
42
+ "@vnejs/configs.ts-common": "~0.0.1"
43
+ }
17
44
  }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.canvas.mask.contract";
3
+
4
+ import { LayerMask } from "./modules/mask.js";
5
+
6
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [LayerMask]);
@@ -0,0 +1,74 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ import type { MaskPluginConstants, MaskPluginEvents, MaskPluginParams } from "../types.js";
4
+ import type { MaskChangePayload, MaskSetPayload, MaskState, MaskUnsetPayload } from "../utils/mask.js";
5
+ import { interpolateArray } from "../utils/mask.js";
6
+
7
+ export class LayerMask extends Module<MaskPluginEvents, MaskPluginConstants, Record<string, never>, MaskPluginParams, MaskState> {
8
+ name = "layer.mask";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LAYER_MASK.SET, this.onMaskSet);
12
+ this.on(this.EVENTS.LAYER_MASK.UNSET, this.onMaskUnset);
13
+ this.on(this.EVENTS.LAYER_MASK.CHANGE, this.onMaskChange);
14
+
15
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
16
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
17
+ };
18
+
19
+ onMaskSet = ({ layer = "", value = "", name = "", duration = 0, args = [] }: MaskSetPayload = {}) => {
20
+ const maskFn = this.PARAMS.LAYER_MASK.MASKS[name as keyof typeof this.PARAMS.LAYER_MASK.MASKS];
21
+ const maskValue = value || (maskFn ? (maskFn as (...maskArgs: unknown[]) => string)(...args) : "");
22
+
23
+ this.state[layer] = { value: maskValue!, name, args };
24
+
25
+ return this.emitLayerPropsUpdate(layer, this.state[layer].value, duration);
26
+ };
27
+
28
+ onMaskUnset = ({ layer = "", duration = 0 }: MaskUnsetPayload = {}) => {
29
+ delete this.state[layer];
30
+
31
+ return this.emitLayerPropsUpdate(layer, "", duration);
32
+ };
33
+
34
+ onMaskChange = ({ layer = "", duration = 0, args = [] }: MaskChangePayload = {}) => {
35
+ const layerState = this.state[layer];
36
+ if (!layerState) return;
37
+
38
+ const maskFunc = this.PARAMS.LAYER_MASK.MASKS[layerState.name as keyof typeof this.PARAMS.LAYER_MASK.MASKS];
39
+ const applyMask = (maskArgs: unknown[]) => (maskFunc as (...maskArgs: unknown[]) => string)(...maskArgs);
40
+ const initialTs = Date.now();
41
+
42
+ return new Promise<void>((resolve) => {
43
+ const onTick = () => {
44
+ const currentTs = Date.now();
45
+
46
+ if (currentTs - initialTs >= duration) {
47
+ Object.assign(layerState, { value: applyMask(args), args });
48
+
49
+ return void this.emitLayerPropsUpdate(layer, layerState.value, 0, false)?.then(() => resolve());
50
+ }
51
+
52
+ const nextTick = () => requestAnimationFrame(onTick);
53
+ const startArgs = layerState.args.map(Number);
54
+ const endArgs = args.map(Number);
55
+
56
+ void this.emitLayerPropsUpdate(layer, applyMask(interpolateArray(startArgs, endArgs, currentTs - initialTs, duration)), 0, false)?.then(nextTick);
57
+ };
58
+
59
+ onTick();
60
+ });
61
+ };
62
+
63
+ onStateSet = async ({ [this.name]: state = {} }: Record<string, MaskState> = {}) => {
64
+ await Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
65
+ await Promise.all(Object.keys(state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.SET, { ...state[layer], layer, duration: 0 })));
66
+ };
67
+
68
+ onStateClear = () => Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
69
+
70
+ emitLayerPropsUpdate = (layer: string, mask: string, duration: number, withTransitionChange = true) =>
71
+ this.emit(this.EVENTS.LAYER.PROPS_UPDATE, { layer, props: { mask }, duration, withTransitionChange });
72
+
73
+ getDefaultState = (): MaskState => ({});
74
+ }
package/src/types.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry } from "@vnejs/shared";
2
+ import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/plugins.canvas.mask.contract";
3
+ import type { PluginName as LayerPluginName, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/plugins.canvas.layer.contract";
4
+ import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/plugins.core.state.contract";
5
+
6
+ export type MaskPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<LayerPluginName, LayerSubscribeEvents> & Record<StatePluginName, StateSubscribeEvents>;
7
+
8
+ export type MaskPluginConstants = VnePluginConstantsMapRegistry & Record<PluginName, Constants>;
9
+
10
+ export type MaskPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params>;
@@ -0,0 +1,34 @@
1
+ export type MaskLayerState = {
2
+ value: string;
3
+ name: string;
4
+ args: unknown[];
5
+ };
6
+
7
+ export type MaskState = Record<string, MaskLayerState>;
8
+
9
+ export type MaskSetPayload = {
10
+ layer?: string;
11
+ value?: string;
12
+ name?: string;
13
+ duration?: number;
14
+ args?: unknown[];
15
+ };
16
+
17
+ export type MaskUnsetPayload = {
18
+ layer?: string;
19
+ duration?: number;
20
+ };
21
+
22
+ export type MaskChangePayload = {
23
+ layer?: string;
24
+ duration?: number;
25
+ args?: unknown[];
26
+ };
27
+
28
+ export const interpolateArray = (startArray: number[], endArray: number[], currentTime: number, duration: number) => {
29
+ if (duration === 0 || currentTime >= duration) return endArray;
30
+
31
+ const delta = Math.min(1, Math.max(0, currentTime / duration));
32
+
33
+ return startArray.map((startValue, index) => startValue + (endArray[index]! - startValue) * delta);
34
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }
package/const/const.js DELETED
@@ -1 +0,0 @@
1
- export const EXEC_ACTIONS = { SET: "set", UNSET: "unset", CHANGE: "change" };
package/const/events.js DELETED
@@ -1,5 +0,0 @@
1
- export const SUBSCRIBE_EVENTS = {
2
- SET: "vne:layer_mask:set",
3
- UNSET: "vne:layer_mask:unset",
4
- CHANGE: "vne:layer_mask:change",
5
- };
package/const/params.js DELETED
@@ -1,3 +0,0 @@
1
- export const MASKS = {
2
- blink: (width = 50, height = 50) => `radial-gradient(ellipse ${width}vw ${height}vh at 50% 50%, black 50%, transparent 100%)`,
3
- };
package/index.js DELETED
@@ -1,9 +0,0 @@
1
- import { regPlugin } from "@vnejs/shared";
2
-
3
- import * as constants from "./const/const";
4
- import { SUBSCRIBE_EVENTS } from "./const/events";
5
- import * as params from "./const/params";
6
-
7
- import { LayerMask } from "./modules/mask";
8
-
9
- regPlugin("LAYER_MASK", { constants, events: SUBSCRIBE_EVENTS, params }, [LayerMask]);
package/modules/mask.js DELETED
@@ -1,62 +0,0 @@
1
- import { Module } from "@vnejs/module";
2
-
3
- const interpolateArray = (startArray, endArray, currentTime, duration) => {
4
- if (duration === 0 || currentTime >= duration) return endArray;
5
-
6
- const delta = Math.min(1, Math.max(0, currentTime / duration));
7
-
8
- return startArray.map((startValue, index) => startValue + (endArray[index] - startValue) * delta);
9
- };
10
-
11
- export class LayerMask extends Module {
12
- name = "layer.mask";
13
-
14
- subscribe = () => {
15
- this.on(this.EVENTS.LAYER_MASK.SET, this.onMaskSet);
16
- this.on(this.EVENTS.LAYER_MASK.UNSET, this.onMaskUnset);
17
- this.on(this.EVENTS.LAYER_MASK.CHANGE, this.onMaskChange);
18
-
19
- this.on(this.EVENTS.STATE.SET, this.onStateSet);
20
- this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
21
- };
22
-
23
- onMaskSet = ({ layer = "", value = "", name = "", duration = 0, args = [] } = {}) => {
24
- this.state[layer] = { value: value || this.PARAMS.LAYER_MASK.MASKS[name](...args), name, args };
25
-
26
- return this.emitLayerPropsUpdate(layer, this.state[layer].value, duration);
27
- };
28
- onMaskUnset = ({ layer = "", duration = 0 } = {}) => {
29
- delete this.state[layer];
30
-
31
- return this.emitLayerPropsUpdate(layer, "", duration);
32
- };
33
- onMaskChange = ({ layer = "", duration = 0, args = [] } = {}) => {
34
- const [maskFunc, initialTs] = [this.PARAMS.LAYER_MASK.MASKS[this.state[layer].name], new Date().getTime()];
35
-
36
- return new Promise((resolve) => {
37
- const onTick = () => {
38
- const currentTs = new Date().getTime();
39
-
40
- if (currentTs - initialTs >= duration) {
41
- Object.assign(this.state[layer], { value: maskFunc(...args), args });
42
- return this.emitLayerPropsUpdate(layer, this.state[layer].value, 0, false).then(resolve);
43
- }
44
-
45
- const nextTick = () => requestAnimationFrame(onTick);
46
-
47
- this.emitLayerPropsUpdate(layer, maskFunc(...interpolateArray(this.state[layer].args, args, currentTs - initialTs, duration)), 0, false).then(nextTick);
48
- };
49
-
50
- onTick();
51
- });
52
- };
53
-
54
- onStateSet = async ({ [this.name]: state = {} } = {}) => {
55
- await Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
56
- await Promise.all(Object.keys(state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.SET, { ...state[layer], layer, duration: 0 })));
57
- };
58
- onStateClear = () => Promise.all(Object.keys(this.state).map((layer) => this.emit(this.EVENTS.LAYER_MASK.UNSET, { layer, duration: 0 })));
59
-
60
- emitLayerPropsUpdate = (layer, mask, duration, withTransitionChange = true) =>
61
- this.emit(this.EVENTS.LAYER.PROPS_UPDATE, { layer, props: { mask }, duration, withTransitionChange });
62
- }