Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SVG Plugin]: SVG exporting does not carry over path specific stokeStyles #3

Open
Zettersten opened this issue Apr 7, 2023 · 2 comments

Comments

@Zettersten
Copy link

@luileito thanks for a great plugin. Super easy to use.

I have the following code in the mousedownBefore event.

mousedownBefore: function (elem, data, evt) {
    var brushType = getBrushType();
    if (brushType == 'eraser') {
        // There is a method to set the brush in eraser mode.
        data.options.graphics.lineWidth = 20;
        data.sketch.eraser();
    } else if (brushType == 'pencil') {

        const color = getBrushColor() ?? "black";

        // There is a method to get the default mode (pencil) back.
        data.options.graphics.lineWidth = 2;
        data.options.graphics.strokeStyle = color;

        data.sketch.saveGraphics(data.options.graphics);
        data.sketch.pencil();
    }
}

The function named getBrushColor is similar to your getBrushType function. It returns a HEX color of the color selected on the page.

Example UI:

image

However, when I execute the "svg.create" method the contents returned only use black.

test

Am I doing something wrong? Is this supported?

@Zettersten
Copy link
Author

I ended up writing:

function changeGraphics(data, graphicOptions) {
    const options = {
        fillStyle: graphicOptions.fillStyle || '#F00',
        strokeStyle: graphicOptions.strokeStyle || '#F0F',
        lineWidth: graphicOptions.lineWidth || 2,
        lineCap: graphicOptions.lineCap || 'round',
        lineJoin: graphicOptions.lineJoin || 'round',
        miterLimit: graphicOptions.miterLimit || 10,
    };
    for (const opt in options) {
        const val = options[opt];
        data.sketch.callStack.push({ property: opt, value: val });
    }
}

Which seems to be working.

The whole thing:

mousedownBefore: function (elem, data, evt) {
    var brushType = getBrushType();
    if (brushType == 'eraser') {
        // There is a method to set the brush in eraser mode.
        data.options.graphics.lineWidth = 20;
        data.sketch.eraser();
    } else if (brushType == 'pencil') {

        const color = getBrushColor() ?? "black";

        // There is a method to get the default mode (pencil) back.
        data.options.graphics.lineWidth = 2;
        data.options.graphics.strokeStyle = color;

        changeGraphics(data, data.options.graphics);
        data.sketch.pencil();
    }
}

@luileito
Copy link
Owner

luileito commented Apr 7, 2023

Hi, thanks for reporting. I believe you can use saveGraphics(obj) followed by restoreGraphics(). For instance:

var brush = new jSketch('yourCanvas');
brush.saveGraphics({strokeStyle:'lime'}).restoreGraphics();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants