Skip to content
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
14 changes: 12 additions & 2 deletions src/modules/context2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
this.globalCompositeOperation = ctx.globalCompositeOperation || "normal";
this.globalAlpha = ctx.globalAlpha || 1.0;
this.clip_path = ctx.clip_path || [];
this.clip_path_rule = ctx.clip_path_rule;
this.currentPoint = ctx.currentPoint || new Point();
this.miterLimit = ctx.miterLimit || 10.0;
this.lastPoint = ctx.lastPoint || new Point();
Expand Down Expand Up @@ -869,10 +870,19 @@ import {
*
* @name clip
* @function
* @param {"nonzero"|"evenodd"} [fillRule="nonzero"] The algorithm by which to determine if a point is inside or outside the clipping region.
* @description The clip() method clips a region of any shape and size from the original canvas.
*/
Context2D.prototype.clip = function() {
Context2D.prototype.clip = function(fillRule) {
// The optional path argument (clip(path[, fillRule])) is not supported, so
// pick the fill rule from whichever argument is a string.
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === "string") {
fillRule = arguments[i];
}
}
this.ctx.clip_path = JSON.parse(JSON.stringify(this.path));
this.ctx.clip_path_rule = fillRule;
pathPreProcess.call(this, null, true);
};

Expand Down Expand Up @@ -2254,7 +2264,7 @@ import {
};

var doClip = function() {
this.pdf.clip();
this.pdf.clip(this.ctx.clip_path_rule);
this.pdf.discardPath();
};

Expand Down
34 changes: 34 additions & 0 deletions test/specs/context2d.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,40 @@ describe("Context2D: standard tests", () => {
]);
});

it("clip uses the nonzero rule by default", () => {
var doc = new jsPDF({ floatPrecision: 2 });
var ctx = doc.context2d;
var writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);

ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(10, 0);
ctx.lineTo(10, 10);
ctx.closePath();
ctx.clip();

expect(writeArray).toContain("W");
expect(writeArray).not.toContain("W*");
});

it("clip respects the evenodd fill rule", () => {
var doc = new jsPDF({ floatPrecision: 2 });
var ctx = doc.context2d;
var writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);

ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(10, 0);
ctx.lineTo(10, 10);
ctx.closePath();
ctx.clip("evenodd");

expect(writeArray).toContain("W*");
expect(writeArray).not.toContain("W");
});

it("margin property shorthands", () => {
const doc = new jsPDF();
const ctx = doc.context2d;
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ declare module "jspdf" {
y: number
): void;
clearRect(x: number, y: number, w: number, h: number): void;
clip(): jsPDF;
clip(fillRule?: "nonzero" | "evenodd"): jsPDF;
clipEvenOdd(): jsPDF;
closePath(): void;
createLinearGradient(
Expand Down