jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Add control of shading pattern's number of samples

Open Trisard opened this issue 3 years ago • 0 comments

Adding control of shading pattern's number of samples

Control of shading pattern's number of samples was implemented but not used, we can now specify (optionally) the number of samples wanted on shading pattern adding.

Example code

import { jsPDF } from "jspdf";

const doc = new jsPDF();

const pattern = doc.ShadingPattern(
    "axial",
    [10, 100, 100, 10],
    [
        { offset: 0, color: [255, 255, 0] },
        { offset: 0.05, color: [0, 0, 255] },
        { offset: 0.1, color: [255, 255, 0] },
        { offset: 0.15, color: [0, 0, 255] },
        { offset: 0.20, color: [255, 255, 0] },
        { offset: 0.25, color: [0, 0, 255] },
        { offset: 0.30, color: [255, 255, 0] },
        { offset: 0.35, color: [0, 0, 255] },
        { offset: 0.40, color: [255, 255, 0] },
        { offset: 0.45, color: [0, 0, 255] },
        { offset: 0.50, color: [255, 255, 0] },
        { offset: 0.55, color: [0, 0, 255] },
        { offset: 0.60, color: [255, 255, 0] },
        { offset: 0.65, color: [0, 0, 255] },
        { offset: 0.70, color: [255, 255, 0] },
        { offset: 0.75, color: [0, 0, 255] },
        { offset: 0.80, color: [255, 255, 0] },
        { offset: 0.85, color: [0, 0, 255] },
        { offset: 0.90, color: [255, 255, 0] },
        { offset: 0.95, color: [0, 0, 255] },
        { offset: 1, color: [255, 255, 0] },
    ]
);

doc.advancedAPI(doc => {
    doc.addShadingPattern("testPattern", pattern, 5);
});

doc.path([
    { op: "m", c: [10, 10] },
    { op: "l", c: [100, 10] },
    { op: "l", c: [100, 10] },
    { op: "l", c: [100, 100] },
    { op: "l", c: [10, 100] }
]);

doc.fill({key:"testPattern"});

doc.save();

Result with a number of samples at 5

image

Result with a number of samples at 80

For this example, any number equal or upper 21 gonna give this result

image

Trisard avatar Aug 24 '22 21:08 Trisard