-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
CircleBrush.ts
140 lines (122 loc) · 3.38 KB
/
CircleBrush.ts
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
137
138
139
140
import { Color } from '../color/Color';
import type { Point } from '../Point';
import { Shadow } from '../Shadow';
import { Circle } from '../shapes/Circle';
import { Group } from '../shapes/Group';
import { getRandomInt } from '../util/internals';
import type { Canvas } from '../canvas/Canvas';
import { BaseBrush } from './BaseBrush';
import type { CircleBrushPoint } from './typedefs';
import { CENTER } from '../constants';
export class CircleBrush extends BaseBrush {
/**
* Width of a brush
* @type Number
* @default
*/
width = 10;
declare points: CircleBrushPoint[];
constructor(canvas: Canvas) {
super(canvas);
this.points = [];
}
/**
* Invoked inside on mouse down and mouse move
* @param {Point} pointer
*/
drawDot(pointer: Point) {
const point = this.addPoint(pointer),
ctx = this.canvas.contextTop;
this._saveAndTransform(ctx);
this.dot(ctx, point);
ctx.restore();
}
dot(ctx: CanvasRenderingContext2D, point: CircleBrushPoint) {
ctx.fillStyle = point.fill;
ctx.beginPath();
ctx.arc(point.x, point.y, point.radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
}
/**
* Invoked on mouse down
*/
onMouseDown(pointer: Point) {
this.points = [];
this.canvas.clearContext(this.canvas.contextTop);
this._setShadow();
this.drawDot(pointer);
}
/**
* Render the full state of the brush
* @private
*/
_render() {
const ctx = this.canvas.contextTop,
points = this.points;
this._saveAndTransform(ctx);
for (let i = 0; i < points.length; i++) {
this.dot(ctx, points[i]);
}
ctx.restore();
}
/**
* Invoked on mouse move
* @param {Point} pointer
*/
onMouseMove(pointer: Point) {
if (this.limitedToCanvasSize === true && this._isOutSideCanvas(pointer)) {
return;
}
if (this.needsFullRender()) {
this.canvas.clearContext(this.canvas.contextTop);
this.addPoint(pointer);
this._render();
} else {
this.drawDot(pointer);
}
}
/**
* Invoked on mouse up
*/
onMouseUp() {
const originalRenderOnAddRemove = this.canvas.renderOnAddRemove;
this.canvas.renderOnAddRemove = false;
const circles: Circle[] = [];
for (let i = 0; i < this.points.length; i++) {
const point = this.points[i],
circle = new Circle({
radius: point.radius,
left: point.x,
top: point.y,
originX: CENTER,
originY: CENTER,
fill: point.fill,
});
this.shadow && (circle.shadow = new Shadow(this.shadow));
circles.push(circle);
}
const group = new Group(circles, { canvas: this.canvas });
this.canvas.fire('before:path:created', { path: group });
this.canvas.add(group);
this.canvas.fire('path:created', { path: group });
this.canvas.clearContext(this.canvas.contextTop);
this._resetShadow();
this.canvas.renderOnAddRemove = originalRenderOnAddRemove;
this.canvas.requestRenderAll();
}
/**
* @param {Object} pointer
* @return {Point} Just added pointer point
*/
addPoint({ x, y }: Point) {
const pointerPoint: CircleBrushPoint = {
x,
y,
radius: getRandomInt(Math.max(0, this.width - 20), this.width + 20) / 2,
fill: new Color(this.color).setAlpha(getRandomInt(0, 100) / 100).toRgba(),
};
this.points.push(pointerPoint);
return pointerPoint;
}
}