scratch-paint/src/helper/guides.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-10-12 18:35:30 -04:00
import paper from '@scratch/paper';
2017-09-11 14:23:30 -04:00
import {getGuideLayer} from './layer';
import {getAllRootItems} from './selection';
2017-09-11 14:23:30 -04:00
const GUIDE_BLUE = '#009dec';
const GUIDE_GREY = '#aaaaaa';
const setDefaultGuideStyle = function (item) {
item.strokeWidth = 1 / paper.view.zoom;
item.opacity = 1;
item.blendMode = 'normal';
item.guide = true;
};
2017-12-08 18:27:23 -05:00
const hoverItem = function (item) {
const segments = item.segments;
2017-09-11 14:23:30 -04:00
const clone = new paper.Path(segments);
setDefaultGuideStyle(clone);
2017-12-08 18:27:23 -05:00
if (item.closed) {
2017-09-11 14:23:30 -04:00
clone.closed = true;
}
clone.parent = getGuideLayer();
2017-12-08 18:27:23 -05:00
clone.position = item.position;
2017-09-11 14:23:30 -04:00
clone.strokeColor = GUIDE_BLUE;
clone.fillColor = null;
clone.data.isHelperItem = true;
2017-12-08 18:27:23 -05:00
clone.data.origItem = item;
2017-09-11 14:23:30 -04:00
clone.bringToFront();
return clone;
};
2018-03-14 16:47:07 -04:00
const hoverBounds = function (item, expandBy) {
let bounds = item.internalBounds;
if (expandBy) {
bounds = bounds.expand(expandBy);
}
const rect = new paper.Path.Rectangle(bounds);
2017-09-11 14:23:30 -04:00
rect.matrix = item.matrix;
setDefaultGuideStyle(rect);
rect.parent = getGuideLayer();
rect.strokeColor = GUIDE_BLUE;
rect.fillColor = null;
rect.data.isHelperItem = true;
rect.data.origItem = item;
2017-09-11 14:23:30 -04:00
rect.bringToFront();
return rect;
};
const rectSelect = function (event, color) {
const half = new paper.Point(0.5 / paper.view.zoom, 0.5 / paper.view.zoom);
const start = event.downPoint.add(half);
const end = event.point.add(half);
const rect = new paper.Path.Rectangle(start, end);
const zoom = 1.0 / paper.view.zoom;
setDefaultGuideStyle(rect);
if (!color) color = GUIDE_GREY;
rect.parent = getGuideLayer();
rect.strokeColor = color;
rect.data.isRectSelect = true;
rect.data.isHelperItem = true;
rect.dashArray = [3.0 * zoom, 3.0 * zoom];
return rect;
};
2017-10-18 14:08:03 -04:00
const getGuideColor = function () {
return GUIDE_BLUE;
2017-09-11 14:23:30 -04:00
};
2017-09-22 12:12:07 -04:00
const _removePaperItemsByDataTags = function (tags) {
const allItems = getAllRootItems(true);
2017-09-22 12:12:07 -04:00
for (const item of allItems) {
for (const tag of tags) {
if (item.data && item.data[tag]) {
item.remove();
}
}
}
};
const _removePaperItemsByTags = function (tags) {
const allItems = getAllRootItems(true);
2017-09-22 12:12:07 -04:00
for (const item of allItems) {
for (const tag of tags) {
if (item[tag]) {
item.remove();
}
}
}
};
2017-11-07 16:00:21 -05:00
const removeBoundsPath = function () {
_removePaperItemsByDataTags(['isSelectionBound', 'isRotHandle', 'isScaleHandle']);
2017-09-11 14:23:30 -04:00
};
const removeBoundsHandles = function () {
_removePaperItemsByDataTags(['isRotHandle', 'isScaleHandle']);
};
2017-09-11 14:23:30 -04:00
const removeAllGuides = function () {
2017-09-22 12:12:07 -04:00
_removePaperItemsByTags(['guide']);
2017-09-11 14:23:30 -04:00
};
2017-10-18 14:08:03 -04:00
const removeHitPoint = function () {
_removePaperItemsByDataTags(['isHitPoint']);
};
const drawHitPoint = function (point) {
removeHitPoint();
if (point) {
const hitPoint = paper.Path.Circle(point, 4 / paper.view.zoom /* radius */);
hitPoint.strokeWidth = 1 / paper.view.zoom;
2017-10-18 14:08:03 -04:00
hitPoint.strokeColor = GUIDE_BLUE;
hitPoint.fillColor = new paper.Color(1, 1, 1, 0.5);
hitPoint.parent = getGuideLayer();
hitPoint.data.isHitPoint = true;
hitPoint.data.isHelperItem = true;
}
};
2017-09-11 14:23:30 -04:00
export {
hoverItem,
hoverBounds,
rectSelect,
removeAllGuides,
removeBoundsHandles,
2017-11-07 16:00:21 -05:00
removeBoundsPath,
2017-10-18 14:08:03 -04:00
drawHitPoint,
removeHitPoint,
2017-09-11 14:23:30 -04:00
getGuideColor,
setDefaultGuideStyle
};