JAVASCRIPT SVG 画图

(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>

转载于:https://my.oschina.net/haison/blog/842908

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值