Skip to content

feat(): Additional controls examples in extensions #10558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- feat(): Additional controls examples in extensions [#10558](https://github.com/fabricjs/fabric.js/pull/10558)
- refactor(tests): move path tests from qunit to vitest [#10552](https://github.com/fabricjs/fabric.js/pull/10552)
- refactor(tests): Migrate Env, ClassRegistry, Rect to vitest [#10557](https://github.com/fabricjs/fabric.js/pull/10557)
- refactor(tests): move textbox tests from qunit to vitest [#10556](https://github.com/fabricjs/fabric.js/pull/10556)
Expand Down
101 changes: 66 additions & 35 deletions src/controls/changeWidth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
import type { TransformActionHandler } from '../EventTypeDefs';
import { CENTER, LEFT, RESIZING, RIGHT } from '../constants';
import { CENTER, LEFT, RESIZING, RIGHT, BOTTOM, TOP } from '../constants';
import { resolveOrigin } from '../util/misc/resolveOrigin';
import { getLocalPoint, isTransformCentered } from './util';
import { wrapWithFireEvent } from './wrapWithFireEvent';
import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';

const changeObjectSize =
(
originName: 'originX' | 'originY',
localCoord: 'x' | 'y',
scaleProp: 'scaleX' | 'scaleY',
prop: 'width' | 'height',
): TransformActionHandler =>
(eventData, transform, x, y): boolean => {
const localPoint = getLocalPoint(
transform,
transform.originX,
transform.originY,
x,
y,
);
const coord = localPoint[localCoord];
const origin = resolveOrigin(transform[originName]);
// make sure the control changes width ONLY from it's side of target
if (
origin === resolveOrigin(CENTER) ||
(origin === resolveOrigin(prop === 'width' ? RIGHT : BOTTOM) &&
coord < 0) ||
(origin === resolveOrigin(prop === 'width' ? LEFT : TOP) && coord > 0)
) {
const { target } = transform,
strokePadding =
target.strokeWidth / (target.strokeUniform ? target[scaleProp] : 1),
multiplier = isTransformCentered(transform) ? 2 : 1,
oldSize = target[prop],
newSize =
Math.abs((coord * multiplier) / target[scaleProp]) - strokePadding;
target.set(prop, Math.max(newSize, 1));
// check against actual target width in case `newWidth` was rejected
return oldSize !== target[prop];
}
return false;
};

/**
* Action handler to change object's width
* Needs to be wrapped with `wrapWithFixedAnchor` to be effective
Expand All @@ -14,42 +52,35 @@ import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';
* @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 changeObjectWidth = changeObjectSize(
'originX',
'x',
'scaleX',
'width',
);

/**
* Action handler to change object's height
* 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 changeObjectHeight = changeObjectSize(
'originY',
'y',
'scaleY',
'height',
);

export const changeWidth = wrapWithFireEvent(
RESIZING,
wrapWithFixedAnchor(changeObjectWidth),
);

export const changeHeight = wrapWithFireEvent(
RESIZING,
wrapWithFixedAnchor(changeObjectHeight),
);