Brush.js
996 Bytes
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
import ComponentInterface from "./ComponentInterface";
import Color from "./Color";
import { fabric } from "fabric";
class Brush extends ComponentInterface {
constructor(fabricObj) {
super(fabricObj.path.length);
this.color = new Color(fabricObj.stroke);
this.paths = fabricObj.path;
this.size = fabricObj.strokeWidth;
this.position = {
top: fabricObj.top,
left: fabricObj.left,
};
}
getCurrentFabricObject() {
const paths = this.paths.filter((_, i) => i < this.state.current);
if (paths.length > 0) {
const popCount = paths[paths.length - 1].length - 3;
for (let i = 0; i < popCount; i++) {
paths[paths.length - 1].pop();
}
paths[paths.length - 1][0] = "L";
}
return new fabric.Path(paths, {
top: this.position.top,
left: this.position.left,
stroke: this.color.getRgba(),
strokeWidth: this.size,
});
}
next() {
return this.addState(10);
}
}
export default Brush;