svg个人小结

1path画格子,不要动态创建
2解决ie浏览器中文本对齐方式不一样问题,用dy控制文本垂直位置
3动态生成文本写法
var text= document.createElementNS(svgNS, 'text');
    text.appendChild(document.createTextNode(textContent));
    text.setAttribute('font-size',text_height);
    text.setAttribute('dy',text_height);
    text.setAttribute('fill','#fff');
    text.setAttribute('x',x0);
    text.setAttribute('y',y0);
    text.setAttribute('id','txt_'+i);
    parent.appendChild(text);
4动态生成图片,指定图片路径的写法
var image=document.createElementNS(svgNS,'image');
    image.href.baseVal = "../custom/bigscreen/img/close.png";
    image.setAttribute('width',text_height);
    image.setAttribute('height',text_height);
    image.setAttribute('x',xi);
    image.setAttribute('y',yi);
    image.setAttribute('id','img_'+i);
    parent.appendChild(image);
5动态生成形状的写法
var rect = document.createElementNS(svgNS, 'rect');
    rect.setAttribute('width',need_width);
    rect.setAttribute('height',title_height);
    rect.setAttribute('x', x0);
    rect.setAttribute('y', y0);
    rect.setAttribute('fill', '#237AE5');
    rect.setAttribute('fill-opacity', '.8');
    rect.setAttribute('id','bg_'+i);
    parent.appendChild(rect);
SVG绘图的特点——着重注意!
(1)所有的图形默认只有填充色(黑色),没有描边色。
(2)SVG图形的样式可以用元素属性声明,也可以用CSS形式来声明。但用CSS声明时,只能使用SVG专用的样式,不能使用CSS样式,如边框设置只能用stroke,而不用border!
(3)图形可以使用JS来对属性赋值;但不能使用HTML DOM形式,只能用核心DOM操作,如:
  r.x = 10;   r.width = 100;    //无效
  r.setAttribute('x', 10); r.setAttribute('width', 100)//有效
(4)动态添加SVG图形可以使用两种方式:
   1)HTML字符串拼接
       var html = `<rect></rect>`;
       svg.innerHTML = html;
   2)使用document.createElementNS('http://www.w3.org/2000/svg','标签名')创建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值