konva/src/shapes/Rect.ts

118 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-02-25 01:06:04 +08:00
import { Collection } from '../Util';
import { Factory } from '../Factory';
import { Shape, ShapeConfig } from '../Shape';
2019-02-27 21:06:04 +08:00
import { _registerNode } from '../Global';
2019-01-02 04:59:27 +08:00
import { GetSet } from '../types';
export interface RectConfig extends ShapeConfig {
cornerRadius?: number | number[];
}
2019-01-02 04:59:27 +08:00
/**
* Rect constructor
* @constructor
* @memberof Konva
* @augments Konva.Shape
* @param {Object} config
* @param {Number} [config.cornerRadius]
* @@shapeParams
* @@nodeParams
* @example
* var rect = new Konva.Rect({
* width: 100,
* height: 50,
* fill: 'red',
* stroke: 'black',
* strokeWidth: 5
* });
*/
export class Rect extends Shape<RectConfig> {
2019-01-02 04:59:27 +08:00
_sceneFunc(context) {
var cornerRadius = this.cornerRadius(),
2019-01-06 16:01:20 +08:00
width = this.width(),
height = this.height();
2019-01-02 04:59:27 +08:00
context.beginPath();
if (!cornerRadius) {
// simple rect - don't bother doing all that complicated maths stuff.
context.rect(0, 0, width, height);
} else {
let topLeft = 0;
let topRight = 0;
let bottomLeft = 0;
let bottomRight = 0;
if (typeof cornerRadius === 'number') {
topLeft = topRight = bottomLeft = bottomRight = Math.min(
cornerRadius,
width / 2,
height / 2
);
} else {
topLeft = Math.min(cornerRadius[0], width / 2, height / 2);
topRight = Math.min(cornerRadius[1], width / 2, height / 2);
bottomRight = Math.min(cornerRadius[2], width / 2, height / 2);
bottomLeft = Math.min(cornerRadius[3], width / 2, height / 2);
}
context.moveTo(topLeft, 0);
context.lineTo(width - topRight, 0);
2019-01-02 04:59:27 +08:00
context.arc(
width - topRight,
topRight,
topRight,
2019-01-02 04:59:27 +08:00
(Math.PI * 3) / 2,
0,
false
);
context.lineTo(width, height - bottomRight);
2019-01-02 04:59:27 +08:00
context.arc(
width - bottomRight,
height - bottomRight,
bottomRight,
2019-01-02 04:59:27 +08:00
0,
Math.PI / 2,
false
);
context.lineTo(bottomLeft, height);
2019-01-02 04:59:27 +08:00
context.arc(
bottomLeft,
height - bottomLeft,
bottomLeft,
2019-01-02 04:59:27 +08:00
Math.PI / 2,
Math.PI,
false
);
context.lineTo(0, topLeft);
context.arc(topLeft, topLeft, topLeft, Math.PI, (Math.PI * 3) / 2, false);
2019-01-02 04:59:27 +08:00
}
context.closePath();
context.fillStrokeShape(this);
}
cornerRadius: GetSet<number | number[], this>;
2019-01-02 04:59:27 +08:00
}
2019-01-06 16:01:20 +08:00
Rect.prototype.className = 'Rect';
2019-02-27 21:06:04 +08:00
_registerNode(Rect);
2019-01-06 16:01:20 +08:00
2019-01-02 04:59:27 +08:00
/**
* get/set corner radius
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Rect#cornerRadius
2019-01-02 04:59:27 +08:00
* @param {Number} cornerRadius
* @returns {Number}
* @example
* // get corner radius
* var cornerRadius = rect.cornerRadius();
*
* // set corner radius
* rect.cornerRadius(10);
*
* // set different corner radius values
* // top-left, top-right, bottom-right, bottom-left
* rect.cornerRadius([0, 10, 20, 30]);
2019-01-02 04:59:27 +08:00
*/
Factory.addGetterSetter(Rect, 'cornerRadius', 0);
2019-01-02 04:59:27 +08:00
Collection.mapMethods(Rect);