使用 Javascript 以编程方式创建 SVG 标记

发布于 2024-09-10 18:39:29 字数 2899 浏览 2 评论 0原文

是否可以使用 Javascript 在 SVG 中创建标记,然后将其应用到新创建的行?如果是这样,请告诉我下面的代码有什么问题。我希望红线和绿线都有箭头,但在 Chrome 和 Firefox 3.6 中只有绿线有。

<?php

header('Content-type: application/xhtml+xml');
echo '<?xml version="1.0" encoding="utf-8" ?>';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>SVG test</title>
<script type="text/javascript">
function init()
{
    var div = document.getElementById('mainDiv');

    var svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

    svgNode.style.width = "200px";
    svgNode.style.height = "200px";
    svgNode.style.overflow = 'visible';
    svgNode.style.position = 'absolute';
    svgNode.setAttribute('version', '1.1');
    svgNode.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
    div.appendChild(svgNode);

    var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
    var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
    marker.setAttribute('id', 'Triangle');
    marker.setAttribute('viewBox', '0 0 10 10');
    marker.setAttribute('refX', '0');
    marker.setAttribute('refY', '5');
    marker.setAttribute('markerUnits', 'strokeWidth');
    marker.setAttribute('markerWidth', '4');
    marker.setAttribute('markerHeight', '3');
    marker.setAttribute('orient', 'auto');
    var path = document.createElementNS('http;//www.w3.org/2000/svg', 'path');
    marker.appendChild(path);
    path.setAttribute('d', 'M 0 0 L 10 5 L 0 10 z');

    svgNode.appendChild(defs);
    defs.appendChild(marker);

    var obj = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    obj.setAttribute('x1', 50);
    obj.setAttribute('y1', 50);
    obj.setAttribute('x2', 50);
    obj.setAttribute('y2', 150);
    obj.setAttribute('stroke', '#ff0000');
    obj.setAttribute('stroke-width', 7);
    obj.setAttribute('marker-end', 'url(#Triangle)');

    svgNode.appendChild(obj);


}
</script>
</head>

<body onload="init();">
<div id="mainDiv" style="width: 100%; height: 100%; visibility: visible; overflow: visible; position: absolute; background: white;">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="width: 200px; height: 200px; position: absolute;">
        <defs>
            <marker id="Triangle-static" viewBox="0 0 10 10" refX="0" refY="5" markerUnits="strokeWidth" markerWidth="4" markerHeight="3" orient="auto">
                <path d="M 0 0 L 10 5 L 0 10 z"></path>
            </marker>
        </defs>
        <line x1="150" y1="50" x2="150" y2="150" stroke="#00ff00" stroke-width="7" marker-end="url(#Triangle-static)" />
    </svg>
</div>
</body>

</html>

Is it possible to create a marker in SVG using Javascript and then apply it to a newly created line? If so, please can you tell me what is wrong with the code below. I would expect the red and green lines to both have an arrow head but in both Chrome and Firefox 3.6 only the green line does.

<?php

header('Content-type: application/xhtml+xml');
echo '<?xml version="1.0" encoding="utf-8" ?>';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>SVG test</title>
<script type="text/javascript">
function init()
{
    var div = document.getElementById('mainDiv');

    var svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

    svgNode.style.width = "200px";
    svgNode.style.height = "200px";
    svgNode.style.overflow = 'visible';
    svgNode.style.position = 'absolute';
    svgNode.setAttribute('version', '1.1');
    svgNode.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
    div.appendChild(svgNode);

    var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
    var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
    marker.setAttribute('id', 'Triangle');
    marker.setAttribute('viewBox', '0 0 10 10');
    marker.setAttribute('refX', '0');
    marker.setAttribute('refY', '5');
    marker.setAttribute('markerUnits', 'strokeWidth');
    marker.setAttribute('markerWidth', '4');
    marker.setAttribute('markerHeight', '3');
    marker.setAttribute('orient', 'auto');
    var path = document.createElementNS('http;//www.w3.org/2000/svg', 'path');
    marker.appendChild(path);
    path.setAttribute('d', 'M 0 0 L 10 5 L 0 10 z');

    svgNode.appendChild(defs);
    defs.appendChild(marker);

    var obj = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    obj.setAttribute('x1', 50);
    obj.setAttribute('y1', 50);
    obj.setAttribute('x2', 50);
    obj.setAttribute('y2', 150);
    obj.setAttribute('stroke', '#ff0000');
    obj.setAttribute('stroke-width', 7);
    obj.setAttribute('marker-end', 'url(#Triangle)');

    svgNode.appendChild(obj);


}
</script>
</head>

<body onload="init();">
<div id="mainDiv" style="width: 100%; height: 100%; visibility: visible; overflow: visible; position: absolute; background: white;">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="width: 200px; height: 200px; position: absolute;">
        <defs>
            <marker id="Triangle-static" viewBox="0 0 10 10" refX="0" refY="5" markerUnits="strokeWidth" markerWidth="4" markerHeight="3" orient="auto">
                <path d="M 0 0 L 10 5 L 0 10 z"></path>
            </marker>
        </defs>
        <line x1="150" y1="50" x2="150" y2="150" stroke="#00ff00" stroke-width="7" marker-end="url(#Triangle-static)" />
    </svg>
</div>
</body>

</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-09-17 18:39:29

如果我像这样更改代码的某些部分,它就可以正常工作:

var newmarker = oldmarker.cloneNode(true);
newmarker.setAttribute("id", "Triangle");

...这表明错误在于创建标记元素的代码中。

It worked fine if I changed some parts of your code like this:

var newmarker = oldmarker.cloneNode(true);
newmarker.setAttribute("id", "Triangle");

...which suggests that the error lies in the code that creates the marker element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文