mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Add HitResult tests (work in progress).
This commit is contained in:
parent
61052a9da9
commit
1ffb8debc3
2 changed files with 117 additions and 1 deletions
|
@ -124,7 +124,9 @@ if (window.tests) {
|
|||
'test/tests/Path_Length.js',
|
||||
'test/tests/CompoundPath.js',
|
||||
|
||||
'test/tests/PlacedSymbol.js'
|
||||
'test/tests/PlacedSymbol.js',
|
||||
|
||||
'test/tests/HitResult.js'
|
||||
);
|
||||
}
|
||||
|
||||
|
|
114
test/tests/HitResult.js
Normal file
114
test/tests/HitResult.js
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Paper.js
|
||||
*
|
||||
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
||||
* based on Scriptographer.org and designed to be largely API compatible.
|
||||
* http://paperjs.org/
|
||||
* http://scriptographer.org/
|
||||
*
|
||||
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
||||
* http://lehni.org/ & http://jonathanpuckey.com/
|
||||
*
|
||||
* Distributed under the MIT license. See LICENSE file for details.
|
||||
*
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
module('HitResult');
|
||||
|
||||
test('hitting a filled shape', function() {
|
||||
var path = new Path.Circle([50, 50], 50);
|
||||
|
||||
var hitResult = path.hitTest([75, 75]);
|
||||
equals(function() {
|
||||
return hitResult == null;
|
||||
}, true, 'Since the path is not filled, the hit-test should return null');
|
||||
|
||||
path.fillColor = 'red';
|
||||
hitResult = path.hitTest([75, 75]);
|
||||
equals(function() {
|
||||
return hitResult.type == 'fill';
|
||||
}, true);
|
||||
equals(function() {
|
||||
return hitResult.item == path;
|
||||
}, true);
|
||||
});
|
||||
|
||||
test('the item on top should be returned', function() {
|
||||
var path = new Path.Circle([50, 50], 50);
|
||||
path.fillColor = 'red';
|
||||
|
||||
// The cloned path is lying above the path:
|
||||
var copy = path.clone();
|
||||
|
||||
var hitResult = paper.project.hitTest([75, 75]);
|
||||
equals(function() {
|
||||
return hitResult.item == copy;
|
||||
}, true);
|
||||
});
|
||||
|
||||
test('hitting a stroked path', function() {
|
||||
var path = new Path([0, 0], [50, 0]);
|
||||
|
||||
// We are hit testing with an offset of 5pt on a path with a stroke width
|
||||
// of 10:
|
||||
|
||||
var hitResult = paper.project.hitTest([25, 5]);
|
||||
equals(function() {
|
||||
return hitResult == null;
|
||||
}, true, 'Since the path is not stroked yet, the hit-test should return null');
|
||||
|
||||
path.strokeColor = 'black';
|
||||
path.strokeWidth = 10;
|
||||
hitResult = path.hitTest([25, 5]);
|
||||
equals(function() {
|
||||
return hitResult.type == 'stroke';
|
||||
}, true);
|
||||
equals(function() {
|
||||
return hitResult.item == path;
|
||||
}, true);
|
||||
});
|
||||
|
||||
test('hitting path handles', function() {
|
||||
var path = new Path(new Segment({
|
||||
point: [0, 0],
|
||||
handleIn: [-50, -50],
|
||||
handleOut: [50, 50]
|
||||
}));
|
||||
|
||||
var hitResult = paper.project.hitTest([50, 50], {
|
||||
handles: true
|
||||
});
|
||||
|
||||
equals(function() {
|
||||
return !!hitResult;
|
||||
}, true, 'A HitResult should be returned (1)');
|
||||
|
||||
if (hitResult) {
|
||||
equals(function() {
|
||||
return hitResult.type;
|
||||
}, 'handle-out');
|
||||
|
||||
equals(function() {
|
||||
return hitResult.item == path;
|
||||
}, true);
|
||||
}
|
||||
|
||||
var hitResult = paper.project.hitTest([-50, -50], {
|
||||
handles: true
|
||||
});
|
||||
|
||||
equals(function() {
|
||||
return !!hitResult;
|
||||
}, true, 'A HitResult should be returned (2)');
|
||||
|
||||
if (hitResult) {
|
||||
equals(function() {
|
||||
return hitResult.type;
|
||||
}, 'handle-in');
|
||||
|
||||
equals(function() {
|
||||
return hitResult.item == path;
|
||||
}, true);
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue