forked from chanind/hanzi-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderState.js
136 lines (124 loc) · 4.09 KB
/
RenderState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { copyAndMergeDeep, colorStringToVals } = require('./utils');
function RenderState(character, options, onStateChange) {
this._onStateChange = onStateChange;
this._mutationChains = [];
this.state = {
options: {
drawingFadeDuration: options.drawingFadeDuration,
drawingWidth: options.drawingWidth,
drawingColor: colorStringToVals(options.drawingColor),
strokeColor: colorStringToVals(options.strokeColor),
outlineColor: colorStringToVals(options.outlineColor),
radicalColor: colorStringToVals(options.radicalColor || options.strokeColor),
highlightColor: colorStringToVals(options.highlightColor),
},
character: {
main: {
opacity: options.showCharacter ? 1 : 0,
strokes: {},
},
outline: {
opacity: options.showOutline ? 1 : 0,
strokes: {},
},
highlight: {
opacity: 1,
strokes: {},
},
},
userStrokes: null,
};
for (let i = 0; i < character.strokes.length; i++) {
this.state.character.main.strokes[i] = {
opacity: 1,
displayPortion: 1,
};
this.state.character.outline.strokes[i] = {
opacity: 1,
displayPortion: 1,
};
this.state.character.highlight.strokes[i] = {
opacity: 0,
displayPortion: 1,
};
}
}
RenderState.prototype.updateState = function(stateChanges) {
const nextState = copyAndMergeDeep(this.state, stateChanges);
this._onStateChange(nextState, this.state);
this.state = nextState;
};
RenderState.prototype.run = function(mutations, options = {}) {
const scopes = mutations.map(mut => mut.scope).filter(x => x);
this.cancelMutations(scopes);
return new Promise(resolve => {
const mutationChain = {
_isActive: true,
_index: 0,
_resolve: resolve,
_mutations: mutations,
_loop: options.loop,
_scopes: scopes,
};
this._mutationChains.push(mutationChain);
this._run(mutationChain);
});
};
RenderState.prototype._run = function(mutationChain) {
if (!mutationChain._isActive) return;
const mutations = mutationChain._mutations;
if (mutationChain._index >= mutations.length) {
if (mutationChain._loop) {
mutationChain._index = 0; // eslint-disable-line no-param-reassign
} else {
mutationChain._isActive = false; // eslint-disable-line no-param-reassign
this._mutationChains = this._mutationChains.filter(chain => chain !== mutationChain);
// The chain is done - resolve the promise to signal it finished successfully
mutationChain._resolve({ canceled: false });
return;
}
}
const activeMutation = mutationChain._mutations[mutationChain._index];
activeMutation.run(this).then(() => {
if (mutationChain._isActive) {
mutationChain._index++; // eslint-disable-line no-param-reassign
this._run(mutationChain);
}
});
};
RenderState.prototype._getActiveMutations = function() {
return this._mutationChains.map(chain => {
return chain._mutations[chain._index];
});
};
RenderState.prototype.pauseAll = function() {
this._getActiveMutations().forEach(mutation => mutation.pause());
};
RenderState.prototype.resumeAll = function() {
this._getActiveMutations().forEach(mutation => mutation.resume());
};
RenderState.prototype.cancelMutations = function(scopes) {
this._mutationChains.forEach(chain => {
chain._scopes.forEach(chainScope => {
scopes.forEach(scope => {
if (chainScope.indexOf(scope) >= 0 || scope.indexOf(chainScope) >= 0) {
this._cancelMutationChain(chain);
}
});
});
});
};
RenderState.prototype.cancelAll = function() {
this.cancelMutations(['']);
};
RenderState.prototype._cancelMutationChain = function(mutationChain) {
mutationChain._isActive = false; // eslint-disable-line no-param-reassign
for (let i = mutationChain._index; i < mutationChain._mutations.length; i++) {
mutationChain._mutations[i].cancel(this);
}
if (mutationChain._resolve) {
mutationChain._resolve({ canceled: true });
}
this._mutationChains = this._mutationChains.filter(chain => chain !== mutationChain);
};
module.exports = RenderState;