-
Notifications
You must be signed in to change notification settings - Fork 18
/
pen_min.js
10 lines (9 loc) · 2.38 KB
/
pen_min.js
1
2
3
4
5
6
7
8
9
10
Pen=function(tag){this.dir=-90;this.x=0;this.y=0;this.tag=document.getElementById(tag)||tag;this.canvas=this.tag.getContext("2d");this.strokeStyle=this.canvas.strokeStyle="#000";this.lineWidth=this.canvas.lineWidth=1;this.fillStyle=this.canvas.fillStyle="";this.ox=0;this.oy=0;this.pen=true;this.canvas.beginPath();};Pen.prototype={turn:function(deg){this.dir+=deg;this.dir=this.dir%360;return this;},fillstyle:function(style){this.fillStyle=this.canvas.fillStyle=style;return this;},set:function(){this.homes.push({x:this.x,y:this.y,angle:this.dir});return this;},angle:function(a){this.dir=a-90;return this;},home:function(){var last=this.homes.pop();this.dir=last.angle;return this.goto(this.x,this.y);},go:function(r){var a=this.toRad(this.dir);this.x+=r*Math.cos(a);this.y+=r*Math.sin(a);if(this.pen)
this.canvas.lineTo(this.x,this.y);else
this.canvas.moveTo(this.x,this.y);return this;},back:function(r){this.turn(-180);this.go(r);this.turn(180);return this;},stroke:function(){this.canvas.stroke();return this;},fill:function(){this.canvas.fill();return this;},begin:function(){this.canvas.beginPath();return this;},close:function(){this.canvas.closePath();return this;},draw:function(){if(this.fillStyle)
this.fill();if(this.lineWidth)
this.stroke();return this.begin();},penup:function(){this.pen=false;return this;},pendown:function(){this.pen=true;return this;},goto:function(x,y){this.x=x;this.y=y;if(!this.pen)
this.canvas.moveTo(x,y);else
this.canvas.lineTo(x,y);return this;},jump:function(x,y){this.canvas.beginPath();var p=this.pen;this.pen=true;this.goto(x,y);this.pen=p;return this;},up:function(r){return this.goto(this.x,this.y-r);},down:function(r){return this.goto(this.x,this.y+r);},left:function(r){return this.goto(this.x-r,this.y);},right:function(r){return this.goto(this.x+r,this.y);},polar:function(r,angle){var a=this.toRad(angle+this.dir);this.x=this.ox+r*Math.cos(a);this.y=this.oy+r*Math.sin(a);if(this.pen)
this.canvas.lineTo(this.x,this.y);else
this.canvas.moveTo(this.x,this.y);return this;},origin:function(){this.ox=this.x;this.oy=this.y;return this;},rad:Math.PI/180.0,toRad:function(d){return d*this.rad;},pensize:function(size){this.lineWidth=this.canvas.lineWidth=size;return this;},penstyle:function(str){this.canvas.strokeStyle=this.strokeStyle=str;return this;},text:function(str){this.canvas.fillText(str,this.x,this.y);return this;},font:function(str){this.canvas.font=str;return this;}};