Import svg with gradient stroke to paper.js project

非 Y 不嫁゛ 提交于 2021-01-04 07:26:28

问题


Paper.js don't show path with gradient imported from SVG.

Here is an example https://codepen.io/Husband/pen/LoomQo As you can see a path with stroke color red is displayed, and path with a gradient is hidden.

<canvas id="canvas" resize></canvas>
<svg xmlns="http://www.w3.org/2000/svg" width="1440" height="593" viewBox="0 0 1440 593" id="svg" style="display: none">
    <defs>
        <linearGradient id="a" x1="3.463%" y1="53.239%" y2="53.239%">
            <stop offset="0%" stop-color="#2CC2FE" stop-opacity="0"/>
            <stop offset="49.904%" stop-color="#24C1FF"/>
            <stop offset="100%" stop-color="#3AC6FE" stop-opacity="0"/>
        </linearGradient>
    </defs>
    <g id="curves" fill="none" fill-rule="evenodd" stroke-width=".271">
        <path stroke="url(#a)" d="M.51 345.572s130.835 62.466 339.31 62.466c208.473 0 264.545-82.37 527.654-82.37 263.108 0 317.742 179.132 671.43 179.132" transform="translate(-81 25)"/>
        <path stroke="red" d="M.51 342.824s137.305 69.09 345.78 69.09c208.473 0 258.075-94.184 521.184-94.184 263.108 0 317.742 179.541 671.43 179.541" transform="translate(-81 25)"/>
        </g>
    </g>
</svg>
    <script type="text/paperscript" canvas="canvas">
        var item = project.importSVG(document.getElementById('svg'));
        item.visible = true; 
        var group = item.children.curves;
        item.fitBounds(view.bounds);
        item.scale(2);
    </script>

回答1:


This is due to a bug in paper.js current SVG import implementation which doesn't follow SVG spec (x2 default value should be 100%).
I reported the issue and I will fix it soon.

As a workaround, you can set x2 value to 100% and this will work as expected.
Here is a sketch demonstrating the workaround.

const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="1440" height="593" viewBox="0 0 1440 593" id="svg" style="display: none">\n' +
    '    <defs>\n' +
    '        <linearGradient id="a" x1="3.463%" y1="53.239%" x2="100%" y2="53.239%">\n' +
    '            <stop offset="0%" stop-color="#2CC2FE" stop-opacity="0"/>\n' +
    '            <stop offset="49.904%" stop-color="#24C1FF"/>\n' +
    '            <stop offset="100%" stop-color="#3AC6FE" stop-opacity="0"/>\n' +
    '        </linearGradient>\n' +
    '    </defs>\n' +
    '    <g id="curves" fill="none" fill-rule="evenodd" stroke-width=".271">\n' +
    '        <path stroke="url(#a)" d="M.51 345.572s130.835 62.466 339.31 62.466c208.473 0 264.545-82.37 527.654-82.37 263.108 0 317.742 179.132 671.43 179.132" transform="translate(-81 25)"/>\n' +
    '        <path stroke="red" d="M.51 342.824s137.305 69.09 345.78 69.09c208.473 0 258.075-94.184 521.184-94.184 263.108 0 317.742 179.541 671.43 179.541" transform="translate(-81 25)"/>\n' +
    '    </g>\n' +
    '</svg>';

var item = project.importSVG(svg);
item.visible = true;
var group = item.children.curves;
item.fitBounds(view.bounds);
item.scale(2);


来源:https://stackoverflow.com/questions/56476565/import-svg-with-gradient-stroke-to-paper-js-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!