| 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 |
|
| 16 |
/** |
| 17 |
* @fileoverview A thick wrapper around paths. |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
goog.provide('goog.graphics.ext.Path'); |
| 23 |
|
| 24 |
goog.require('goog.graphics.AffineTransform'); |
| 25 |
goog.require('goog.graphics.Path'); |
| 26 |
goog.require('goog.math.Rect'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Creates a path object |
| 32 |
* @constructor |
| 33 |
* @extends {goog.graphics.Path} |
| 34 |
* @final |
| 35 |
*/ |
| 36 |
goog.graphics.ext.Path = function() { |
| 37 |
goog.graphics.Path.call(this); |
| 38 |
}; |
| 39 |
goog.inherits(goog.graphics.ext.Path, goog.graphics.Path); |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Optional cached or user specified bounding box. A user may wish to |
| 44 |
* precompute a bounding box to save time and include more accurate |
| 45 |
* computations. |
| 46 |
* @type {goog.math.Rect?} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
goog.graphics.ext.Path.prototype.bounds_ = null; |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Clones the path. |
| 54 |
* @return {!goog.graphics.ext.Path} A clone of this path. |
| 55 |
* @override |
| 56 |
*/ |
| 57 |
goog.graphics.ext.Path.prototype.clone = function() { |
| 58 |
var output = /** @type {goog.graphics.ext.Path} */ |
| 59 |
(goog.graphics.ext.Path.superClass_.clone.call(this)); |
| 60 |
output.bounds_ = this.bounds_ && this.bounds_.clone(); |
| 61 |
return output; |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Transforms the path. Only simple paths are transformable. Attempting |
| 67 |
* to transform a non-simple path will throw an error. |
| 68 |
* @param {!goog.graphics.AffineTransform} tx The transformation to perform. |
| 69 |
* @return {!goog.graphics.ext.Path} The path itself. |
| 70 |
* @override |
| 71 |
*/ |
| 72 |
goog.graphics.ext.Path.prototype.transform = function(tx) { |
| 73 |
goog.graphics.ext.Path.superClass_.transform.call(this, tx); |
| 74 |
|
| 75 |
// Make sure the precomputed bounds are cleared when the path is transformed. |
| 76 |
this.bounds_ = null; |
| 77 |
|
| 78 |
return this; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Modify the bounding box of the path. This may cause the path to be |
| 84 |
* simplified (i.e. arcs converted to curves) as a side-effect. |
| 85 |
* @param {number} deltaX How far to translate the x coordinates. |
| 86 |
* @param {number} deltaY How far to translate the y coordinates. |
| 87 |
* @param {number} xFactor After translation, all x coordinates are multiplied |
| 88 |
* by this number. |
| 89 |
* @param {number} yFactor After translation, all y coordinates are multiplied |
| 90 |
* by this number. |
| 91 |
* @return {!goog.graphics.ext.Path} The path itself. |
| 92 |
*/ |
| 93 |
goog.graphics.ext.Path.prototype.modifyBounds = function( |
| 94 |
deltaX, deltaY, xFactor, yFactor) { |
| 95 |
if (!this.isSimple()) { |
| 96 |
var simple = goog.graphics.Path.createSimplifiedPath(this); |
| 97 |
this.clear(); |
| 98 |
this.appendPath(simple); |
| 99 |
} |
| 100 |
|
| 101 |
return this.transform( |
| 102 |
goog.graphics.AffineTransform.getScaleInstance(xFactor, yFactor) |
| 103 |
.translate(deltaX, deltaY)); |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Set the precomputed bounds. |
| 109 |
* @param {goog.math.Rect?} bounds The bounds to use, or set to null to clear |
| 110 |
* and recompute on the next call to getBoundingBox. |
| 111 |
*/ |
| 112 |
goog.graphics.ext.Path.prototype.useBoundingBox = function(bounds) { |
| 113 |
this.bounds_ = bounds && bounds.clone(); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* @return {goog.math.Rect?} The bounding box of the path, or null if the |
| 119 |
* path is empty. |
| 120 |
*/ |
| 121 |
goog.graphics.ext.Path.prototype.getBoundingBox = function() { |
| 122 |
if (!this.bounds_ && !this.isEmpty()) { |
| 123 |
var minY; |
| 124 |
var minX = minY = Number.POSITIVE_INFINITY; |
| 125 |
var maxY; |
| 126 |
var maxX = maxY = Number.NEGATIVE_INFINITY; |
| 127 |
|
| 128 |
var simplePath = |
| 129 |
this.isSimple() ? this : goog.graphics.Path.createSimplifiedPath(this); |
| 130 |
simplePath.forEachSegment(function(type, points) { |
| 131 |
for (var i = 0, len = points.length; i < len; i += 2) { |
| 132 |
minX = Math.min(minX, points[i]); |
| 133 |
maxX = Math.max(maxX, points[i]); |
| 134 |
minY = Math.min(minY, points[i + 1]); |
| 135 |
maxY = Math.max(maxY, points[i + 1]); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
this.bounds_ = new goog.math.Rect(minX, minY, maxX - minX, maxY - minY); |
| 140 |
} |
| 141 |
|
| 142 |
return this.bounds_; |
| 143 |
}; |