(function(){
function _each(obj,func){
for(var i=0;i<obj.length;i++){
func.call(this,obj[i]);
}
}
function _create(name,attributes){
var element = document.createElementNS('http://www.w3.org/2000/svg',name);
var attr = attributes||{};
for(p in attr){
element.setAttribute(p,attr[p]);
}
return element;
}
function $(name){
return document.getElementById(name)||document.getElementsByTagName(name);
}
var Rect= function(text,attributes){
this.text=text;
if(attributes){
this.target=_create('rect');
for(name in attributes){
this[name]=attributes[name];
this.target.setAttribute(name,attributes[name]);
}
}
}
Rect.prototype.points=function(width){
var w=width||4;
var data=new Array();
data.push({x:this.x-2,y:this.y-2},{x:this.x-2,y:this.y-2+this.height/2},{x:this.x-2,y:this.y-2+this.height});
data.push({x:this.x-2+this.width/2,y:this.y-2},{x:this.x-2+this.width/2,y:this.y-2+this.height});
data.push({x:this.x-2+this.width,y:this.y-2},{x:this.x-2+this.width,y:this.y-2+this.height/2},{x:this.x-2+this.width,y:this.y-2+this.height});
var group=_create('g');
_each(data, function(d){
d['width']=w;
d['height']=w;
console.log(d);
var point = new Rect('',d);
group.appendChild(point.draw());
});
return group;
}
Rect.prototype.draw=function(style){
if(!this.target){
this.target=_create('rect',{x:10,y:10,width:100,height:30});
}
if(style){
var t=new Array();
for(name in style){
t.push(name+':'+style[name]);
}
this.target.setAttribute('style',t.join(';'));
}
return this.target;
}
var rect = new Rect('this is a test',{x:10,y:10,width:100,height:50});
var obj = rect.draw({fill:'#efefef','stroke-width':1,'stroke':'gray'});
$("canvos").appendChild(obj);
$("btn").onclick=function(){
$("canvos").appendChild(rect.points());
}
})();
参考url
http://blog.csdn.net/happyduoduo1/article/details/51789552
http://www.zhangxinxu.com/wordpress/2015/10/understand-svg-transform/
<defs>
<marker id="arraw" markerWidth="10" markerHeight="10" refx="9" refy="5" orient="auto">
<path d="M0,0 V10 L10,5 z" style="fill:grey;" />
</marker>
</defs>
<path d="M80 80 V100 H200 V81" fill="none" stroke="gray" marker-end="url(#arraw)"></path>