rect.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. define(function(require, exports, module) {
  2. var RectUtils = {},
  3. Utils = require('../core/utils'),
  4. Point = require('./point'),
  5. Box = require('./box');
  6. Utils.extend(RectUtils, {
  7. //根据传递进来的width、height和radius属性,
  8. //获取最适合的radius值
  9. formatRadius: function(width, height, radius) {
  10. var minValue = Math.floor(Math.min(width / 2, height / 2));
  11. return Math.min(minValue, radius);
  12. }
  13. });
  14. /**
  15. * @class kity.Rect
  16. * @description 表示一个矩形
  17. * @base kity.Path
  18. */
  19. var Rect = require('../core/class').createClass('Rect', {
  20. base: require('./path'),
  21. /**
  22. * @constructor
  23. * @for kity.Rect
  24. * @grammar new kity.Rect(width, height, x, y, radius)
  25. * @param {Number} width 矩形的初始化宽度
  26. * @param {Number} height 矩形的初始化高度
  27. * @param {Number} x 矩形的初始化 x 坐标
  28. * @param {Number} y 矩形的初始化 y 坐标
  29. * @param {Number} radius 矩形的初始化圆角大小
  30. */
  31. constructor: function(width, height, x, y, radius) {
  32. this.callBase();
  33. this.x = x || 0;
  34. this.y = y || 0;
  35. this.width = width || 0;
  36. this.height = height || 0;
  37. this.radius = RectUtils.formatRadius(this.width, this.height, radius || 0);
  38. this.update();
  39. },
  40. update: function() {
  41. var x = this.x,
  42. y = this.y,
  43. w = this.width,
  44. h = this.height,
  45. r = this.radius;
  46. var drawer = this.getDrawer().redraw();
  47. if (!r) {
  48. // 直角
  49. drawer.push('M', x, y);
  50. drawer.push('h', w);
  51. drawer.push('v', h);
  52. drawer.push('h', -w);
  53. drawer.push('z');
  54. } else {
  55. //圆角
  56. w -= 2 * r;
  57. h -= 2 * r;
  58. drawer.push('M', x + r, y);
  59. drawer.push('h', w);
  60. drawer.push('a', r, r, 0, 0, 1, r, r);
  61. drawer.push('v', h);
  62. drawer.push('a', r, r, 0, 0, 1, -r, r);
  63. drawer.push('h', -w);
  64. drawer.push('a', r, r, 0, 0, 1, -r, -r);
  65. drawer.push('v', -h);
  66. drawer.push('a', r, r, 0, 0, 1, r, -r);
  67. drawer.push('z');
  68. }
  69. drawer.done();
  70. return this;
  71. },
  72. /**
  73. * @method setWidth
  74. * @for kity.Rect
  75. * @grammar setWidth(width) => kity.Rect
  76. * @description 设置矩形的宽度,设置后返回矩形实例本身
  77. * @param {Number} width 宽度值
  78. *
  79. * @example
  80. * ```js
  81. * rect.setWidth(300);
  82. * ```
  83. */
  84. setWidth: function(width) {
  85. this.width = width;
  86. return this.update();
  87. },
  88. /**
  89. * @method setHeight
  90. * @for kity.Rect
  91. * @grammar setHeight(height) => kity.Rect
  92. * @description 设置矩形的高度,设置后返回矩形实例本身
  93. * @param {Number} height 高度值
  94. *
  95. * @example
  96. * ```js
  97. * rect.setHeight(200);
  98. * ```
  99. */
  100. setHeight: function(height) {
  101. this.height = height;
  102. return this.update();
  103. },
  104. /**
  105. * @method setSize
  106. * @for kity.Rect
  107. * @grammar setSize(width, height) => kity.Rect
  108. * @description 设置矩形的尺寸,设置后返回矩形本身
  109. * @param {Number} width 矩形的宽度值
  110. * @param {Number} height 矩形的高度值
  111. *
  112. * @example
  113. * ```js
  114. * rect.setSize(300, 200);
  115. * ```
  116. */
  117. setSize: function(width, height) {
  118. this.width = width;
  119. this.height = height;
  120. return this.update();
  121. },
  122. /**
  123. * @method setBox
  124. * @for kity.Rect
  125. * @grammar setBox(box) => kity.Rect
  126. * @description 使用一个 kity 的盒子数据,
  127. * @param {kity.Box} box 盒子数据
  128. */
  129. setBox: function(box) {
  130. this.x = box.x;
  131. this.y = box.y;
  132. this.width = box.width;
  133. this.height = box.height;
  134. return this.update();
  135. },
  136. getBox: function() {
  137. return new Box(this.x, this.y, this.width, this.height);
  138. },
  139. getRadius: function() {
  140. return this.radius;
  141. },
  142. setRadius: function(radius) {
  143. this.radius = RectUtils.formatRadius(this.width, this.height, radius || 0);
  144. return this.update();
  145. },
  146. getPosition: function() {
  147. return new Point(
  148. this.x,
  149. this.y
  150. );
  151. },
  152. setPosition: function(x, y) {
  153. if (arguments.length == 1) {
  154. var p = Point.parse(arguments[0]);
  155. y = p.y;
  156. x = p.x;
  157. }
  158. this.x = x;
  159. this.y = y;
  160. return this.update();
  161. },
  162. getWidth: function() {
  163. return this.width;
  164. },
  165. getHeight: function() {
  166. return this.height;
  167. },
  168. getPositionX: function() {
  169. return this.x;
  170. },
  171. getPositionY: function() {
  172. return this.y;
  173. },
  174. setPositionX: function(x) {
  175. this.x = x;
  176. return this.update();
  177. },
  178. setPositionY: function(y) {
  179. this.y = y;
  180. return this.update();
  181. }
  182. });
  183. return Rect;
  184. });