-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
controlRendering.ts
138 lines (134 loc) · 4.85 KB
/
controlRendering.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
import { twoMathPi } from '../constants';
import type { InteractiveFabricObject } from '../shapes/Object/InteractiveObject';
import { degreesToRadians } from '../util/misc/radiansDegreesConversion';
import type { Control } from './Control';
export type ControlRenderingStyleOverride = Partial<
Pick<
InteractiveFabricObject,
| 'cornerStyle'
| 'cornerSize'
| 'cornerColor'
| 'cornerStrokeColor'
| 'cornerDashArray'
| 'transparentCorners'
>
>;
export type ControlRenderer = (
ctx: CanvasRenderingContext2D,
left: number,
top: number,
styleOverride: ControlRenderingStyleOverride,
fabricObject: InteractiveFabricObject
) => void;
/**
* Render a round control, as per fabric features.
* This function is written to respect object properties like transparentCorners, cornerSize
* cornerColor, cornerStrokeColor
* plus the addition of offsetY and offsetX.
* @param {CanvasRenderingContext2D} ctx context to render on
* @param {Number} left x coordinate where the control center should be
* @param {Number} top y coordinate where the control center should be
* @param {Object} styleOverride override for FabricObject controls style
* @param {FabricObject} fabricObject the fabric object for which we are rendering controls
*/
export function renderCircleControl(
this: Control,
ctx: CanvasRenderingContext2D,
left: number,
top: number,
styleOverride: ControlRenderingStyleOverride,
fabricObject: InteractiveFabricObject
) {
styleOverride = styleOverride || {};
const xSize =
this.sizeX || styleOverride.cornerSize || fabricObject.cornerSize,
ySize = this.sizeY || styleOverride.cornerSize || fabricObject.cornerSize,
transparentCorners =
typeof styleOverride.transparentCorners !== 'undefined'
? styleOverride.transparentCorners
: fabricObject.transparentCorners,
methodName = transparentCorners ? 'stroke' : 'fill',
stroke =
!transparentCorners &&
(styleOverride.cornerStrokeColor || fabricObject.cornerStrokeColor);
let myLeft = left,
myTop = top,
size;
ctx.save();
ctx.fillStyle = styleOverride.cornerColor || fabricObject.cornerColor || '';
ctx.strokeStyle =
styleOverride.cornerStrokeColor || fabricObject.cornerStrokeColor || '';
// TODO: use proper ellipse code.
if (xSize > ySize) {
size = xSize;
ctx.scale(1.0, ySize / xSize);
myTop = (top * xSize) / ySize;
} else if (ySize > xSize) {
size = ySize;
ctx.scale(xSize / ySize, 1.0);
myLeft = (left * ySize) / xSize;
} else {
size = xSize;
}
// this is still wrong
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(myLeft, myTop, size / 2, 0, twoMathPi, false);
ctx[methodName]();
if (stroke) {
ctx.stroke();
}
ctx.restore();
}
/**
* Render a square control, as per fabric features.
* This function is written to respect object properties like transparentCorners, cornerSize
* cornerColor, cornerStrokeColor
* plus the addition of offsetY and offsetX.
* @param {CanvasRenderingContext2D} ctx context to render on
* @param {Number} left x coordinate where the control center should be
* @param {Number} top y coordinate where the control center should be
* @param {Object} styleOverride override for FabricObject controls style
* @param {FabricObject} fabricObject the fabric object for which we are rendering controls
*/
export function renderSquareControl(
this: Control,
ctx: CanvasRenderingContext2D,
left: number,
top: number,
styleOverride: ControlRenderingStyleOverride,
fabricObject: InteractiveFabricObject
) {
styleOverride = styleOverride || {};
const xSize =
this.sizeX || styleOverride.cornerSize || fabricObject.cornerSize,
ySize = this.sizeY || styleOverride.cornerSize || fabricObject.cornerSize,
transparentCorners =
typeof styleOverride.transparentCorners !== 'undefined'
? styleOverride.transparentCorners
: fabricObject.transparentCorners,
methodName = transparentCorners ? 'stroke' : 'fill',
stroke =
!transparentCorners &&
(styleOverride.cornerStrokeColor || fabricObject.cornerStrokeColor),
xSizeBy2 = xSize / 2,
ySizeBy2 = ySize / 2;
ctx.save();
ctx.fillStyle = styleOverride.cornerColor || fabricObject.cornerColor || '';
ctx.strokeStyle =
styleOverride.cornerStrokeColor || fabricObject.cornerStrokeColor || '';
// this is still wrong
ctx.lineWidth = 1;
ctx.translate(left, top);
// angle is relative to canvas plane
const angle = fabricObject.getTotalAngle();
ctx.rotate(degreesToRadians(angle));
// this does not work, and fixed with ( && ) does not make sense.
// to have real transparent corners we need the controls on upperCanvas
// transparentCorners || ctx.clearRect(-xSizeBy2, -ySizeBy2, xSize, ySize);
ctx[`${methodName}Rect`](-xSizeBy2, -ySizeBy2, xSize, ySize);
if (stroke) {
ctx.strokeRect(-xSizeBy2, -ySizeBy2, xSize, ySize);
}
ctx.restore();
}