javascript - 将带有渐变描边的 svg 导入到 paper.js 项目中

标签 javascript svg canvas paperjs

Paper.js 不显示带有从 SVG 导入的渐变的路径。

这是一个示例 https://codepen.io/Husband/pen/LoomQo 正如您所看到的,显示了带有描边颜色红色的路径,并且隐藏了带有渐变的路径。

<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>

最佳答案

这是由于 paper.js 当前 SVG 导入实现中存在错误,该实现不遵循 SVG 规范(x2 默认值应为 100%)。
我举报了issue我会尽快修复它。

作为解决方法,您可以将 x2 值设置为 100%,这将按预期工作。
这是sketch演示解决方法。

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);

关于javascript - 将带有渐变描边的 svg 导入到 paper.js 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56476565/

相关文章:

javascript - 有没有办法使用 javascript 来阻止 javascript?

javascript - 根据ids选择多个元素,得到相邻的最大组

javascript - 使基于 Canvas 滚动的图像序列动画更加流畅

javascript - 从 HTML Canvas 中提取的 ArrayBuffer 与使用 UTIF 解码并加载到其上的 ArrayBuffer 不同

javascript - 如何防止双击时在 HTML5 Canvas 外选择文本?

javascript - 使用 Java 或 JavaScript 将 HTML 页面与 CSS 一起转换为 PDF

javascript - ajaxForm提交成功后触发action

javascript - 如何访问 JSON 中的数组并在 Jade 中迭代

xml - HTML 中嵌入的 SVG 中的重要空白

html - 内联 SVG 过滤器作为数据 URI