ajax load svg,jquery操作svg

$(function () {

var svg = window.external.GetOne();

console.log(svg);

var paper = Raphael(document.getElementById('container'), 800, 600);

paper.importSVG(svg);

//bindDefaultEvent(paper);

});

这样两次输出的svg不一样

下面贴出importSVG函数

Raphael.fn.importSVG = function (rawSVG, set) {

try {

if (typeof rawSVG === 'undefined')

throw 'No data was provided.';

rawSVG = rawSVG.replace(/\n|\r|\t/gi, '');

if (!rawSVG.match(/(.*)/i))

throw "The data you entered doesn't contain valid SVG.";

var findAttr  = new RegExp('([a-z\-]+)="(.*?)"','gi'),

findStyle = new RegExp('([a-z\-]+) ?: ?([^ ;]+)[ ;]?','gi'),

findNodes = new RegExp('','gi');

while(match = findNodes.exec(rawSVG)){

var shape, style,

attr = { 'fill':'#000' },

node = RegExp.$1;

while(findAttr.exec(match)){

switch(RegExp.$1) {

case 'stroke-dasharray':

attr[RegExp.$1] = '- ';

break;

case 'style':

style = RegExp.$2;

break;

default:

attr[RegExp.$1] = RegExp.$2;

break;

}

};

if (typeof attr['stroke-width'] === 'undefined')

attr['stroke-width'] = (typeof attr['stroke'] === 'undefined' ? 0 : 1);

if (style)

while(findStyle.exec(style))

attr[RegExp.$1] = RegExp.$2;

switch(node) {

case 'rect':

shape = this.rect();

break;

case 'circle':

shape = this.circle();

break;

case 'ellipse':

shape = this.ellipse();

break;

case 'path':

shape = this.path(attr['d']);

break;

case 'polygon':

shape = this.polygon(attr['points']);

break;

case 'image':

shape = this.image();

break;

case 'text':

shape = this.text();

break;

}

shape.attr(attr);

if((typeof attr['id']) !== 'undefined'){

shape.id = attr['id'];

}

if (typeof set !== 'undefined')

set.push(shape);

};

} catch (error) {

alert('The SVG data you entered was invalid! (' + error + ')');

}

};

// extending raphael with a polygon function

Raphael.fn.polygon = function(pointString) {

var poly  = ['M'],

point = pointString.split(' ');

for(var i=0; i < point.length; i++) {

var c = point[i].split(',');

for(var j=0; j < c.length; j++) {

var d = parseFloat(c[j]);

if (d)

poly.push(d);

};

if (i == 0)

poly.push('L');

}

poly.push('Z');

return this.path(poly);

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值