-
Notifications
You must be signed in to change notification settings - Fork 18
/
hit-test-rectangle.ts
64 lines (62 loc) · 1.61 KB
/
hit-test-rectangle.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
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { EShape } from "../e-shape";
import { EShapeStrokeSide } from "../e-shape-stroke-side";
import { hitTestBBox } from "./hit-test-bbox";
export const hitTestRectangle = (
shape: EShape,
x: number,
y: number,
ax: number,
ay: number,
sw: number,
ss: number
): boolean => {
if (hitTestBBox(x, y, ax, ay)) {
const fill = shape.fill;
if (fill.enable) {
return true;
} else {
const strokeSide = shape.stroke.side;
if (0 < sw && strokeSide !== EShapeStrokeSide.NONE) {
const s = sw * ss;
const wx = Math.max(0.0, ax - s);
const wy = Math.max(0.0, ay - s);
if (!hitTestBBox(x, y, wx, wy)) {
if (strokeSide === EShapeStrokeSide.ALL) {
return true;
} else {
if (x <= -wx) {
if (y <= -wy) {
return !!(strokeSide & EShapeStrokeSide.TOP_OR_LEFT);
} else if (+wy <= y) {
return !!(strokeSide & EShapeStrokeSide.BOTTOM_OR_LEFT);
} else {
return !!(strokeSide & EShapeStrokeSide.LEFT);
}
} else if (+wx <= x) {
if (y <= -wy) {
return !!(strokeSide & EShapeStrokeSide.TOP_OR_RIGHT);
} else if (+wy <= y) {
return !!(strokeSide & EShapeStrokeSide.BOTTOM_OR_RIGHT);
} else {
return !!(strokeSide & EShapeStrokeSide.RIGHT);
}
} else {
if (y <= -wy) {
return !!(strokeSide & EShapeStrokeSide.TOP);
} else if (+wy <= y) {
return !!(strokeSide & EShapeStrokeSide.BOTTOM);
} else {
return false;
}
}
}
}
}
}
}
return false;
};