svg DOM的一些js操作

本文介绍了如何使用JavaScript创建SVG文档并添加基本图形元素,包括圆形、椭圆、直线、路径、多边形、折线、矩形和圆角矩形,并展示了SVG使用方法和属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是第一个实例,其中讲了如何新建svg,添加元素,保存svg document,查看svg.

下面将附上常用一些元素的添加方法:(为js的,但基本上跟java中操作一样,就是类名有点细微差别)

Circle

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "circle");

    shape.setAttributeNS(null, "cx", 25);

    shape.setAttributeNS(null, "cy", 25);

    shape.setAttributeNS(null, "r",  20);

    shape.setAttributeNS(null, "fill", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Ellipse

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "ellipse");

    shape.setAttributeNS(null, "cx", 25);

    shape.setAttributeNS(null, "cy", 25);

    shape.setAttributeNS(null, "rx", 20);

    shape.setAttributeNS(null, "ry", 10);

    shape.setAttributeNS(null, "fill", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Line

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "line");

    shape.setAttributeNS(null, "x1", 5);

    shape.setAttributeNS(null, "y1", 5);

    shape.setAttributeNS(null, "x2", 45);

    shape.setAttributeNS(null, "y2", 45);

    shape.setAttributeNS(null, "stroke", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Path

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "path");

    shape.setAttributeNS(null, "d", "M5,5 C5,45 45,45 45,5");

    shape.setAttributeNS(null, "fill", "none");

    shape.setAttributeNS(null, "stroke", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Polygon

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    shape = svgDocument.createElementNS(svgns, "polygon");

    shape.setAttributeNS(null, "points", "5,5 45,45 5,45 45,5");

    shape.setAttributeNS(null, "fill", "none");

    shape.setAttributeNS(null, "stroke", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Polyline

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    shape = svgDocument.createElementNS(svgns, "polyline");

    shape.setAttributeNS(null, "points", "5,5 45,45 5,45 45,5");

    shape.setAttributeNS(null, "fill", "none");

    shape.setAttributeNS(null, "stroke", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Rectangle

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "rect");

    shape.setAttributeNS(null, "x", 5);

    shape.setAttributeNS(null, "y", 5);

    shape.setAttributeNS(null, "width",  40);

    shape.setAttributeNS(null, "height", 40);

    shape.setAttributeNS(null, "fill", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Rounded Rectangle

var svgns = "http://www.w3.org/2000/svg";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var shape = svgDocument.createElementNS(svgns, "rect");

    shape.setAttributeNS(null, "x", 5);

    shape.setAttributeNS(null, "y", 5);

    shape.setAttributeNS(null, "rx", 5);

    shape.setAttributeNS(null, "ry", 5);

    shape.setAttributeNS(null, "width",  40);

    shape.setAttributeNS(null, "height", 40);

    shape.setAttributeNS(null, "fill", "green");

 

    svgDocument.documentElement.appendChild(shape);

}

Use

var svgns   = "http://www.w3.org/2000/svg";

var xlinkns = "http://www.w3.org/1999/xlink";

 

function makeShape(evt) {

    if ( window.svgDocument == null )

        svgDocument = evt.target.ownerDocument;

 

    var svgRoot = svgDocument.documentElement;

 

    var defs = svgDocument.createElementNS(svgns, "defs");

 

    var rect = svgDocument.createElementNS(svgns, "rect");

    rect.setAttributeNS(null, "id", "rect");

    rect.setAttributeNS(null, "width", 15);

    rect.setAttributeNS(null, "height", 15);

    rect.setAttributeNS(null, "style", "fill: green");

 

    defs.appendChild(rect);

 

    var use1 = svgDocument.createElementNS(svgns, "use");

    use1.setAttributeNS(null, "x", 5);

    use1.setAttributeNS(null, "y", 5);

    use1.setAttributeNS(xlinkns, "xlink:href", "#rect");

 

    use2 = svgDocument.createElementNS(svgns, "use");

    use2.setAttributeNS(null, "x", 30);

    use2.setAttributeNS(null, "y", 30);

    use2.setAttributeNS(xlinkns, "xlink:href", "#rect");

 

    svgRoot.appendChild(defs);

    svgRoot.appendChild(use1);

    svgRoot.appendChild(use2);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值