/
canvas.js
134 lines (115 loc) · 3.34 KB
/
canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function canvas_render_frame(frame, context){
var shapes = framestore[frame]
//context.clearRect(0,0,stage.width, stage.height)
context.fillStyle = "rgb(255,255,255)";
context.fillRect(0,0,stage.width, stage.height) //GIF can't do transparent so do white
for(var i = 0; i < shapes.length; i++){
CanvasDrawFigure(shapes[i], context)
}
}
function CanvasDrawFigure(src, context){
var root = new CanvasRoot(context, src.angle, src.pos);
(function(children, anchor){
for(var i = 0; i < children.length; i++){
var s = children[i]
if(s.type == "line"){
var n = new CanvasLine(anchor, s.angle, s.length, s.width, s.color)
}else if(s.type == "circle"){
var n = new CanvasCircle(anchor, s.angle, s.length, s.width, s.color)
}
arguments.callee(s.children, n)
}
})(src.children, root)
}
function CanvasRoot(ctx, angle, pos){
//this is a root point.
var point = this;
this.pos = pos||[500,500];
this._angle = angle||0;
this.children = [];
this.ctx = ctx;
}
CanvasRoot.prototype.getPos = function(){
return this.pos;
}
CanvasRoot.prototype.angle = function(){
return this._angle;
}
CanvasRoot.prototype.renderAll = function(){
for(var i = 0; i < this.children.length; i++){
this.children[i].renderAll()
}
}
function CanvasShape(){} //empty object which is extended upon
CanvasShape.prototype.angle = function(){
return this.anchor.angle() + this._angle
}
CanvasShape.prototype.getPos = function(){
//time for some trigonometry!
var anchor = this.anchor.getPos()
var dy = Math.sin(this.angle()) * this.length;
var dx = Math.cos(this.angle()) * this.length;
return [anchor[0] + dx, anchor[1] + dy];
}
CanvasShape.prototype.renderAll = function(){
this.render();
for(var i = 0; i < this.children.length; i++){
this.children[i].renderAll()
}
}
function CanvasLine(anchor, angle, length, width, color, noend){
//a shape is a rendering of 2 arbitrary points
var line = this;
this.type = "line"
this.anchor = anchor;
this.children = [];
this.length = length||50;
this._angle = angle/(180/Math.PI);
this.width = width||6;
this.color = color||"#000";
this.anchor.children.push(this);
this.ctx = this.anchor.ctx;
this.render();
}
CanvasLine.prototype = new CanvasShape();
CanvasLine.prototype.render = function(){
var anchor = this.anchor.getPos()
var end = this.getPos();
var ctx = this.ctx;
ctx.strokeStyle = this.color;
ctx.lineWidth = this.width;
ctx.lineCap = "round"
ctx.beginPath();
ctx.moveTo(anchor[0], anchor[1]);
ctx.lineTo(end[0], end[1]);
ctx.stroke();
}
function CanvasCircle(anchor, angle, length, width, color, noend){
//a shape is a rendering of 2 arbitrary points
var circle = this;
this.anchor = anchor; //type = shape.
this.children = [];
this.type = "circle"
this.length = length||50;
var draw = this.draw = anchor.draw;
this._angle = angle/(180/Math.PI);
this.width = width||6;
this.color = color||"#000";
this.anchor.children.push(this);
this.ctx = this.anchor.ctx;
this.render();
}
CanvasCircle.prototype = new CanvasShape();
CanvasCircle.prototype.render = function(){
var anchor = this.anchor.getPos()
var end = this.getPos();
var ctx = this.ctx;
ctx.beginPath();
ctx.strokeStyle = this.color;
ctx.lineWidth = this.width;
ctx.arc((anchor[0]+end[0])/2, (anchor[1]+end[1])/2,
this.length/2,
0,
2 *Math.PI)
ctx.stroke();
}