Skip to content

Commit fc826d4

Browse files
committed
Reset transforms during clip application to ensure proper coordinate handling
1 parent 22f8093 commit fc826d4

1 file changed

Lines changed: 10 additions & 31 deletions

File tree

src/core/p5.Renderer2D.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class Renderer2D extends Renderer {
305305
// Start a new path. Everything from here on out should become part of this
306306
// one path so that we can clip to the whole thing.
307307
this.clipPath = new Path2D();
308-
this.initialTransform = this.drawingContext.getTransform();
308+
this._clipBaseTransform = this.drawingContext.getTransform();
309309

310310
if (this._clipInvert) {
311311
// Slight hack: draw a big rectangle over everything with reverse winding
@@ -331,10 +331,11 @@ class Renderer2D extends Renderer {
331331
}
332332

333333
endClip() {
334-
const currentTransform = this.drawingContext.getTransform();
335-
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
334+
const savedTransform = this.drawingContext.getTransform();
335+
this.drawingContext.setTransform(this._clipBaseTransform);
336336
this.drawingContext.clip(this.clipPath);
337-
this.drawingContext.setTransform(currentTransform);
337+
this.drawingContext.setTransform(savedTransform);
338+
338339
this.clipPath = null;
339340

340341
super.endClip();
@@ -731,33 +732,11 @@ class Renderer2D extends Renderer {
731732
radiusY = h / 2;
732733
if (this._clipping) {
733734
const tempPath = new Path2D();
734-
const current = this.drawingContext.getTransform();
735-
// Transform coordinates manually but preserve scale signs
736-
const transformPoint = (x, y) => {
737-
return {
738-
x: current.a * x + current.c * y + current.e,
739-
y: current.b * x + current.d * y + current.f
740-
};
741-
};
742-
const transformedCenter = transformPoint(centerX, centerY);
743-
// Calculate transformed radii WITHOUT Math.abs() to preserve negative scaling
744-
const scaleX = Math.sqrt(current.a * current.a + current.c * current.c);
745-
const scaleY = Math.sqrt(current.b * current.b + current.d * current.d);
746-
747-
// Preserve the sign of the scaling for mirroring effects
748-
const signX = current.a < 0 ? -1 : 1;
749-
const signY = current.d < 0 ? -1 : 1;
750-
751-
const transformedRadiusX = scaleX * radiusX * signX;
752-
const transformedRadiusY = scaleY * radiusY * signY;
753-
tempPath.ellipse(
754-
transformedCenter.x,
755-
transformedCenter.y,
756-
Math.abs(transformedRadiusX),
757-
Math.abs(transformedRadiusY),
758-
0, 0, 2 * Math.PI
759-
);
760-
this.clipPath.addPath(tempPath);
735+
tempPath.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
736+
const currentTransform = this.drawingContext.getTransform();
737+
const ClipBaseTransform = this._clipBaseTransform.inverse();
738+
const relativeTransform = ClipBaseTransform.multiply(currentTransform);
739+
this.clipPath.addPath(tempPath, relativeTransform);
761740
} else {
762741
ctx.beginPath();
763742
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);

0 commit comments

Comments
 (0)