@@ -69,6 +69,93 @@ test('hitting a stroked path', function() {
6969 } , true ) ;
7070} ) ;
7171
72+ test ( 'hitting path segments' , function ( ) {
73+ var path = new Path ( [ 0 , 0 ] , [ 10 , 10 ] , [ 20 , 0 ] ) ;
74+
75+ var hitResult = paper . project . hitTest ( [ 10 , 10 ] ) ;
76+
77+ equals ( function ( ) {
78+ return ! ! hitResult ;
79+ } , true , 'A HitResult should be returned.' ) ;
80+
81+ if ( hitResult ) {
82+ equals ( function ( ) {
83+ return hitResult . type ;
84+ } , 'segment' ) ;
85+
86+ equals ( function ( ) {
87+ return hitResult . item == path ;
88+ } , true ) ;
89+ }
90+ } ) ;
91+
92+ test ( 'hitting the center of a path' , function ( ) {
93+ var path = new Path ( [ 0 , 0 ] , [ 100 , 100 ] , [ 200 , 0 ] ) ;
94+ path . closed = true ;
95+
96+ var hitResult = paper . project . hitTest ( path . position , {
97+ center : true
98+ } ) ;
99+
100+ equals ( function ( ) {
101+ return ! ! hitResult ;
102+ } , true , 'A HitResult should be returned.' ) ;
103+
104+ if ( hitResult ) {
105+ equals ( function ( ) {
106+ return hitResult . point . toString ( ) ;
107+ } , path . position . toString ( ) ) ;
108+
109+ equals ( function ( ) {
110+ return hitResult . type ;
111+ } , 'center' ) ;
112+ equals ( function ( ) {
113+ return hitResult . item !== paper . project . activeLayer ;
114+ } , true , 'We should not be hitting the active layer.' ) ;
115+
116+ equals ( function ( ) {
117+ return hitResult . item == path ;
118+ } , true , 'We should be hitting the path.' ) ;
119+ }
120+ } ) ;
121+
122+ test ( 'hitting the center of a path with tolerance' , function ( ) {
123+ var path = new Path ( [ 0 , 0 ] , [ 100 , 100 ] , [ 200 , 0 ] ) ;
124+ path . closed = true ;
125+
126+ var hitResult = paper . project . hitTest ( path . position + [ 1 , 1 ] , {
127+ center : true
128+ } ) ;
129+
130+ equals ( function ( ) {
131+ return ! ! hitResult ;
132+ } , true , 'A HitResult should be returned.' ) ;
133+
134+ if ( hitResult ) {
135+ equals ( function ( ) {
136+ return ! ! hitResult . point ;
137+ } , true , 'HitResult#point should not be empty' ) ;
138+
139+ if ( hitResult . point ) {
140+ equals ( function ( ) {
141+ return hitResult . point . toString ( ) ;
142+ } , path . position . toString ( ) ) ;
143+ }
144+
145+ equals ( function ( ) {
146+ return hitResult . type ;
147+ } , 'center' ) ;
148+
149+ equals ( function ( ) {
150+ return hitResult . item !== paper . project . activeLayer ;
151+ } , true , 'We should not be hitting the active layer.' ) ;
152+
153+ equals ( function ( ) {
154+ return hitResult . item == path ;
155+ } , true , 'We should be hitting the path.' ) ;
156+ }
157+ } ) ;
158+
72159test ( 'hitting path handles' , function ( ) {
73160 var path = new Path ( new Segment ( {
74161 point : [ 0 , 0 ] ,
@@ -111,4 +198,103 @@ test('hitting path handles', function() {
111198 return hitResult . item == path ;
112199 } , true ) ;
113200 }
201+ } ) ;
202+
203+ test ( 'HitTest options.segments and options.ends should be mutually exclusive' , function ( ) {
204+ var options = HitResult . getOptions ( {
205+ ends : true
206+ } ) ;
207+ equals ( function ( ) {
208+ return options . ends ;
209+ } , true ) ;
210+ equals ( function ( ) {
211+ return options . segments ;
212+ } , false ) ;
213+ } ) ;
214+
215+ test ( 'hitting path ends' , function ( ) {
216+ var path = new Path ( [ 0 , 0 ] , [ 50 , 50 ] , [ 100 , 0 ] ) ;
217+ path . closed = true ;
218+
219+ var hitResult = paper . project . hitTest ( path . firstSegment . point , {
220+ ends : true
221+ } ) ;
222+
223+ equals ( function ( ) {
224+ return ! ! hitResult ;
225+ } , true , 'A HitResult should be returned (1)' ) ;
226+
227+ if ( hitResult ) {
228+ equals ( function ( ) {
229+ return hitResult . type ;
230+ } , 'segment' ) ;
231+
232+ equals ( function ( ) {
233+ return hitResult . segment == path . firstSegment ;
234+ } , true ) ;
235+ }
236+
237+ var hitResult = paper . project . hitTest ( path . lastSegment . point , {
238+ ends : true
239+ } ) ;
240+
241+ equals ( function ( ) {
242+ return ! ! hitResult ;
243+ } , true , 'A HitResult should be returned (1)' ) ;
244+
245+ if ( hitResult ) {
246+ equals ( function ( ) {
247+ return hitResult . type ;
248+ } , 'segment' ) ;
249+
250+ equals ( function ( ) {
251+ return hitResult . segment == path . lastSegment ;
252+ } , true ) ;
253+ }
254+
255+ equals ( function ( ) {
256+ var hitResult = paper . project . hitTest ( path . segments [ 1 ] . point , {
257+ ends : true
258+ } ) ;
259+ return ! hitResult ;
260+ } , true , 'No HitResult should be returned, since the second segment is not an end' ) ;
261+ } ) ;
262+
263+ test ( 'When a path is closed, the end of a path cannot be hit.' , function ( ) {
264+ var path = new Path ( [ 0 , 0 ] , [ 50 , 50 ] , [ 100 , 0 ] ) ;
265+ path . closed = true ;
266+
267+ var hitResult = paper . project . hitTest ( [ 0 , 0 ] , {
268+ ends : true
269+ } ) ;
270+
271+ equals ( function ( ) {
272+ return ! hitResult ;
273+ } , true , 'When a path is closed, the end of a path cannot be hit.' ) ;
274+ } ) ;
275+
276+ test ( 'hitting path bounding box' , function ( ) {
277+ var path = new Path . Circle ( new Point ( 100 , 100 ) , 50 ) ;
278+
279+ var hitResult = paper . project . hitTest ( path . bounds . topLeft , {
280+ bounds : true
281+ } ) ;
282+
283+ equals ( function ( ) {
284+ return ! ! hitResult ;
285+ } , true , 'A HitResult should be returned (1)' ) ;
286+
287+ if ( hitResult ) {
288+ equals ( function ( ) {
289+ return hitResult . type ;
290+ } , 'bounds' ) ;
291+
292+ equals ( function ( ) {
293+ return hitResult . name ;
294+ } , 'top-left' ) ;
295+
296+ equals ( function ( ) {
297+ return hitResult . point . toString ( ) ;
298+ } , path . bounds . topLeft . toString ( ) ) ;
299+ }
114300} ) ;
0 commit comments