国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 擴展 > SVG > 正文

svg DOM的一些js操作

2024-09-06 19:57:08
字體:
來源:轉載
供稿:網友
這是第一個實例,其中講了如何新建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);}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桦甸市| 锡林浩特市| 喀喇沁旗| 文昌市| 绩溪县| 德州市| 夏邑县| 宁德市| 田林县| 武义县| 寻乌县| 霍林郭勒市| 图们市| 宜兴市| 荆州市| 府谷县| 墨江| 清苑县| 池州市| 花垣县| 彰化县| 伊金霍洛旗| 同江市| 通化县| 修文县| 申扎县| 威海市| 沧州市| 民县| 财经| 从化市| 洛隆县| 玛纳斯县| 咸宁市| 江西省| 黄冈市| 建昌县| 双辽市| 乐山市| 华安县| 酉阳|