123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- var vec2 = require("./vector");
- var matrix = require("./matrix");
- /**
- * @module echarts/core/BoundingRect
- */
- var v2ApplyTransform = vec2.applyTransform;
- var mathMin = Math.min;
- var mathMax = Math.max;
- /**
- * @alias module:echarts/core/BoundingRect
- */
- function BoundingRect(x, y, width, height) {
- if (width < 0) {
- x = x + width;
- width = -width;
- }
- if (height < 0) {
- y = y + height;
- height = -height;
- }
- /**
- * @type {number}
- */
- this.x = x;
- /**
- * @type {number}
- */
- this.y = y;
- /**
- * @type {number}
- */
- this.width = width;
- /**
- * @type {number}
- */
- this.height = height;
- }
- BoundingRect.prototype = {
- constructor: BoundingRect,
- /**
- * @param {module:echarts/core/BoundingRect} other
- */
- union: function (other) {
- var x = mathMin(other.x, this.x);
- var y = mathMin(other.y, this.y);
- this.width = mathMax(other.x + other.width, this.x + this.width) - x;
- this.height = mathMax(other.y + other.height, this.y + this.height) - y;
- this.x = x;
- this.y = y;
- },
- /**
- * @param {Array.<number>} m
- * @methods
- */
- applyTransform: function () {
- var lt = [];
- var rb = [];
- var lb = [];
- var rt = [];
- return function (m) {
- // In case usage like this
- // el.getBoundingRect().applyTransform(el.transform)
- // And element has no transform
- if (!m) {
- return;
- }
- lt[0] = lb[0] = this.x;
- lt[1] = rt[1] = this.y;
- rb[0] = rt[0] = this.x + this.width;
- rb[1] = lb[1] = this.y + this.height;
- v2ApplyTransform(lt, lt, m);
- v2ApplyTransform(rb, rb, m);
- v2ApplyTransform(lb, lb, m);
- v2ApplyTransform(rt, rt, m);
- this.x = mathMin(lt[0], rb[0], lb[0], rt[0]);
- this.y = mathMin(lt[1], rb[1], lb[1], rt[1]);
- var maxX = mathMax(lt[0], rb[0], lb[0], rt[0]);
- var maxY = mathMax(lt[1], rb[1], lb[1], rt[1]);
- this.width = maxX - this.x;
- this.height = maxY - this.y;
- };
- }(),
- /**
- * Calculate matrix of transforming from self to target rect
- * @param {module:zrender/core/BoundingRect} b
- * @return {Array.<number>}
- */
- calculateTransform: function (b) {
- var a = this;
- var sx = b.width / a.width;
- var sy = b.height / a.height;
- var m = matrix.create(); // 矩阵右乘
- matrix.translate(m, m, [-a.x, -a.y]);
- matrix.scale(m, m, [sx, sy]);
- matrix.translate(m, m, [b.x, b.y]);
- return m;
- },
- /**
- * @param {(module:echarts/core/BoundingRect|Object)} b
- * @return {boolean}
- */
- intersect: function (b) {
- if (!b) {
- return false;
- }
- if (!(b instanceof BoundingRect)) {
- // Normalize negative width/height.
- b = BoundingRect.create(b);
- }
- var a = this;
- var ax0 = a.x;
- var ax1 = a.x + a.width;
- var ay0 = a.y;
- var ay1 = a.y + a.height;
- var bx0 = b.x;
- var bx1 = b.x + b.width;
- var by0 = b.y;
- var by1 = b.y + b.height;
- return !(ax1 < bx0 || bx1 < ax0 || ay1 < by0 || by1 < ay0);
- },
- contain: function (x, y) {
- var rect = this;
- return x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
- },
- /**
- * @return {module:echarts/core/BoundingRect}
- */
- clone: function () {
- return new BoundingRect(this.x, this.y, this.width, this.height);
- },
- /**
- * Copy from another rect
- */
- copy: function (other) {
- this.x = other.x;
- this.y = other.y;
- this.width = other.width;
- this.height = other.height;
- },
- plain: function () {
- return {
- x: this.x,
- y: this.y,
- width: this.width,
- height: this.height
- };
- }
- };
- /**
- * @param {Object|module:zrender/core/BoundingRect} rect
- * @param {number} rect.x
- * @param {number} rect.y
- * @param {number} rect.width
- * @param {number} rect.height
- * @return {module:zrender/core/BoundingRect}
- */
- BoundingRect.create = function (rect) {
- return new BoundingRect(rect.x, rect.y, rect.width, rect.height);
- };
- var _default = BoundingRect;
- module.exports = _default;
|