You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.9 KiB

1 year ago
  1. // DOMRect
  2. (function (global) {
  3. function number(v) {
  4. return v === undefined ? 0 : Number(v);
  5. }
  6. function different(u, v) {
  7. return u !== v && !(isNaN(u) && isNaN(v));
  8. }
  9. function DOMRect(xArg, yArg, wArg, hArg) {
  10. var x, y, width, height, left, right, top, bottom;
  11. x = number(xArg);
  12. y = number(yArg);
  13. width = number(wArg);
  14. height = number(hArg);
  15. Object.defineProperties(this, {
  16. x: {
  17. get: function () { return x; },
  18. set: function (newX) {
  19. if (different(x, newX)) {
  20. x = newX;
  21. left = right = undefined;
  22. }
  23. },
  24. enumerable: true
  25. },
  26. y: {
  27. get: function () { return y; },
  28. set: function (newY) {
  29. if (different(y, newY)) {
  30. y = newY;
  31. top = bottom = undefined;
  32. }
  33. },
  34. enumerable: true
  35. },
  36. width: {
  37. get: function () { return width; },
  38. set: function (newWidth) {
  39. if (different(width, newWidth)) {
  40. width = newWidth;
  41. left = right = undefined;
  42. }
  43. },
  44. enumerable: true
  45. },
  46. height: {
  47. get: function () { return height; },
  48. set: function (newHeight) {
  49. if (different(height, newHeight)) {
  50. height = newHeight;
  51. top = bottom = undefined;
  52. }
  53. },
  54. enumerable: true
  55. },
  56. left: {
  57. get: function () {
  58. if (left === undefined) {
  59. left = x + Math.min(0, width);
  60. }
  61. return left;
  62. },
  63. enumerable: true
  64. },
  65. right: {
  66. get: function () {
  67. if (right === undefined) {
  68. right = x + Math.max(0, width);
  69. }
  70. return right;
  71. },
  72. enumerable: true
  73. },
  74. top: {
  75. get: function () {
  76. if (top === undefined) {
  77. top = y + Math.min(0, height);
  78. }
  79. return top;
  80. },
  81. enumerable: true
  82. },
  83. bottom: {
  84. get: function () {
  85. if (bottom === undefined) {
  86. bottom = y + Math.max(0, height);
  87. }
  88. return bottom;
  89. },
  90. enumerable: true
  91. }
  92. });
  93. }
  94. global.DOMRect = DOMRect;
  95. }(self));