国产探花免费观看_亚洲丰满少妇自慰呻吟_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);}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤冈县| 商河县| 莲花县| 伊宁市| 永定县| 耿马| 石阡县| 岳普湖县| 改则县| 施甸县| 肥乡县| 玉树县| 灵丘县| 平陆县| 芦溪县| 蛟河市| 营口市| 罗城| 泾川县| 东丽区| 丹江口市| 桑日县| 安庆市| 湟中县| 叙永县| 新沂市| 黔东| 门头沟区| 钦州市| 南木林县| 桃园市| 册亨县| 台山市| 镇原县| 邻水| 靖安县| 石景山区| 红原县| 集安市| 青浦区| 溧阳市|