Skip to content

Commit

Permalink
fix(vpt): move to absolutePointer
Browse files Browse the repository at this point in the history
fix(vpt): move to `absolutePointer`

test
  • Loading branch information
ShaMan123 committed Feb 4, 2023
1 parent 9d3b47b commit 65da3a2
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/brushes/BaseBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export abstract class BaseBrush<
* Same as FabricObject `clipPath` property.
* The clip path is positioned relative to the top left corner of the viewport.
* The `absolutePositioned` property renders the clip path w/o viewport transform.
* The clip path is prone to the `setCoords` gotcha.
*/
clipPath?: FabricObject;

Expand Down Expand Up @@ -143,7 +144,7 @@ export abstract class BaseBrush<
}

transform(ctx: CanvasRenderingContext2D) {
ctx.transform(...this.canvas.viewportTransform);
// noop
}

protected needsFullRender() {
Expand Down Expand Up @@ -213,8 +214,8 @@ export abstract class BaseBrush<
ctx.globalCompositeOperation = clipPath.inverted
? 'destination-out'
: 'destination-in';
if (clipPath.absolutePositioned) {
ctx.transform(...invertTransform(this.canvas.viewportTransform));
if (!clipPath.absolutePositioned) {
ctx.transform(...this.canvas.viewportTransform);
}
clipPath.transform(ctx);
ctx.scale(1 / clipPath.zoomX, 1 / clipPath.zoomY);
Expand Down Expand Up @@ -260,8 +261,11 @@ export abstract class BaseBrush<
sendObjectToPlane(
clipPath,
undefined,
this.clipPath.absolutePositioned
? multiplyTransformMatrices(this.canvas.viewportTransform, t)
!this.clipPath.absolutePositioned
? multiplyTransformMatrices(
invertTransform(this.canvas.viewportTransform),
t
)
: t
);
return clipPath;
Expand Down Expand Up @@ -297,6 +301,7 @@ export abstract class BaseBrush<
shadow: this.shadow ? new Shadow(this.shadow) : undefined,
clipPath: await this.createClipPath(shape),
});
sendObjectToPlane(shape, undefined, this.canvas.viewportTransform);
shape.setCoords();
}
this.onEnd(shape);
Expand Down
6 changes: 0 additions & 6 deletions src/brushes/DrawShapeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ export abstract class DrawShapeBase<
this.setStyles();
}

transform(ctx: CanvasRenderingContext2D) {
const t = this.canvas.viewportTransform;
const offset = new Point().transform(t);
ctx.transform(t[0], t[1], t[2], t[3], -offset.x, -offset.y);
}

protected _render(ctx: CanvasRenderingContext2D) {
ctx.save();
this.shape!.transform(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/brushes/PatternBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class PatternBrush extends PencilBrush {
* Creates "pattern" instance property
* @param {CanvasRenderingContext2D} ctx
*/
getPattern(ctx: CanvasRenderingContext2D) {
protected getPattern(ctx: CanvasRenderingContext2D) {
return ctx.createPattern(this.source || this.getPatternSrc(), 'repeat');
}

Expand Down
2 changes: 1 addition & 1 deletion src/brushes/SimpleBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export abstract class SimpleBrush<
}

protected extractPointer(ev: TFabricEvent<TPointerEventInfo>) {
return ev.pointer;
return ev.absolutePointer;
}

protected shouldHandleEvent({ e }: TPointerEventInfo) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/draw_shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
function fireBrushEvent(brush, type, pointer, { suffix = ':before', e = {} } = {}) {
brush.fire(`mouse:${type}${suffix}`, fabric.Event.init({
e,
pointer
pointer,
absolutePointer: brush.canvas._isRetinaScaling() ? brush.canvas.restorePointerVpt(pointer) : pointer
}));
}
hooks.before(() => {
Expand Down
3 changes: 2 additions & 1 deletion test/visual/freedraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function setBrush(canvas, brush) {
function fireBrushEvent(brush, type, pointer) {
brush.fire(`mouse:${type}:before`, fabric.Event.init({
e: { pointerId: 1 },
pointer
pointer,
absolutePointer: brush.canvas._isRetinaScaling() ? brush.canvas.restorePointerVpt(pointer) : pointer
}));
}
function pointDrawer(points, brush, onComplete = false, onMove = undefined) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 65da3a2

Please sign in to comment.