scratch-paint/src/helper/hover.js

39 lines
1 KiB
JavaScript
Raw Normal View History

2017-09-11 10:52:00 -04:00
import paper from 'paper';
2017-09-11 14:23:30 -04:00
import {isBoundsItem, getRootItem} from './item';
import {hoverBounds, hoverItem} from './guides';
import {isGroupChild} from './group';
2017-09-11 10:52:00 -04:00
/**
2017-09-11 14:23:30 -04:00
* @param {!MouseEvent} event mouse event
* @param {?object} hitOptions hit options to use
* @return {paper.Item} the hovered item or null if there is none
2017-09-11 10:52:00 -04:00
*/
2017-09-11 14:23:30 -04:00
const getHoveredItem = function (event, hitOptions) {
2017-09-11 10:52:00 -04:00
const hitResults = paper.project.hitTestAll(event.point, hitOptions);
if (hitResults.length === 0) {
return null;
}
let hitResult;
for (const result of hitResults) {
if (!(result.item.data && result.item.data.noHover) && !hitResult.item.selected) {
hitResult = result;
break;
}
}
if (!hitResult) {
return null;
}
2017-09-11 14:23:30 -04:00
if (isBoundsItem(hitResult.item)) {
return hoverBounds(hitResult.item);
} else if (isGroupChild(hitResult.item)) {
return hoverBounds(getRootItem(hitResult.item));
2017-09-11 10:52:00 -04:00
}
2017-09-11 14:23:30 -04:00
return hoverItem(hitResult);
2017-09-11 10:52:00 -04:00
};
export {
2017-09-11 14:23:30 -04:00
getHoveredItem
2017-09-11 10:52:00 -04:00
};