fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
112 lines (109 loc) • 3.07 kB
JavaScript
import { getDistanceList } from './basic.mjs';
function collectVerticalPoint(props) {
const {
target,
isScale,
isUniform,
corner,
point,
diagonalPoint,
list,
isCenter
} = props;
const {
dis,
arr
} = getDistanceList(point, list, 'x');
const margin = this.margin / this.canvas.getZoom();
if (dis > margin) return [];
let v = arr[arr.length - 1].x - point.x;
// tl bl ml
// If modifying on the left side, the size decreases; conversely, it increases.
const dirX = corner.includes('l') ? -1 : 1;
v *= dirX;
const {
width,
height,
scaleX,
scaleY
} = target;
// Because when modifying through the center point, isUniform is always false, so skew does not need to be considered.
const dStrokeWidth = target.strokeUniform ? 0 : target.strokeWidth;
const scaleWidth = scaleX * width + dStrokeWidth;
const sx = (v + scaleWidth) / scaleWidth;
// When v equals -scaleWidth, sx equals 0.
if (sx == 0) return [];
if (isScale) {
target.set('scaleX', scaleX * sx);
if (isUniform) target.set('scaleY', scaleY * sx);
} else {
target.set('width', width * sx);
if (isUniform) target.set('height', height * sx);
}
if (isCenter) {
target.setRelativeXY(diagonalPoint, 'center', 'center');
} else {
const originArr = this.contraryOriginMap;
target.setRelativeXY(diagonalPoint, ...originArr[corner]);
}
target.setCoords();
return arr.map(target => ({
origin: point,
target
}));
}
function collectHorizontalPoint(props) {
const {
target,
isScale,
isUniform,
corner,
point,
diagonalPoint,
list,
isCenter
} = props;
const {
dis,
arr
} = getDistanceList(point, list, 'y');
const margin = this.margin / this.canvas.getZoom();
if (dis > margin) return [];
let v = arr[arr.length - 1].y - point.y;
// tl mt tr
// If modifying on the top side, the size decreases; conversely, it increases.
const dirY = corner.includes('t') ? -1 : 1;
v *= dirY;
const {
width,
height,
scaleX,
scaleY
} = target;
// Because when modifying through the center point, isUniform is always false, so skew does not need to be considered.
const dStrokeWidth = target.strokeUniform ? 0 : target.strokeWidth;
const scaleHeight = scaleY * height + dStrokeWidth;
const sy = (v + scaleHeight) / scaleHeight;
// When v equals -scaleHeight, sy equals 0.
if (sy == 0) return [];
if (isScale) {
target.set('scaleY', scaleY * sy);
if (isUniform) target.set('scaleX', scaleX * sy);
} else {
target.set('height', height * sy);
if (isUniform) target.set('width', width * sy);
}
if (isCenter) {
target.setRelativeXY(diagonalPoint, 'center', 'center');
} else {
const originArr = this.contraryOriginMap;
target.setRelativeXY(diagonalPoint, ...originArr[corner]);
}
target.setCoords();
return arr.map(target => ({
origin: point,
target
}));
}
export { collectHorizontalPoint, collectVerticalPoint };
//# sourceMappingURL=collect-point.mjs.map