Skip to content
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

fix(IText): drag image + retina #8712

Merged
merged 7 commits into from
Feb 21, 2023
Merged
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]

- fix(DraggableText): drag image blur [#8712](https://github.com/fabricjs/fabric.js/pull/8712)
- ci(): Fix tests for firefox 110 update [#8710](https://github.com/fabricjs/fabric.js/pull/8710)
- chore(): index files for exports and tree shaking [#8661](https://github.com/fabricjs/fabric.js/pull/8661)
- ci(test): cleanup node config (#8694 followup) [#8707](https://github.com/fabricjs/fabric.js/issues/8707)
Expand Down
40 changes: 14 additions & 26 deletions src/shapes/IText/DraggableTextDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { Point } from '../../Point';
import type { IText } from './IText';
import { setStyle } from '../../util/dom_style';
import { cloneDeep } from '../../util/internals/cloneDeep';
import { createCanvasElement } from '../../util/misc/dom';
import { isIdentityMatrix } from '../../util/misc/matrix';
import { TextStyleDeclaration } from '../Text/StyledText';

/**
Expand Down Expand Up @@ -140,37 +138,27 @@ export class DraggableTextDelegate {
};
target.setSelectionStyles(styleOverride, 0, selectionStart);
target.setSelectionStyles(styleOverride, selectionEnd, target.text.length);
let dragImage = target.toCanvasElement({
target.dirty = true;
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
const dragImage = target.toCanvasElement({
enableRetinaScaling,
viewportTransform: true,
});
// restore values
target.backgroundColor = bgc;
target.styles = styles;
// handle retina scaling and vpt
if (retinaScaling > 1 || !isIdentityMatrix(vpt)) {
const dragImageCanvas = createCanvasElement();
const size = new Point(dragImage.width, dragImage.height)
.scalarDivide(retinaScaling)
.transform(vpt, true);
dragImageCanvas.width = size.x;
dragImageCanvas.height = size.y;
const ctx = dragImageCanvas.getContext('2d')!;
ctx.scale(1 / retinaScaling, 1 / retinaScaling);
const [a, b, c, d] = vpt;
ctx.transform(a, b, c, d, 0, 0);
ctx.drawImage(dragImage, 0, 0);
dragImage = dragImageCanvas;
}
this.__dragImageDisposer && this.__dragImageDisposer();
this.__dragImageDisposer = () => {
dragImage.remove();
};
target.dirty = true;
// position drag image offscreen
setStyle(dragImage, {
position: 'absolute',
left: -dragImage.width + 'px',
position: 'fixed',
left: `${-dragImage.width}px`,
border: 'none',
width: `${dragImage.width / retinaScaling}px`,
height: `${dragImage.height / retinaScaling}px`,
});
this.__dragImageDisposer && this.__dragImageDisposer();
this.__dragImageDisposer = () => {
dragImage.remove();
};
getDocument().body.appendChild(dragImage);
e.dataTransfer?.setDragImage(dragImage, offset.x, offset.y);
}
Expand Down Expand Up @@ -301,7 +289,7 @@ export class DraggableTextDelegate {
} else if (insertAt > selectionEnd) {
insertAt -= selectionEnd - selectionStart;
}
target.insertChars('', undefined, selectionStart, selectionEnd);
target.removeChars(selectionStart, selectionEnd);
// prevent `dragend` from handling event
delete this.__dragStartSelection;
}
Expand Down Expand Up @@ -365,7 +353,7 @@ export class DraggableTextDelegate {
} else {
target.clearContextTop();
if (dropEffect === 'move') {
target.insertChars('', undefined, selectionStart, selectionEnd);
target.removeChars(selectionStart, selectionEnd);
target.selectionStart = target.selectionEnd = selectionStart;
target.hiddenTextarea &&
(target.hiddenTextarea.value = target.text);
Expand Down
5 changes: 5 additions & 0 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
resetObjectTransform,
saveObjectTransform,
} from '../../util/misc/objectTransforms';
import { sendObjectToPlane } from '../../util/misc/planeChange';
import { pick } from '../../util/misc/pick';
import { toFixed } from '../../util/misc/toFixed';
import type { Group } from '../Group';
Expand Down Expand Up @@ -1651,6 +1652,7 @@ export class FabricObject<
* @param {Boolean} [options.enableRetinaScaling] Enable retina scaling for clone image. Introduce in 1.6.4
* @param {Boolean} [options.withoutTransform] Remove current object transform ( no scale , no angle, no flip, no skew ). Introduced in 2.3.4
* @param {Boolean} [options.withoutShadow] Remove current object shadow. Introduced in 2.4.2
* @param {Boolean} [options.viewportTransform] Account for canvas viewport transform
* @return {HTMLCanvasElement} Returns DOM element <canvas> with the FabricObject
*/
toCanvasElement(options: any = {}) {
Expand All @@ -1669,6 +1671,9 @@ export class FabricObject<
if (options.withoutShadow) {
this.shadow = null;
}
if (options.viewportTransform) {
sendObjectToPlane(this, this.getViewportTransform());
}

const el = createCanvasElement(),
// skip canvas zoom and calculate with setCoords now.
Expand Down
Binary file modified test/visual/golden/drag_image_vpt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/visual/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@
golden: 'drag_image.png',
width: 120,
height: 220,
percentage: 0.04,
percentage: 0.01,
fabricClass: 'Canvas'
});

Expand All @@ -529,7 +529,7 @@
golden: 'drag_image.png',
width: 110,
height: 250,
percentage: 0.04,
percentage: 0.01,
fabricClass: 'Canvas'
});

Expand All @@ -540,7 +540,7 @@
golden: 'drag_image_vpt.png',
width: 220,
height: 250,
percentage: 0.04,
percentage: 0.01,
fabricClass: 'Canvas'
});

Expand All @@ -551,7 +551,7 @@
golden: 'drag_image_vpt.png',
width: 220,
height: 250,
percentage: 0.04,
percentage: 0.01,
fabricClass: 'Canvas'
});

Expand Down