js-draw
Version:
Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript.
40 lines (39 loc) • 1.71 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const RenderablePathSpec_1 = require("../../../rendering/RenderablePathSpec");
class SelectionBuilder {
/** Renders a preview of the selection bounds */
render(renderer, color) {
renderer.drawPath((0, RenderablePathSpec_1.pathToRenderable)(this.previewPath(), { fill: color }));
}
/** Converts the selection preview into a set of selected elements */
resolve(image, viewport) {
const path = this.previewPath();
const filterComponents = (components) => {
return components.filter((component) => {
return component.isSelectable();
});
};
let components;
// If the bounding box is very small, search for items **near** the bounding box,
// rather than in the bounding box.
const clickSize = viewport.getSizeOfPixelOnCanvas() * 3;
const isClick = path.bbox.maxDimension <= clickSize;
if (isClick) {
const searchRegionSize = viewport.visibleRect.maxDimension / 200;
const minSizeBox = path.bbox.grownBy(searchRegionSize);
components = image.getComponentsIntersecting(minSizeBox).filter((component) => {
return minSizeBox.containsRect(component.getBBox()) || component.intersectsRect(minSizeBox);
});
components = filterComponents(components);
if (components.length > 1) {
components = [components[0]];
}
}
else {
components = filterComponents(this.resolveInternal(image));
}
return components;
}
}
exports.default = SelectionBuilder;
;