@vnejs/plugins.canvas.mask 0.1.1

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.
package/const/const.js ADDED
@@ -0,0 +1 @@
1
+ export const EXEC_ACTIONS = { SET: "set", UNSET: "unset", CHANGE: "change" };
@@ -0,0 +1,5 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SET: "vne:layer_mask:set",
3
+ UNSET: "vne:layer_mask:unset",
4
+ CHANGE: "vne:layer_mask:change",
5
+ };
@@ -0,0 +1,3 @@
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 ADDED
@@ -0,0 +1,9 @@
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]);
@@ -0,0 +1,62 @@
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
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.canvas.mask",
3
+ "version": "0.1.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "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"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }