好的,这就是答案!
http://jsfiddle.net/fqaJM/
<canvas id="myCanvas" width="256" height="128"></canvas>
<div id="xycoordinates"></div>
#myCanvas {
border: 1px solid #c3c3c3;
}
#xycoordinates {
font: 9pt sans-serif;
}
var canvas = document.getElementById("myCanvas"); // Get canvas
// Some initialisation stuff to make things work out of PaperScript context
// Have to have things to be done this way because jsfiddle don't allow to save source with script type="text/paperscript"
paper.install(window);
paper.setup(canvas);
var myPath = new paper.Path.Circle([64, 64], 32); // Red one, with 'pointer' cursor on it
myPath.style = {
fillColor: '#FF0000'
};
var scndPath = new paper.Path.Circle([192, 64], 32); // Green one, without cursor accent
scndPath.style = {
fillColor: '#00FF00'
};
paper.view.draw(); // Have to call manually when working from JavaScript directly
var hitOptions = { // Trigger hit only on 'fill' part of the path with 0 tolerance
segments: false,
stroke: false,
fill: true,
tolerance: 0
};
var tool = new paper.Tool(); // Again manually. Life is much easier with script type="text/paperscript"
tool.onMouseMove = function (event) { // Installig paperjs event
var x = event.point.x;
var y = event.point.y;
document.getElementById("xycoordinates").innerHTML = "Coordinates: (" + x + "," + y + ")";
var hitResult = myPath.hitTest(event.point, hitOptions); // isPointInPath
if (hitResult) {
document.body.style.cursor = "pointer";
} else {
document.body.style.cursor = "default";
}
};
关键是我错过了 paperjs 有它自己的onMouseMove
and hitTest()
,它是isPointInPath()
包装器。
不知道它是怎么发生的,因为我已经在项目中使用它了!也许需要休息 %)
无论如何,仍然存在一些问题:它看起来hitTest()
会引发一些奇怪的误报,有时它不会在它应该触发的地方触发。使用 (46,96) 和 (77,64) 坐标检查点!
UPD:刚刚在一个本地 HTML 文件中测试了相同的代码以获得相同的工件: http: //pastebin.com/HiYgKnw0