-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton5.html
221 lines (190 loc) · 6.27 KB
/
skeleton5.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>skeleton 5</title>
</head>
<body>
</body>
<script src="iio-sdk/iioEngine-1.2.1.js"></script>
<script >
var dot = function(x,y,ctx){
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(x, y, 2, 0, Math.PI*2, true);
ctx.stroke();
ctx.closePath();
}
var skeletonApp1 = function(io){
/**
* Bone(width,height) 骨骼位置大部分是依附关节,所以提供长宽就可以
* Bone(x,y,width,height)
*/
var Bone = iio.inherit(function(x,y,width,height){
this.joints = {};
this.rotation = 0; //中心旋转角度,为iio处理
this.angle = 0; //按fixed关节旋转角度
if(arguments.length == 2){
iio.ioRect.call(this,0,0,x,y);
}else{
iio.ioRect.call(this,x,y,width,height);
}
//iio.ioRect.apply(this, arguments);
},iio.ioRect);
//关节的x,y是相对于骨头的pos(就是骨头的pos)
Bone.prototype.setJoint = function(name,x,y){
if(!this.joints) this.joints = {};
//pos为关节的绝对位置(因运动而不断重设)
this.joints[name] = {x:x,y:y,pos:{x:x,y:y}};
}
Bone.prototype.getJoint = function(name){
return this.joints[name];
}
Bone.prototype.getJointAbsPos = function(name){
if(this.joints[name]){
var targetJoint = this.joints[name],
fixedJoint = this.joints['fixed'];
var disX = targetJoint.x - fixedJoint.x;
var disY = targetJoint.y - fixedJoint.y;
var xPos = this.pos.x+ fixedJoint.x + Math.cos(this.angle)*disX - Math.sin(this.angle)*disY;
var yPos = this.pos.y+ fixedJoint.y + Math.sin(this.angle)*disX + Math.cos(this.angle)*disY;
//同时更新关节的实际位置
targetJoint.pos.x = xPos;
targetJoint.pos.y = yPos;
//dot(xPos,yPos,io.context);
return {x: xPos, y: yPos}
}
}
Bone.prototype.setPosByJoint = function(bone,name){
if(bone instanceof Bone && bone.joints[name]){
var jointAbsPos = bone.getJointAbsPos(name);
var thisFixed = this.joints['fixed'];
var boneFixed = bone.joints['fixed'];
//把自身的固定关节依附到设定关节中
thisFixed.pos.x = jointAbsPos.x;
thisFixed.pos.y = jointAbsPos.y;
//同时更新自身的位置
this.pos.x = jointAbsPos.x - thisFixed.x;
this.pos.y = jointAbsPos.y - thisFixed.y;
}
}
Bone.prototype.drawJoints = function(ctx){
//重新调整自身关节绘画的位置(不影响实际位置)
for(var i in this.joints){
var joint = this.joints[i];
//关节是依附在骨骼上,那么他的位置是相对于骨骼
//它的位置只要计算在骨骼正常位置情况下所在就行了,骨骼旋转或平移会自动相对的了
var posX = this.pos.x + joint.x;
var posY = this.pos.y + joint.y;
this.drawJoint(ctx,posX,posY);
}
}
Bone.prototype.drawJoint = function(ctx,x,y,radius,lineWidth,color){
ctx.lineWidth = lineWidth || 2;
ctx.strokeStyle = color || 'green';
radius = radius || 4;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI*2, true);
ctx.stroke();
ctx.closePath();
}
Bone.prototype.draw = function(ctx,pos,r){
ctx.save();
var jointPos = this.joints['fixed'].pos; //取出固定关节的实际位置(绝对位置)
ctx.translate(jointPos.x, jointPos.y);
ctx.rotate(this.angle);
ctx.translate(-jointPos.x, -jointPos.y);
//如果带有自己的绘制方法,就不使用默认的矩形
if(typeof this.redraw == 'function'){
this.redraw(ctx);
}else{
this.setStrokeStyle('#000').setFillStyle('#fff'); //默认样式,iio没有设置默认样式,不填是不能显示
Bone.parent.prototype.draw.apply(this,arguments);
}
//画出关节
this.drawJoints(ctx);
ctx.restore();
}
var bone0 = new Bone(400,300,120,30);
bone0.setJoint('fixed',-50,0);
bone0.setJoint('leg',50,0);
io.addObj(bone0,-1);
var bone1 = new Bone(120,20);
bone1.setJoint('fixed',-50,0);
bone1.setPosByJoint(bone0,'leg');
io.addObj(bone1,-1);
var bone2 = new Bone(400,300,120,30);
bone2.setJoint('fixed',-50,0);
bone2.setJoint('leg',50,0);
bone2.setPosByJoint(bone0,'fixed');
io.addObj(bone2,1);
var bone3 = new Bone(120,20);
bone3.setJoint('fixed',-50,0);
bone3.setPosByJoint(bone2,'leg');
io.addObj(bone3,1);
//body
var body = new Bone(60,150);
body.angle = Math.PI/18;
body.setJoint('fixed',0,65);
body.setJoint('head',0,-65);
body.setPosByJoint(bone0,'fixed');
io.addObj(body);
//arm
var arm0 = new Bone(110,26);
arm0.setJoint('fixed',-45,0);
arm0.setJoint('arm',45,0);
arm0.setPosByJoint(body,'head');
io.addObj(arm0,1);
var arm1 = new Bone(110,20);
arm1.setJoint('fixed',-45,0);
arm1.setPosByJoint(arm0,'arm');
io.addObj(arm1,1);
var arm2 = new Bone(110,26);
arm2.setJoint('fixed',-45,0);
arm2.setJoint('arm',45,0);
arm2.setPosByJoint(body,'head');
io.addObj(arm2,-2);
var arm3 = new Bone(110,20);
arm3.setJoint('fixed',-45,0);
arm3.setPosByJoint(arm2,'arm');
io.addObj(arm3,-2);
//head
var head = new Bone(50,70);
head.setJoint('fixed',0,0);
head.setPosByJoint(body,'head');
head.redraw = function(ctx){
this.drawJoint(ctx,this.pos.x,this.pos.y-50,30,2,'#555');
};
io.addObj(head);
console.log(io);
var walkLeg = function(boneA,boneB,c){
var objRotation = Math.PI/2; //骨碌组合选择角度。如果为0则水平
var angleA = Math.sin(c)*Math.PI/4 + objRotation;
var angleB = Math.sin(c-Math.PI/2)*Math.PI/4+Math.PI/4; // "-"
boneA.angle = angleA;
boneB.angle = boneA.angle+angleB;
boneB.setPosByJoint(boneA,'leg');
}
var walkArm = function(boneA,boneB,c){
var objRotation = Math.PI/2; //骨碌组合选择角度。如果为0则水平
var angleA = Math.sin(c)*Math.PI/4 + objRotation;
var angleB = Math.sin(c+Math.PI/2)*Math.PI/4+Math.PI/4; // "+"
boneA.angle = angleA;
boneB.angle = boneA.angle-angleB;
boneB.setPosByJoint(boneA,'arm');
}
var cycle = 0,ctx;
io.setFramerate(60,function(){
cycle+=0.05;
walkLeg(bone0, bone1, cycle);
walkLeg(bone2, bone3, cycle + Math.PI);
walkArm(arm2, arm3, cycle + Math.PI);
walkArm(arm0, arm1,cycle);
//现在我们把我们的骨骼对象都交给ioAppManager,但是我们的Bone对象都没有设定enableKinematics方法
//注意即使设定了enableKinematics方法而没有setVel,ioAppManager都不会把对象不断重绘的。
//所以我们在setFramerrate里面强制告诉ioAppManager,我们需要重绘
io.draw();
});
}; iio.start(skeletonApp1);
</script>
</html>