-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton6.html
331 lines (283 loc) · 9.22 KB
/
skeleton6.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>skeleton 6</title>
<style>
.range{
display: inline-block;
width: 140px;
}
.range p{
display: inline-block;
width: 100%;
padding: 0;
margin: 0;
text-align: left;
font-family: '微软雅黑';
color: #bbb;
position: relative;
top: -15px;
z-index: -1;
}
</style>
</head>
<body>
<div id="controller">
<div class="range">
<input type="range" id="speedRange" max="0.1" min="0.01" step="0.01" value="0.05">
<p>Speed</p>
</div>
<div class="range">
<input type="range" id="thighRange" max="90" min="0" step="1" value="45">
<p>Thigh</p>
</div>
<div class="range">
<input type="range" id="calfRange" max="90" min="0" step="1" value="45">
<p>Calf</p>
</div>
<div class="range">
<input type="range" id="thighBase" max="180" min="0" step="1" value="90">
<p>ThighBase</p>
</div>
<div class="range">
<input type="range" id="calfOffset" max="180" min="-180" step="1" value="-90">
<p>CalfOffset</p>
</div>
</div>
<canvas id="skeletonCanvas" width="1024" height="600"></canvas>
</body>
<script src="iio-sdk/iioEngine-1.2.2.js"></script>
<script >
/**
* Bone(width,height) 骨骼位置大部分是依附关节,所以提供长宽就可以
* Bone(x,y,width,height)
*/
var Bone = iio.inherit(function(x,y,width,height){
//关节集合
this.joints = {};
//中心旋转角度,为iio处理
this.rotation = 0;
//按fixed关节旋转角度
this.angle = 0;
//注意这不能使用this._super.call(this);因为这时继承关系还没有继承
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没有设置默认样式,不填是不能显示
this._super.draw.apply(this,arguments);
}
//画出关节
this.drawJoints(ctx);
ctx.restore();
}
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 bone0;
var skeletonApp = function(io){
/**
* iio引擎1.2.2和1.2.1有个bug
* 1.2.1版的同个zIndex情况下,先addObj的比后addObj的层次低(后add的高)
* 1.2.2版的同个zIndex情况下,先addObj的比后addObj的层次高(后add的低)
* 并且1.2.2有个bug,默认情况(不填zIndex)会有bug,建议只能填-1或1
* 目前我是直接用1.2.1的源码覆盖1.2.2的部分源码,使行为与1.2.1一致但也有可能还有问题
*/
bone0 = new Bone(400,300,120,30);
bone0.setJoint('fixed',-50,0);
bone0.setJoint('leg',50,0);
//io.addToGroup('bodyBack',bone0,-1);
io.addObj(bone0,-1);
var bone1 = new Bone(120,20);
bone1.setJoint('fixed',-50,0);
bone1.setPosByJoint(bone0,'leg');
//io.addToGroup('bodyBack',bone1,-1);
io.addObj(bone1,-2);
var bone2 = new Bone(400,300,120,30);
bone2.setJoint('fixed',-50,0);
bone2.setJoint('leg',50,0);
bone2.setPosByJoint(bone0,'fixed');
//io.addToGroup('bodyFront',bone2,1);
io.addObj(bone2,1);
var bone3 = new Bone(120,20);
bone3.setJoint('fixed',-50,0);
bone3.setPosByJoint(bone2,'leg');
//io.addToGroup('bodyFront',bone3,1);
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.addToGroup('body',body);
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.addToGroup('bodyBack',arm0,-2);
io.addObj(arm0,2);
var arm1 = new Bone(110,20);
arm1.setJoint('fixed',-45,0);
arm1.setPosByJoint(arm0,'arm');
//io.addToGroup('bodyBack',arm1,-2);
io.addObj(arm1,2);
var arm2 = new Bone(110,26);
arm2.setJoint('fixed',-45,0);
arm2.setJoint('arm',45,0);
arm2.setPosByJoint(body,'head');
//io.addToGroup('bodyFront',arm2,1);
io.addObj(arm2,-1);
var arm3 = new Bone(110,20);
arm3.setJoint('fixed',-45,0);
arm3.setPosByJoint(arm2,'arm');
//io.addToGroup('bodyFront',arm3,1);
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.addToGroup('body',head);
io.addObj(head);
console.log(io);
var els = {};
els.speed = document.getElementById('speedRange');
els.thigh = document.getElementById('thighRange');
els.thighB = document.getElementById('thighBase');
els.calf = document.getElementById('calfRange');
els.calfO = document.getElementById('calfOffset');
var walkLeg = function(boneA,boneB,c){
var deg = Math.PI/180,
thigh = parseFloat(els.thigh.value)*deg,
thighB = parseFloat(els.thighB.value)*deg,
calf = parseFloat(els.calf.value)*deg,
calfO = parseFloat(els.calfO.value)*deg;
var angleA = Math.sin(c)* thigh + thighB;
var angleB = Math.sin(c + calfO) * calf + calf;
boneA.angle = angleA;
boneB.angle = boneA.angle+angleB;
boneB.setPosByJoint(boneA,'leg');
}
var walkArm = function(boneA,boneB,c){
var deg = Math.PI/180,
thigh = parseFloat(els.thigh.value)*deg,
thighB = parseFloat(els.thighB.value)*deg,
calf = parseFloat(els.calf.value)*deg,
calfO = parseFloat(els.calfO.value)*deg;
var angleA = Math.sin(c) * thigh + thighB;
angleB = Math.sin(c - calfO) * calf + calf;
boneA.angle = angleA;
boneB.angle = boneA.angle-angleB;
boneB.setPosByJoint(boneA,'arm');
}
var cycle = 0,ctx;
io.setFramerate(60,function(){
cycle += parseFloat(els.speed.value);
var deg = Math.PI/180;
body.angle = Math.sin(cycle) * 5*deg;
head.angle = Math.sin(cycle) * 10*deg;
/* 下面walk函数里面也有setPosByJoint 要想全身动作整体连贯,最好报所有关键的setPostByJoint都更新一次 */
//bone1.setPosByJoint(bone0,'leg');
bone2.setPosByJoint(bone0,'fixed');
//bone3.setPosByJoint(bone2,'leg');
body.setPosByJoint(bone0,'fixed');
arm0.setPosByJoint(body,'head');
//arm1.setPosByJoint(arm0,'arm');
arm2.setPosByJoint(body,'head');
//arm3.setPosByJoint(arm2,'arm');
head.setPosByJoint(body,'head');
walkLeg(bone0, bone1, cycle);
walkLeg(bone2, bone3, cycle + Math.PI);
walkArm(arm0, arm1,cycle);
walkArm(arm2, arm3, cycle + Math.PI);
//现在我们把我们的骨骼对象都交给ioAppManager,但是我们的Bone对象都没有设定enableKinematics方法
//注意即使设定了enableKinematics方法而没有setVel,ioAppManager都不会把对象不断重绘的。
//所以我们在setFramerrate里面强制告诉ioAppManager,我们需要重绘
io.draw();
});
}
;(function(){
iio.start(skeletonApp,"skeletonCanvas");
})();
</script>
</html>