-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
SprayBrush.ts
219 lines (188 loc) · 4.98 KB
/
SprayBrush.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Point } from '../Point';
import { Group } from '../shapes/Group';
import { Shadow } from '../Shadow';
import { Rect } from '../shapes/Rect';
import { getRandomInt } from '../util/internals';
import type { Canvas } from '../canvas/Canvas';
import { BaseBrush } from './BaseBrush';
export type SprayBrushPoint = {
x: number;
y: number;
width: number;
opacity: number;
};
/**
*
* @param rects
* @returns
*/
function getUniqueRects(rects: Rect[]) {
const uniqueRects: Record<string, boolean> = {};
const uniqueRectsArray: Rect[] = [];
for (let i = 0, key: string; i < rects.length; i++) {
key = `${rects[i].left}${rects[i].top}`;
if (!uniqueRects[key]) {
uniqueRects[key] = true;
uniqueRectsArray.push(rects[i]);
}
}
return uniqueRectsArray;
}
export class SprayBrush extends BaseBrush {
/**
* Width of a spray
* @type Number
* @default
*/
width = 10;
/**
* Density of a spray (number of dots per chunk)
* @type Number
* @default
*/
density = 20;
/**
* Width of spray dots
* @type Number
* @default
*/
dotWidth = 1;
/**
* Width variance of spray dots
* @type Number
* @default
*/
dotWidthVariance = 1;
/**
* Whether opacity of a dot should be random
* @type Boolean
* @default
*/
randomOpacity = false;
/**
* Whether overlapping dots (rectangles) should be removed (for performance reasons)
* @type Boolean
* @default
*/
optimizeOverlapping = true;
private declare sprayChunks: SprayBrushPoint[][];
private declare sprayChunk: SprayBrushPoint[];
/**
* Constructor
* @param {Canvas} canvas
* @return {SprayBrush} Instance of a spray brush
*/
constructor(canvas: Canvas) {
super(canvas);
this.sprayChunks = [];
this.sprayChunk = [];
}
/**
* Invoked on mouse down
* @param {Point} pointer
*/
onMouseDown(pointer: Point) {
this.sprayChunks = [];
this.canvas.clearContext(this.canvas.contextTop);
this._setShadow();
this.addSprayChunk(pointer);
this.renderChunck(this.sprayChunk);
}
/**
* Invoked on mouse move
* @param {Point} pointer
*/
onMouseMove(pointer: Point) {
if (this.limitedToCanvasSize === true && this._isOutSideCanvas(pointer)) {
return;
}
this.addSprayChunk(pointer);
this.renderChunck(this.sprayChunk);
}
/**
* Invoked on mouse up
*/
onMouseUp() {
const originalRenderOnAddRemove = this.canvas.renderOnAddRemove;
this.canvas.renderOnAddRemove = false;
const rects: Rect[] = [];
for (let i = 0; i < this.sprayChunks.length; i++) {
const sprayChunk = this.sprayChunks[i];
for (let j = 0; j < sprayChunk.length; j++) {
const chunck = sprayChunk[j];
const rect = new Rect({
width: chunck.width,
height: chunck.width,
left: chunck.x + 1,
top: chunck.y + 1,
originX: 'center',
originY: 'center',
fill: this.color,
});
rects.push(rect);
}
}
const group = new Group(
this.optimizeOverlapping ? getUniqueRects(rects) : rects,
{
objectCaching: true,
layout: 'fixed',
subTargetCheck: false,
interactive: false,
}
);
this.shadow && group.set('shadow', new Shadow(this.shadow));
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();
}
renderChunck(sprayChunck: SprayBrushPoint[]) {
const ctx = this.canvas.contextTop;
ctx.fillStyle = this.color;
this._saveAndTransform(ctx);
for (let i = 0; i < sprayChunck.length; i++) {
const point = sprayChunck[i];
ctx.globalAlpha = point.opacity;
ctx.fillRect(point.x, point.y, point.width, point.width);
}
ctx.restore();
}
/**
* Render all spray chunks
*/
_render() {
const ctx = this.canvas.contextTop;
ctx.fillStyle = this.color;
this._saveAndTransform(ctx);
for (let i = 0; i < this.sprayChunks.length; i++) {
this.renderChunck(this.sprayChunks[i]);
}
ctx.restore();
}
/**
* @param {Point} pointer
*/
addSprayChunk(pointer: Point) {
this.sprayChunk = [];
const radius = this.width / 2;
for (let i = 0; i < this.density; i++) {
this.sprayChunk.push({
x: getRandomInt(pointer.x - radius, pointer.x + radius),
y: getRandomInt(pointer.y - radius, pointer.y + radius),
width: this.dotWidthVariance
? getRandomInt(
// bottom clamp width to 1
Math.max(1, this.dotWidth - this.dotWidthVariance),
this.dotWidth + this.dotWidthVariance
)
: this.dotWidth,
opacity: this.randomOpacity ? getRandomInt(0, 100) / 100 : 1,
});
}
this.sprayChunks.push(this.sprayChunk);
}
}