aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/graphics/ext/path.js
blob: c2edffd32d1f2bfac2f9a8d717bf861c5647d171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview A thick wrapper around paths.
 * @author robbyw@google.com (Robby Walker)
 */


goog.provide('goog.graphics.ext.Path');

goog.require('goog.graphics.AffineTransform');
goog.require('goog.graphics.Path');
goog.require('goog.math');
goog.require('goog.math.Rect');



/**
 * Creates a path object
 * @constructor
 * @extends {goog.graphics.Path}
 */
goog.graphics.ext.Path = function() {
  goog.graphics.Path.call(this);
};
goog.inherits(goog.graphics.ext.Path, goog.graphics.Path);


/**
 * Optional cached or user specified bounding box.  A user may wish to
 * precompute a bounding box to save time and include more accurate
 * computations.
 * @type {goog.math.Rect?}
 * @private
 */
goog.graphics.ext.Path.prototype.bounds_ = null;


/**
 * Clones the path.
 * @return {!goog.graphics.ext.Path} A clone of this path.
 * @override
 */
goog.graphics.ext.Path.prototype.clone = function() {
  var output = /** @type {goog.graphics.ext.Path} */
      (goog.graphics.ext.Path.superClass_.clone.call(this));
  output.bounds_ = this.bounds_ && this.bounds_.clone();
  return output;
};


/**
 * Transforms the path. Only simple paths are transformable. Attempting
 * to transform a non-simple path will throw an error.
 * @param {!goog.graphics.AffineTransform} tx The transformation to perform.
 * @return {!goog.graphics.ext.Path} The path itself.
 * @override
 */
goog.graphics.ext.Path.prototype.transform = function(tx) {
  goog.graphics.ext.Path.superClass_.transform.call(this, tx);

  // Make sure the precomputed bounds are cleared when the path is transformed.
  this.bounds_ = null;

  return this;
};


/**
 * Modify the bounding box of the path.  This may cause the path to be
 * simplified (i.e. arcs converted to curves) as a side-effect.
 * @param {number} deltaX How far to translate the x coordinates.
 * @param {number} deltaY How far to translate the y coordinates.
 * @param {number} xFactor After translation, all x coordinates are multiplied
 *     by this number.
 * @param {number} yFactor After translation, all y coordinates are multiplied
 *     by this number.
 * @return {goog.graphics.ext.Path} The path itself.
 */
goog.graphics.ext.Path.prototype.modifyBounds = function(deltaX, deltaY,
    xFactor, yFactor) {
  if (!this.isSimple()) {
    var simple = goog.graphics.Path.createSimplifiedPath(this);
    this.clear();
    this.appendPath(simple);
  }

  return this.transform(goog.graphics.AffineTransform.getScaleInstance(
      xFactor, yFactor).translate(deltaX, deltaY));
};


/**
 * Set the precomputed bounds.
 * @param {goog.math.Rect?} bounds The bounds to use, or set to null to clear
 *     and recompute on the next call to getBoundingBox.
 */
goog.graphics.ext.Path.prototype.useBoundingBox = function(bounds) {
  this.bounds_ = bounds && bounds.clone();
};


/**
 * @return {goog.math.Rect?} The bounding box of the path, or null if the
 *     path is empty.
 */
goog.graphics.ext.Path.prototype.getBoundingBox = function() {
  if (!this.bounds_ && !this.isEmpty()) {
    var minY;
    var minX = minY = Number.POSITIVE_INFINITY;
    var maxY;
    var maxX = maxY = Number.NEGATIVE_INFINITY;

    var simplePath = this.isSimple() ? this :
        goog.graphics.Path.createSimplifiedPath(this);
    simplePath.forEachSegment(function(type, points) {
      for (var i = 0, len = points.length; i < len; i += 2) {
        minX = Math.min(minX, points[i]);
        maxX = Math.max(maxX, points[i]);
        minY = Math.min(minY, points[i + 1]);
        maxY = Math.max(maxY, points[i + 1]);
      }
    });

    this.bounds_ = new goog.math.Rect(minX, minY, maxX - minX, maxY - minY);
  }

  return this.bounds_;
};