konva/src/shapes/Rect.ts

108 lines
2.5 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-25 01:06:04 +08:00
import { getNumberValidator } from '../Validators';
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;
}
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 {
// arcTo would be nicer, but browser support is patchy (Opera)
cornerRadius = Math.min(cornerRadius, width / 2, height / 2);
context.moveTo(cornerRadius, 0);
context.lineTo(width - cornerRadius, 0);
context.arc(
width - cornerRadius,
cornerRadius,
cornerRadius,
(Math.PI * 3) / 2,
0,
false
);
context.lineTo(width, height - cornerRadius);
context.arc(
width - cornerRadius,
height - cornerRadius,
cornerRadius,
0,
Math.PI / 2,
false
);
context.lineTo(cornerRadius, height);
context.arc(
cornerRadius,
height - cornerRadius,
cornerRadius,
Math.PI / 2,
Math.PI,
false
);
context.lineTo(0, cornerRadius);
context.arc(
cornerRadius,
cornerRadius,
cornerRadius,
Math.PI,
(Math.PI * 3) / 2,
false
);
}
context.closePath();
context.fillStrokeShape(this);
}
cornerRadius: GetSet<number, this>;
}
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);
*/
2019-02-25 01:06:04 +08:00
Factory.addGetterSetter(Rect, 'cornerRadius', 0, getNumberValidator());
2019-01-02 04:59:27 +08:00
Collection.mapMethods(Rect);