| Current Path : /home/cbequiperp/www/components/com_flexicontent/librairies/zrender/shape/ |
| Current File : /home/cbequiperp/www/components/com_flexicontent/librairies/zrender/shape/Rectangle.js |
/**
* 矩形
* @module zrender/shape/Rectangle
* @author Kener (@Kener-林峰, kener.linfeng@gmail.com) ,
* strwind (@劲风FEI)
* @example
* var Rectangle = require('zrender/shape/Rectangle');
* var shape = new Rectangle({
* style: {
* x: 0,
* y: 0,
* width: 100,
* height: 100,
* radius: 20
* }
* });
* zr.addShape(shape);
*/
/**
* @typedef {Object} IRectangleStyle
* @property {number} x 左上角x坐标
* @property {number} y 左上角y坐标
* @property {number} width 宽度
* @property {number} height 高度
* @property {number|Array.<number>} radius 矩形圆角,可以用数组分别指定四个角的圆角
* @property {string} [brushType='fill']
* @property {string} [color='#000000'] 填充颜色
* @property {string} [strokeColor='#000000'] 描边颜色
* @property {string} [lineCape='butt'] 线帽样式,可以是 butt, round, square
* @property {number} [lineWidth=1] 描边宽度
* @property {number} [opacity=1] 绘制透明度
* @property {number} [shadowBlur=0] 阴影模糊度,大于0有效
* @property {string} [shadowColor='#000000'] 阴影颜色
* @property {number} [shadowOffsetX=0] 阴影横向偏移
* @property {number} [shadowOffsetY=0] 阴影纵向偏移
* @property {string} [text] 图形中的附加文本
* @property {string} [textColor='#000000'] 文本颜色
* @property {string} [textFont] 附加文本样式,eg:'bold 18px verdana'
* @property {string} [textPosition='end'] 附加文本位置, 可以是 inside, left, right, top, bottom
* @property {string} [textAlign] 默认根据textPosition自动设置,附加文本水平对齐。
* 可以是start, end, left, right, center
* @property {string} [textBaseline] 默认根据textPosition自动设置,附加文本垂直对齐。
* 可以是top, bottom, middle, alphabetic, hanging, ideographic
*/
define(
function (require) {
var Base = require('./Base');
/**
* @alias module:zrender/shape/Rectangle
* @constructor
* @extends module:zrender/shape/Base
* @param {Object} options
*/
var Rectangle = function (options) {
Base.call(this, options);
/**
* 矩形绘制样式
* @name module:zrender/shape/Rectangle#style
* @type {module:zrender/shape/Rectangle~IRectangleStyle}
*/
/**
* 矩形高亮绘制样式
* @name module:zrender/shape/Rectangle#highlightStyle
* @type {module:zrender/shape/Rectangle~IRectangleStyle}
*/
};
Rectangle.prototype = {
type: 'rectangle',
_buildRadiusPath: function (ctx, style) {
// 左上、右上、右下、左下角的半径依次为r1、r2、r3、r4
// r缩写为1 相当于 [1, 1, 1, 1]
// r缩写为[1] 相当于 [1, 1, 1, 1]
// r缩写为[1, 2] 相当于 [1, 2, 1, 2]
// r缩写为[1, 2, 3] 相当于 [1, 2, 3, 2]
var x = style.x;
var y = style.y;
var width = style.width;
var height = style.height;
var r = style.radius;
var r1;
var r2;
var r3;
var r4;
if (typeof r === 'number') {
r1 = r2 = r3 = r4 = r;
}
else if (r instanceof Array) {
if (r.length === 1) {
r1 = r2 = r3 = r4 = r[0];
}
else if (r.length === 2) {
r1 = r3 = r[0];
r2 = r4 = r[1];
}
else if (r.length === 3) {
r1 = r[0];
r2 = r4 = r[1];
r3 = r[2];
}
else {
r1 = r[0];
r2 = r[1];
r3 = r[2];
r4 = r[3];
}
}
else {
r1 = r2 = r3 = r4 = 0;
}
var total;
if (r1 + r2 > width) {
total = r1 + r2;
r1 *= width / total;
r2 *= width / total;
}
if (r3 + r4 > width) {
total = r3 + r4;
r3 *= width / total;
r4 *= width / total;
}
if (r2 + r3 > height) {
total = r2 + r3;
r2 *= height / total;
r3 *= height / total;
}
if (r1 + r4 > height) {
total = r1 + r4;
r1 *= height / total;
r4 *= height / total;
}
ctx.moveTo(x + r1, y);
ctx.lineTo(x + width - r2, y);
r2 !== 0 && ctx.quadraticCurveTo(
x + width, y, x + width, y + r2
);
ctx.lineTo(x + width, y + height - r3);
r3 !== 0 && ctx.quadraticCurveTo(
x + width, y + height, x + width - r3, y + height
);
ctx.lineTo(x + r4, y + height);
r4 !== 0 && ctx.quadraticCurveTo(
x, y + height, x, y + height - r4
);
ctx.lineTo(x, y + r1);
r1 !== 0 && ctx.quadraticCurveTo(x, y, x + r1, y);
},
/**
* 创建矩形路径
* @param {CanvasRenderingContext2D} ctx
* @param {Object} style
*/
buildPath : function (ctx, style) {
if (!style.radius) {
ctx.moveTo(style.x, style.y);
ctx.lineTo(style.x + style.width, style.y);
ctx.lineTo(style.x + style.width, style.y + style.height);
ctx.lineTo(style.x, style.y + style.height);
ctx.lineTo(style.x, style.y);
// ctx.rect(style.x, style.y, style.width, style.height);
}
else {
this._buildRadiusPath(ctx, style);
}
ctx.closePath();
return;
},
/**
* 计算返回矩形包围盒矩阵
* @param {module:zrender/shape/Rectangle~IRectangleStyle} style
* @return {module:zrender/shape/Base~IBoundingRect}
*/
getRect : function(style) {
if (style.__rect) {
return style.__rect;
}
var lineWidth;
if (style.brushType == 'stroke' || style.brushType == 'fill') {
lineWidth = style.lineWidth || 1;
}
else {
lineWidth = 0;
}
style.__rect = {
x : Math.round(style.x - lineWidth / 2),
y : Math.round(style.y - lineWidth / 2),
width : style.width + lineWidth,
height : style.height + lineWidth
};
return style.__rect;
}
};
require('../tool/util').inherits(Rectangle, Base);
return Rectangle;
}
);