fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
56 lines (53 loc) • 2 kB
text/typescript
import type { TransformActionHandler } from '../EventTypeDefs';
import { CENTER, LEFT, RESIZING, RIGHT } from '../constants';
import { resolveOrigin } from '../util/misc/resolveOrigin';
import { getLocalPoint, isTransformCentered } from './util';
import { wrapWithFireEvent } from './wrapWithFireEvent';
import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';
/**
* Action handler to change object's width
* Needs to be wrapped with `wrapWithFixedAnchor` to be effective
* @param {Event} eventData javascript event that is doing the transform
* @param {Object} transform javascript object containing a series of information around the current transform
* @param {number} x current mouse x position, canvas normalized
* @param {number} y current mouse y position, canvas normalized
* @return {Boolean} true if some change happened
*/
export const changeObjectWidth: TransformActionHandler = (
eventData,
transform,
x,
y,
) => {
const localPoint = getLocalPoint(
transform,
transform.originX,
transform.originY,
x,
y,
);
// make sure the control changes width ONLY from it's side of target
if (
resolveOrigin(transform.originX) === resolveOrigin(CENTER) ||
(resolveOrigin(transform.originX) === resolveOrigin(RIGHT) &&
localPoint.x < 0) ||
(resolveOrigin(transform.originX) === resolveOrigin(LEFT) &&
localPoint.x > 0)
) {
const { target } = transform,
strokePadding =
target.strokeWidth / (target.strokeUniform ? target.scaleX : 1),
multiplier = isTransformCentered(transform) ? 2 : 1,
oldWidth = target.width,
newWidth =
Math.abs((localPoint.x * multiplier) / target.scaleX) - strokePadding;
target.set('width', Math.max(newWidth, 1));
// check against actual target width in case `newWidth` was rejected
return oldWidth !== target.width;
}
return false;
};
export const changeWidth = wrapWithFireEvent(
RESIZING,
wrapWithFixedAnchor(changeObjectWidth),
);