Skip to content

Commit a5e1523

Browse files
committed
Fix transforms in clip() by using shape system for primitives using manual method
1 parent fc8ca8a commit a5e1523

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/core/p5.Renderer2D.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ class Renderer2D extends Renderer {
7070
}
7171
this.scale(this._pixelDensity, this._pixelDensity);
7272

73-
if(!this.filterRenderer){
74-
this.filterRenderer = new FilterRenderer2D(this);
75-
}
7673
// Set and return p5.Element
7774
this.wrappedElt = new Element(this.elt, this._pInst);
7875
this.clipPath = null;
7976
}
8077

78+
get filterRenderer() {
79+
if (!this._filterRenderer) {
80+
this._filterRenderer = new FilterRenderer2D(this);
81+
}
82+
return this._filterRenderer;
83+
}
84+
8185
remove(){
8286
this.wrappedElt.remove();
8387
this.wrappedElt = null;
@@ -301,6 +305,7 @@ class Renderer2D extends Renderer {
301305
// Start a new path. Everything from here on out should become part of this
302306
// one path so that we can clip to the whole thing.
303307
this.clipPath = new Path2D();
308+
this.initialClipTransform = this.drawingContext.getTransform().inverse();
304309

305310
if (this._clipInvert) {
306311
// Slight hack: draw a big rectangle over everything with reverse winding
@@ -701,7 +706,7 @@ class Renderer2D extends Renderer {
701706
}
702707

703708
ellipse(args) {
704-
const ctx = this.clipPath || this.drawingContext;
709+
const ctx = this.drawingContext;
705710
const doFill = !!this.states.fillColor,
706711
doStroke = this.states.strokeColor;
707712
const x = parseFloat(args[0]),
@@ -721,17 +726,28 @@ class Renderer2D extends Renderer {
721726
centerY = y + h / 2,
722727
radiusX = w / 2,
723728
radiusY = h / 2;
724-
if (!this._clipping) ctx.beginPath();
725-
726-
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
727-
ctx.closePath();
729+
if (this._clipping) {
730+
const tempPath = new Path2D();
731+
tempPath.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
728732

729-
if (!this._clipping && doFill) {
730-
ctx.fill();
731-
}
732-
if (!this._clipping && doStroke) {
733-
ctx.stroke();
733+
const currentTransform = this.drawingContext.getTransform();
734+
const initialClip = this.initialClipTransform;
735+
const relativeTransform = initialClip.multiply(currentTransform);
736+
this.clipPath.addPath(tempPath, relativeTransform);
737+
} else {
738+
// Normal drawing (existing code)
739+
ctx.beginPath();
740+
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
741+
ctx.closePath();
742+
if (doFill) {
743+
ctx.fill();
744+
}
745+
if (doStroke) {
746+
ctx.stroke();
747+
}
734748
}
749+
750+
return this;
735751
}
736752

737753
line(x1, y1, x2, y2) {

0 commit comments

Comments
 (0)