path.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A thick wrapper around paths.
  16. * @author robbyw@google.com (Robby Walker)
  17. */
  18. goog.provide('goog.graphics.ext.Path');
  19. goog.require('goog.graphics.AffineTransform');
  20. goog.require('goog.graphics.Path');
  21. goog.require('goog.math.Rect');
  22. /**
  23. * Creates a path object
  24. * @constructor
  25. * @extends {goog.graphics.Path}
  26. * @final
  27. */
  28. goog.graphics.ext.Path = function() {
  29. goog.graphics.Path.call(this);
  30. };
  31. goog.inherits(goog.graphics.ext.Path, goog.graphics.Path);
  32. /**
  33. * Optional cached or user specified bounding box. A user may wish to
  34. * precompute a bounding box to save time and include more accurate
  35. * computations.
  36. * @type {goog.math.Rect?}
  37. * @private
  38. */
  39. goog.graphics.ext.Path.prototype.bounds_ = null;
  40. /**
  41. * Clones the path.
  42. * @return {!goog.graphics.ext.Path} A clone of this path.
  43. * @override
  44. */
  45. goog.graphics.ext.Path.prototype.clone = function() {
  46. var output = /** @type {goog.graphics.ext.Path} */
  47. (goog.graphics.ext.Path.superClass_.clone.call(this));
  48. output.bounds_ = this.bounds_ && this.bounds_.clone();
  49. return output;
  50. };
  51. /**
  52. * Transforms the path. Only simple paths are transformable. Attempting
  53. * to transform a non-simple path will throw an error.
  54. * @param {!goog.graphics.AffineTransform} tx The transformation to perform.
  55. * @return {!goog.graphics.ext.Path} The path itself.
  56. * @override
  57. */
  58. goog.graphics.ext.Path.prototype.transform = function(tx) {
  59. goog.graphics.ext.Path.superClass_.transform.call(this, tx);
  60. // Make sure the precomputed bounds are cleared when the path is transformed.
  61. this.bounds_ = null;
  62. return this;
  63. };
  64. /**
  65. * Modify the bounding box of the path. This may cause the path to be
  66. * simplified (i.e. arcs converted to curves) as a side-effect.
  67. * @param {number} deltaX How far to translate the x coordinates.
  68. * @param {number} deltaY How far to translate the y coordinates.
  69. * @param {number} xFactor After translation, all x coordinates are multiplied
  70. * by this number.
  71. * @param {number} yFactor After translation, all y coordinates are multiplied
  72. * by this number.
  73. * @return {!goog.graphics.ext.Path} The path itself.
  74. */
  75. goog.graphics.ext.Path.prototype.modifyBounds = function(
  76. deltaX, deltaY, xFactor, yFactor) {
  77. if (!this.isSimple()) {
  78. var simple = goog.graphics.Path.createSimplifiedPath(this);
  79. this.clear();
  80. this.appendPath(simple);
  81. }
  82. return this.transform(
  83. goog.graphics.AffineTransform.getScaleInstance(xFactor, yFactor)
  84. .translate(deltaX, deltaY));
  85. };
  86. /**
  87. * Set the precomputed bounds.
  88. * @param {goog.math.Rect?} bounds The bounds to use, or set to null to clear
  89. * and recompute on the next call to getBoundingBox.
  90. */
  91. goog.graphics.ext.Path.prototype.useBoundingBox = function(bounds) {
  92. this.bounds_ = bounds && bounds.clone();
  93. };
  94. /**
  95. * @return {goog.math.Rect?} The bounding box of the path, or null if the
  96. * path is empty.
  97. */
  98. goog.graphics.ext.Path.prototype.getBoundingBox = function() {
  99. if (!this.bounds_ && !this.isEmpty()) {
  100. var minY;
  101. var minX = minY = Number.POSITIVE_INFINITY;
  102. var maxY;
  103. var maxX = maxY = Number.NEGATIVE_INFINITY;
  104. var simplePath =
  105. this.isSimple() ? this : goog.graphics.Path.createSimplifiedPath(this);
  106. simplePath.forEachSegment(function(type, points) {
  107. for (var i = 0, len = points.length; i < len; i += 2) {
  108. minX = Math.min(minX, points[i]);
  109. maxX = Math.max(maxX, points[i]);
  110. minY = Math.min(minY, points[i + 1]);
  111. maxY = Math.max(maxY, points[i + 1]);
  112. }
  113. });
  114. this.bounds_ = new goog.math.Rect(minX, minY, maxX - minX, maxY - minY);
  115. }
  116. return this.bounds_;
  117. };