Base class for creating other shapes. Each custom shape must extends from this class.
Kind: global class
Since: 1.0.0
- Shape
- new Shape(cursor, [options])
- instance
- .get(path) ⇒
* - .set(path, value) ⇒
Shape - .getCursor() ⇒
Cursor - .setCursor(cursor) ⇒
Shape - .getText() ⇒
String - .setText([text]) ⇒
Shape - .getWidth() ⇒
Number - .setWidth([width]) ⇒
Shape - .getHeight() ⇒
Number - .setHeight([height]) ⇒
Shape - .getX() ⇒
Number - .setX([x]) ⇒
Shape - .getY() ⇒
Number - .setY([y]) ⇒
Shape - .getBackground() ⇒
String - .setBackground([background]) ⇒
Shape - .getForeground() ⇒
String - .setForeground([foreground]) ⇒
Shape - .render()
- .toObject() ⇒
Object - .toJSON() ⇒
JSON
- .get(path) ⇒
- static
Create basic Shape instance. This shape renders nothing, but throws an exception that you need to implement this shape in childes.
| Param | Type | Description |
|---|---|---|
| cursor | Cursor |
Cursor instance used for render the shape |
| [options] | Object |
Options object |
| [options.text] | String |
Text that will be rendered in the shape |
| [options.width] | Number | String |
Shape width can be 100 (cells) or 100% |
| [options.height] | Number | String |
Shape height can be 100 (cells) or 100% |
| [options.x] | Number | String |
Absolute coordinate X can be 100 (cells), left, center, right or percents |
| [options.y] | Number | String |
Absolute coordinate Y can be 100 (cells), top, middle, bottom or percents |
| [options.background] | String |
Background color can be only color name or none if you want to disable |
| [options.foreground] | String |
Foreground color can be only color name or none if you want to disable |
Example
import Shape from 'kittik-shape-basic';
export default class Rectangle extends Shape {
render() {
const cursor = this.getCursor();
// Implement your logic here for rendering the shape
}
}Get option value.
Kind: instance method of Shape
| Param | Type | Description |
|---|---|---|
| path | String |
Path can be set with dot-notation |
Example
shape.get('my.options.object.value');shape.set(path, value) ⇒ Shape
Set new option value.
Kind: instance method of Shape
| Param | Type | Description |
|---|---|---|
| path | String |
Path can be set with dot-notation |
| value | * |
Value that need to be written to the options object |
Example
shape.set('my.options.object.value', 'value');Get cursor that used for render this shape.
Kind: instance method of Shape
shape.setCursor(cursor) ⇒ Shape
Assign cursor to the shape which will be used for render this shape.
Kind: instance method of Shape
| Param | Type |
|---|---|
| cursor | Cursor |
Get text content from this shape.
Kind: instance method of Shape
shape.setText([text]) ⇒ Shape
Set new text content to this shape.
Kind: instance method of Shape
| Param | Type | Default | Description |
|---|---|---|---|
| [text] | String |
'' |
New text |
Get shape width.
Kind: instance method of Shape
shape.setWidth([width]) ⇒ Shape
Set new shape width.
Kind: instance method of Shape
| Param | Type | Default | Description |
|---|---|---|---|
| [width] | Number | String |
15 |
Shape width |
Example
shape.setWidth(15); // shape width is equal to 15 cells in the terminal
shape.setWidth('20%'); // shape width is equal to 20% of total viewport widthGet shape height.
Kind: instance method of Shape
shape.setHeight([height]) ⇒ Shape
Set new shape height.
Kind: instance method of Shape
| Param | Type | Default | Description |
|---|---|---|---|
| [height] | Number | String |
5 |
Shape height |
Example
shape.setHeight(15); // shape height is equal to 15 cells in the terminal
shape.setHeight('20%'); // shape height is equal to 20% of total viewport heightGet X coordinate.
Kind: instance method of Shape
shape.setX([x]) ⇒ Shape
Set X coordinate.
Kind: instance method of Shape
| Param | Type | Default |
|---|---|---|
| [x] | Number | String |
10 |
Example
shape.setX(2); // move shape to third cell by X axis
shape.setX('left'); // align shape to the left
shape.setX('center'); // align shape in the center
shape.setX('right'); // align shape to the right
shape.setX('50%'); // move shape to 50% by X axisGet Y coordinate.
Kind: instance method of Shape
shape.setY([y]) ⇒ Shape
Set Y coordinate.
Kind: instance method of Shape
| Param | Type | Default |
|---|---|---|
| [y] | Number | String |
10 |
Example
shape.setY(2); // move shape to third cell by Y axis
shape.setY('top'); // align shape to the top
shape.setY('middle'); // align shape in the middle
shape.setY('bottom'); // align shape to the bottom
shape.setY('50%'); // move shape to 50% by Y axisGet background color.
Kind: instance method of Shape
shape.setBackground([background]) ⇒ Shape
Set new background color.
Kind: instance method of Shape
| Param | Type | Default | Description |
|---|---|---|---|
| [background] | String |
none |
Color name or none if you want to disable background |
Example
shape.setBackground('black');
shape.setBackground('none');Get foreground color.
Kind: instance method of Shape
shape.setForeground([foreground]) ⇒ Shape
Set new foreground color.
Kind: instance method of Shape
| Param | Type | Default | Description |
|---|---|---|---|
| [foreground] | String |
none |
Color name or none if you want to disable foreground |
Example
shape.setForeground('black');
shape.setForeground('none');Base render method that must be implemented in childes.
Kind: instance abstract method of Shape
Throws:
ErrorThrows error if method is not overridden
Returns Object representation of the shape. This representation consists of all options fields that you can pass in the constructor.
Kind: instance method of Shape
Returns JSON representation of the shape.
Kind: instance method of Shape
Shape.create(args) ⇒ Shape
Wrapper around new Shape().
Kind: static method of Shape
| Param | Type |
|---|---|
| args | * |
Shape.fromObject(obj, [cursor]) ⇒ Shape
Creates new Shape instance from Object representation. You can ignore cursor param and create only Shape representation. Though, you can add cursor in the runtime via setCursor method.
Kind: static method of Shape
Throws:
ErrorThrows an error if object is not a representation of the shape
| Param | Type | Description |
|---|---|---|
| obj | Object |
Object that you got from toObject method |
| [cursor] | Cursor |
Cursor instance |
Shape.fromJSON(json, [cursor]) ⇒ Shape
Creates new Shape instance from JSON representation. You can ignore cursor param and create only Shape representation. Though, you can add cursor in the runtime via setCursor method.
Kind: static method of Shape
| Param | Type | Description |
|---|---|---|
| json | String |
JSON string that you got from Shape.toJSON |
| [cursor] | Cursor |
Cursor instance |