@@ -126,13 +126,17 @@ exports.clearLocationCache = function() {
126
126
* given by `bounds` {left, right, top, bottom}, to within a
127
127
* precision of `buffer` px
128
128
*
129
- * returns {
129
+ * returns: undefined if nothing is visible, else object:
130
+ * {
130
131
* min: position where the path first enters bounds, or 0 if it
131
132
* starts within bounds
132
133
* max: position where the path last exits bounds, or the path length
133
134
* if it finishes within bounds
135
+ * len: max - min, ie the length of visible path
134
136
* total: the total path length - just included so the caller doesn't
135
137
* need to call path.getTotalLength() again
138
+ * isClosed: true iff the start and end points of the path are both visible
139
+ * and are at the same point
136
140
* }
137
141
*
138
142
* Works by starting from either end and repeatedly finding the distance from
@@ -147,17 +151,24 @@ exports.getVisibleSegment = function getVisibleSegment(path, bounds, buffer) {
147
151
var top = bounds . top ;
148
152
var bottom = bounds . bottom ;
149
153
154
+ var pMin = 0 ;
155
+ var pTotal = path . getTotalLength ( ) ;
156
+ var pMax = pTotal ;
157
+
158
+ var pt0 , ptTotal ;
159
+
150
160
function getDistToPlot ( len ) {
151
161
var pt = path . getPointAtLength ( len ) ;
162
+
163
+ // hold on to the start and end points for `closed`
164
+ if ( len === 0 ) pt0 = pt ;
165
+ else if ( len === pTotal ) ptTotal = pt ;
166
+
152
167
var dx = ( pt . x < left ) ? left - pt . x : ( pt . x > right ? pt . x - right : 0 ) ;
153
168
var dy = ( pt . y < top ) ? top - pt . y : ( pt . y > bottom ? pt . y - bottom : 0 ) ;
154
169
return Math . sqrt ( dx * dx + dy * dy ) ;
155
170
}
156
171
157
- var pMin = 0 ;
158
- var pTotal = path . getTotalLength ( ) ;
159
- var pMax = pTotal ;
160
-
161
172
var distToPlot = getDistToPlot ( pMin ) ;
162
173
while ( distToPlot ) {
163
174
pMin += distToPlot + buffer ;
@@ -172,5 +183,13 @@ exports.getVisibleSegment = function getVisibleSegment(path, bounds, buffer) {
172
183
distToPlot = getDistToPlot ( pMax ) ;
173
184
}
174
185
175
- return { min : pMin , max : pMax , total : pTotal } ;
186
+ return {
187
+ min : pMin ,
188
+ max : pMax ,
189
+ len : pMax - pMin ,
190
+ total : pTotal ,
191
+ isClosed : pMin === 0 && pMax === pTotal &&
192
+ Math . abs ( pt0 . x - ptTotal . x ) < 0.1 &&
193
+ Math . abs ( pt0 . y - ptTotal . y ) < 0.1
194
+ } ;
176
195
} ;
0 commit comments