-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
rectangle.js
143 lines (133 loc) · 3.85 KB
/
rectangle.js
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
import Component from "@pencil.js/component";
import Position from "@pencil.js/position";
/**
* @module Rectangle
*/
/**
* Basic rectangle
* <br><img src="./media/examples/rectangle.png" alt="rectangle demo"/>
* @class
* @extends {module:Component}
*/
export default class Rectangle extends Component {
/**
* Rectangle constructor
* @param {PositionDefinition} positionDefinition - Position in space
* @param {Number} [width=0] - Horizontal size
* @param {Number} [height=0] - Vertical size
* @param {RectangleOptions} [options] - Drawing options
*/
constructor (positionDefinition, width = 0, height = 0, options) {
super(positionDefinition, options);
/**
* @type {Number}
*/
this.width = width;
/**
* @type {Number}
*/
this.height = height;
}
/**
* Draw the rectangle
* @param {path} path - Drawing context
* @return {Rectangle} Itself
*/
trace (path) {
const { rounded } = this.options;
if (rounded) {
path.roundRect(0, 0, this.width, this.height, ...(Array.isArray(rounded) ? rounded : [rounded]));
}
else {
path.rect(0, 0, this.width, this.height);
}
return this;
}
/**
* @inheritDoc
*/
getOrigin () {
const { origin } = this.options;
if (typeof origin === "string") {
const { origins } = Rectangle;
const position = new Position();
// Horizontal
if ([origins.topRight, origins.centerRight, origins.bottomRight].includes(origin)) {
position.x = -this.width;
}
else if ([origins.topCenter, origins.center, origins.bottomCenter].includes(origin)) {
position.x = -this.width / 2;
}
// Vertical
if ([origins.bottomLeft, origins.bottomCenter, origins.bottomRight].includes(origin)) {
position.y = -this.height;
}
else if ([origins.centerLeft, origins.center, origins.centerRight].includes(origin)) {
position.y = -this.height / 2;
}
return position;
}
return super.getOrigin();
}
/**
* @inheritDoc
*/
toJSON () {
const { width, height } = this;
return {
...super.toJSON(),
width,
height,
};
}
/**
* @inheritDoc
* @param {Object} definition - Rectangle definition
* @return {Rectangle}
*/
static from (definition) {
return new Rectangle(definition.position, definition.width, definition.height, definition.options);
}
/**
* @typedef {Object} RectangleOptions
* @extends ComponentOptions
* @prop {Number|Array<Number>} rounded - Corner radius or an array of radii [top-left, top-right, bottom-right, bottom-left]
*/
/**
* @type {RectangleOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
rounded: 0,
};
}
/**
* @typedef {Object} RectangleOrigins
* @prop {String} topLeft
* @prop {String} topRight
* @prop {String} topCenter
* @prop {String} center
* @prop {String} centerLeft
* @prop {String} centerRight
* @prop {String} bottomLeft
* @prop {String} bottomRight
* @prop {String} bottomCenter
*/
/**
* @type {RectangleOrigins}
*/
static get origins () {
return {
topLeft: "topLeft",
topRight: "topRight",
topCenter: "topCenter",
center: "center",
centerLeft: "centerLeft",
centerRight: "centerRight",
bottomLeft: "bottomLeft",
bottomRight: "bottomRight",
bottomCenter: "bottomCenter",
};
}
}