/
clip.js
46 lines (41 loc) · 915 Bytes
/
clip.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
import Layer from './base.js'
/**
* Clip layer
*/
export default class ClipLayer extends Layer {
/**
* @param {object} config object
* @param {number | string} [config.min] Minimum value
* @param {number | string} [config.max] Maximum value
*/
constructor({ min = null, max = null, ...rest }) {
super(rest)
this._min = min
this._max = max
}
calc(x) {
const min = typeof this._min === 'string' ? this.graph.getNode(this._min).outputValue.toScaler() : this._min
const max = typeof this._max === 'string' ? this.graph.getNode(this._max).outputValue.toScaler() : this._max
const o = x.copy()
o.map(v => {
if (min !== null && v < min) {
return min
} else if (max !== null && v > max) {
return max
}
return v
})
return o
}
grad(bo) {
return bo
}
toObject() {
return {
type: 'clip',
min: this._min,
max: this._max,
}
}
}
ClipLayer.registLayer()