-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
Surface.ts
286 lines (247 loc) · 7.18 KB
/
Surface.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import {ContainerConfig} from 'konva/lib/Container';
import {getOriginDelta, Origin, Size} from '../types';
import {CanvasHelper} from '../helpers';
import {easeOutExpo, linear, tween} from '../tweening';
import {GetSet} from 'konva/lib/types';
import {KonvaNode, threadable} from '../decorators';
import {getset} from '../decorators/getset';
import {Node} from 'konva/lib/Node';
import {Reference} from '../utils';
import {Group} from 'konva/lib/Group';
import {Shape} from 'konva/lib/Shape';
import {Canvas} from 'konva/lib/Canvas';
export interface SurfaceMask {
width: number;
height: number;
radius: number;
color: string;
}
export interface CircleMask {
radius: number;
x: number;
y: number;
}
export interface SurfaceConfig extends ContainerConfig {
ref?: Reference<Surface>;
radius?: number;
circleMask?: CircleMask;
background?: string;
child?: Node;
rescaleChild?: boolean;
shadow?: boolean;
overflow?: boolean;
}
@KonvaNode()
export class Surface extends Group {
@getset(8)
public radius: GetSet<SurfaceConfig['radius'], this>;
@getset('#00000000')
public background: GetSet<SurfaceConfig['background'], this>;
@getset(null)
public child: GetSet<SurfaceConfig['child'], this>;
@getset(true)
public rescaleChild: GetSet<SurfaceConfig['rescaleChild'], this>;
@getset(false)
public shadow: GetSet<SurfaceConfig['shadow'], this>;
@getset(false)
public overflow: GetSet<SurfaceConfig['overflow'], this>;
private surfaceMask: SurfaceMask | null = null;
private circleMask: CircleMask | null = null;
private layoutData: Size;
private rippleTween = 0;
public constructor(config?: SurfaceConfig) {
super(config);
this.markDirty();
}
public setChild(value: Shape | Group): this {
this.attrs.child?.remove();
this.attrs.child = value;
if (value) {
this.add(value);
}
this.markDirty();
return this;
}
public getChild<T extends Node>(): T {
return <T>this.attrs.child;
}
public setCircleMask(value: CircleMask | null): this {
this.circleMask = value;
return this;
}
public getCircleMask(): CircleMask | null {
return this.circleMask ?? null;
}
public clone(obj?: unknown): this {
const child = this.child();
this.child(null);
const clone: this = Node.prototype.clone.call(this, obj);
this.child(child);
if (child) {
clone.setChild(child.clone());
}
this.getChildren().forEach(node => {
if (node !== child) {
clone.add(node.clone());
}
});
return clone;
}
public getLayoutSize(): Size {
return {
width: this.surfaceMask?.width ?? this.layoutData?.width ?? 0,
height: this.surfaceMask?.height ?? this.layoutData?.height ?? 0,
};
}
/**
* @deprecated Use {@link ripple} instead.
*/
public doRipple() {
return this.ripple();
}
@threadable()
public *ripple() {
if (this.surfaceMask) return;
yield* tween(1, value => {
this.rippleTween = value;
});
this.rippleTween = 0;
}
public setMask(data: SurfaceMask) {
if (data === null) {
this.surfaceMask = null;
this.markDirty();
return;
}
const child = this.child();
const contentSize = child.getLayoutSize();
const contentMargin = child.getMargin();
const scale = Math.min(
1,
data.width / (contentSize.width + contentMargin.x),
);
this.surfaceMask = data;
if (this.rescaleChild()) {
child.scaleX(scale);
child.scaleY(scale);
}
child.position(getOriginDelta(data, Origin.Middle, child.getOrigin()));
this.markDirty();
}
public getMask(): SurfaceMask {
return {
...this.layoutData,
radius: this.radius(),
color: this.background(),
};
}
public recalculateLayout() {
if (this.surfaceMask) return;
this.layoutData ??= {
width: 0,
height: 0,
};
const child = this.child();
if (child) {
const size = child.getLayoutSize();
const margin = child.getMargin();
const scale = child.getAbsoluteScale(this);
const padding = this.getPadd();
this.layoutData = {
width: (size.width + margin.x + padding.x) * scale.x,
height: (size.height + margin.y + padding.y) * scale.y,
};
const offset = child.getOriginDelta(Origin.Middle);
child.position({
x: -offset.x,
y: -offset.y,
});
}
super.recalculateLayout();
}
public _drawChildren(drawMethod: string, canvas: Canvas, top: Node): void {
const context = canvas && canvas.getContext();
context.save();
const transform = this.getAbsoluteTransform(top);
let m = transform.getMatrix();
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
const opacity = this.getAbsoluteOpacity();
const size = this.surfaceMask ?? this.layoutData;
const radius = this.surfaceMask?.radius ?? this.radius();
if (this.rippleTween > 0) {
const width = size.width + easeOutExpo(this.rippleTween, 0, 100);
const height = size.height + easeOutExpo(this.rippleTween, 0, 100);
const rippleRadius = radius + easeOutExpo(this.rippleTween, 0, 50);
context.save();
context._context.fillStyle = this.surfaceMask?.color ?? this.background();
context._context.globalAlpha = linear(this.rippleTween, opacity / 2, 0);
CanvasHelper.roundRect(
context._context,
-width / 2,
-height / 2,
width,
height,
rippleRadius,
);
context._context.fill();
context.restore();
}
if (this.circleMask) {
context._context.beginPath();
context._context.arc(
this.circleMask.x,
this.circleMask.y,
this.circleMask.radius,
0,
Math.PI * 2,
);
context._context.closePath();
context._context.clip();
}
context.save();
context._context.fillStyle = this.surfaceMask?.color ?? this.background();
context._context.globalAlpha = opacity;
if (this.shadow()) {
context._context.shadowColor = 'rgba(0, 0, 0, 0.32)';
context._context.shadowOffsetY = 10;
context._context.shadowBlur = 40;
}
const path = CanvasHelper.roundRectPath(
new Path2D(),
-size.width / 2,
-size.height / 2,
size.width,
size.height,
radius,
);
context._context.fill(path);
context.restore();
if (!this.overflow() || this.surfaceMask) {
context._context.clip(path);
}
m = transform.copy().invert().getMatrix();
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
super._drawChildren(drawMethod, canvas, top);
context.restore();
}
public getAbsoluteCircleMask(custom?: SurfaceConfig): CircleMask {
const mask = custom?.circleMask ?? this.circleMask ?? null;
if (mask === null) return null;
const size = this.getLayoutSize();
const position = {
x: (size.width * mask.x) / 2,
y: (size.height * mask.y) / 2,
};
const farthestEdge = {
x: Math.abs(position.x) + size.width / 2,
y: Math.abs(position.y) + size.height / 2,
};
const distance = Math.sqrt(
farthestEdge.x * farthestEdge.x + farthestEdge.y * farthestEdge.y,
);
return {
...position,
radius: distance * mask.radius,
};
}
}