-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcorners.js
328 lines (279 loc) · 8.96 KB
/
corners.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
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
/**
* @classDescription A shape made out of bezier curves. Hopefully connected
* @param {Array} sections
*/
function BezierShape(sections) {
this.sections = sections;
this.name = ""; //optional
this.skeleton = [];
}
BezierShape.prototype.copy = function() {
var newSections = [],
newSkeleton = [];
for(var i in this.sections) {
newSections[i] = [];
for(var j = 0; j<4; j++) {
newSections[i][j] = this.sections[i][j].slice(0);
}
}
for(var i in this.skeleton)
newSkeleton[i] = this.skeleton[i].copy();
var copy = new BezierShape(newSections);
copy.name = this.name;
copy.skeleton = newSkeleton;
return copy;
};
/**
* Draws the BezierShape NO SCALING OR NUFFIN. Probably only used internally
* @param {Object} ctx
*/
BezierShape.prototype.draw = function(ctx) {
var x = this.sections[0][0][0], //ew
y = this.sections[0][0][1];
ctx.beginPath();
ctx.moveTo(x,y);
for(var i = 0; i < this.sections.length; i++) {
var b = this.sections[i];
ctx.bezierCurveTo(b[1][0],b[1][1],b[2][0],b[2][1],b[3][0],b[3][1]);
}
ctx.closePath();
ctx.fill();
if(DEBUG.CORNER_OUTLINES) {
ctx.globalCompositeOperation = "xor";
ctx.stroke();
ctx.globalCompositeOperation = "source-over";
}
};
function Bone(points,offset) {
this.points = points;
this.offset = offset;
}
Bone.prototype.copy = function() {
var nP = [];
for(var i in this.points)
nP[i] = this.points[i].slice(0);
return new Bone(nP,this.offset);
};
function drawCornerScaled(corner,pos,dir,width,height,ctx) { //FIXME degree, radian inconsistency
ctx.save();
ctx.translate(pos[0],pos[1]);
ctx.rotate(dir);
ctx.scale(height,width);
corner.draw(ctx);
ctx.restore();
}
function drawCorner(corner,pos,dir,width,ctx) {
drawCornerScaled(corner,pos,dir,width,width,ctx);
}
function drawDICorner(corner,attrs,width,ctx) {
if(corner == null)
return;
// corner rotation
var pos = attrs.point,
inAngle = attrs.inAngle-corner.skeleton["armA"].offset, //This is so the whole corner is rotated //FIXME a little gross
outAngle = attrs.outAngle,
c = setBoneAngles(corner,[["armB",(outAngle-inAngle)/180*Math.PI]]);
drawCorner(c,pos,inAngle/180*Math.PI,width,ctx);
}
// HERE ARE SOME CORNERS // some may need to be rotated
kappa = 0.5522847498;
// Circle-ish thing. Not a corner.
CIRCLE = new BezierShape([
[[-5,0],[-5,-5*kappa],[-5*kappa,-5],[0,-5]],
[[0,-5],[5*kappa,-5],[5,-5*kappa],[5,0]],
[[5,0],[5,5*kappa],[5*kappa,5],[0,5]],
[[0,5],[-5*kappa,5],[-5,5*kappa],[-5,0]]
]);
C1 = new BezierShape([
[[15,6], [-3,4], [-11,5], [-20,0]]
,[[-20,0], [-15,-5], [4,-9], [13,-5]]
,[[13,-5], [20,0], [21,8], [15,6]]
]);
C1.name = "C1";
C2 = new BezierShape([
[[2,5], [-2,5], [-12,2], [-13,-2]]
,[[-13,2], [-7,-5], [0,-5], [2,-5]]
,[[2,-5], [3,-5], [3,5], [2,5]]
]);
C2.name = "C2";
C3 = new BezierShape([
[[-8,5], [-10,5], [-10,-5], [-8,-5]]
,[[-8,-5], [3,-5], [15,0], [15,5]]
,[[15,5], [10,7], [2,5], [-8,5]]
]);
C3.name = "C3";
C4 = new BezierShape([
[[0,5], [-2,5], [-4,7], [-5,8]]
,[[-5,8], [-7,10], [-9,12], [-8,5]]
,[[-8,5], [-7,3], [-5,-5], [0,-5]]
,[[0,-5], [3,-5], [3,5], [0,5]]
]);
C4.name = "C4";
C5 = new BezierShape([
[[0,-5], [-3,-5], [-3,5], [0,5]]
,[[0,5], [8,5], [10,5], [15,2]]
,[[15,2], [12,-2], [-2,-5], [0,-5]]
]);
C5.name = "C5";
C6 = new BezierShape([
[[0,5], [-6,6], [-8,7], [-12,8]]
,[[-12,8], [-13,9], [-13,7], [-12,6]]
,[[-12,6], [-10,3], [-5,-4], [0,-5]]
,[[0,-5], [3,-5], [3,5], [0,5]]
]);
C6.name = "C6";
C7 = new BezierShape([
[[-5,-5],[0,-5],[11,-7],[15,-6]]
,[[15,-6],[17,-5],[2,4],[1,5]]
,[[1,5],[0,5],[0,5],[-5,5]]
,[[-5,5],[-8,5],[-8,-5],[-5,-5]]
]);
C7.name = "C7";
SI_CORNERS = [C1,C2,C3,C4,C5,C6,C7];
C8 = new BezierShape([
[[-13,3], [-20,3], [-20,-3], [-13,-3]],
[[-13,-3], [-5,-5], [-6,-7], [-4,-8]],
[[-4,-8], [0,-8], [12,3], [7,5]],
[[7,5], [5,6], [5,8], [3,13]],
[[3,13], [3,20], [-3,20], [-3,13]],
[[-3,13], [-5,5], [-10,5], [-13,3]]
]);
C8.name = "C8";
C8.skeleton["armA"] = new Bone([[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[5,2],[5,3]],0);
C8.skeleton["armB"] = new Bone([[4,0],[4,1],[4,2],[4,3],[3,2],[3,3],[5,0],[5,1],
[1,2],[1,3],[2,0],[2,1]],90);
C8R = horizFlipCopy(C8);
/*C9 = new BezierShape([ //TODO fix corner so that stem moves depending on angle
[[-3,-10], [-3,-15], [3,-15], [3,-10]],
[[3,-10], [5,-5], [6,6], [0,11]],
[[0,11], [-5,15], [-3,5], [-10,3]],
[[-10,3], [-15,3], [-15,-3], [-10,-3]],
[[-10,-3], [-5,-5], [-5,-7], [-3,-10]]
]);
C9.name = "C9";
C9.skeleton["armA"] = new Bone([[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[4,2],[4,3]],90);
C9.skeleton["armB"] = new Bone([[3,0],[3,1],[3,2],[3,3],[4,0],[4,1],[2,2],[2,3]],180);*/
C9 = new BezierShape([ //note, 90º angles look a little weird
[[-4,-12],[-4,-15],[4,-15],[5,-12]],
[[5,-12],[5,-2],[6,3],[1,8]],
[[-1,8],[-3,11],[-4,2],[-12,-5]],
[[-12,-5],[-15,-7],[-15,-9],[-10,-8]],
[[-10,-8],[-6,-8],[-4,-7],[-4,-12]]
]);
C9.name = "C9";
C9.skeleton["armA"] = new Bone([[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[4,2],[4,3],[1,2]],90); //not that this actually matters
C9.skeleton["armB"] = new Bone([[3,0],[3,1],[3,2],[3,3],[4,0],[4,1],[2,2],[2,3]],210);
C9R = vertFlipCopy(C9);
C10 = new BezierShape([
[[-5,5],[-6,5],[-6,-5],[-5,-5]],
[[-5,-5],[-2,-7],[2,-7],[5,-5]],
[[5,-5],[6,-5],[6,5],[5,5]],
[[5,5],[2,7],[-2,7],[-5,5]]
]);
C10.name = "C10";
C10.skeleton["armA"] = new Bone([[0,0],[0,1],[0,2],[0,3],[1,0],[1,2],[3,2],[3,3]],0);
C10.skeleton["armB"] = new Bone([[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[1,2],[1,3]],0);
function linInterpolate(y0,y1,mu) {
return y0*(1-mu) + y1*mu;
}
function cosInterpolate(y0,y1,mu) {
var mu2 = (1-Math.cos(mu*Math.PI))/2;
return y0*(1-mu2)+y1*mu2;
}
/**
* Returns a function that linearly interpolates between the values given
*/
function linFunction(points) {
return function(t) {
if(t==0)
return points[0][1];
for(var i = 1; i<points.length; i++) {
var p0 = points[i-1],
p1 = points[i];
if(t<=p1[0] && t>p0[0]) {
var mu = (t-p0[0])/(p1[0]-p0[0]);
return linInterpolate(p0[1],p1[1],mu); //cubic might be better
}
}
};
}
/**
* Returns a function that cosine interpolates between the values given
*/
function cosFunction(points) {
return function(t) {
if(t==0)
return points[0][1];
for(var i = 1; i<points.length; i++) {
var p0 = points[i-1],
p1 = points[i];
if(t<=p1[0] && t>p0[0]) {
var mu = (t-p0[0])/(p1[0]-p0[0]);
return cosInterpolate(p0[1],p1[1],mu); //cubic might be better
}
}
};
}
//example thickness functions
function one(t) {
return 1;
}
function test(t) {
return t;
}
//These are ugly
SEGMENT_I = cosFunction([[0,1],[.5,.7],[1,1]]); //FIXME, sometimes extends past corners
SEGMENT_II = linFunction([[0,1],[.5,.8],[1,.2]]); //kinda ugly :||
SEGMENT_III = linFunction([[0,.2],[.5,.8],[1,1]]);
HEN = [C2,SEGMENT_I,C3];
SHU1 = [C4,SEGMENT_I,C5];
SHU2 = [C4,SEGMENT_II];
NA = [C6,SEGMENT_I,C7];
DIAN = [C1];
OTHER = [C4,SEGMENT_II];
function RAND(t) {
return Math.random();
}
function setBoneAngles(c,dirList) {
var c = c.copy();
for(var i in dirList) {
var dir = dirList[i][1],
bone = dirList[i][0];
for(var j in c.skeleton[bone].points) {
var p = c.skeleton[bone].points[j],
offset = c.skeleton[bone].offset/180*Math.PI,
vec = c.sections[p[0]][p[1]];
//console.log(vec);
console.log(dir-offset);
//console.log(rotate(vec,dir-offset));
c.sections[p[0]][p[1]] = rotate(vec,dir-offset);
}
}
return c;
}
function vertFlipCopy(c) {
var c = c.copy();
for(var i in c.sections){
for(var j in c.sections[i]) {
c.sections[i][j][1] = -c.sections[i][j][1];
}
}
for(var i in c.skeleton) {
c.skeleton[i].offset = 360 - c.skeleton[i].offset;
}
return c
}
function horizFlipCopy(c) {
var c = c.copy();
for(var i in c.sections){
for(var j in c.sections[i]) {
c.sections[i][j][0] = -c.sections[i][j][0];
}
}
for(var i in c.skeleton) {
c.skeleton[i].offset = 180 - c.skeleton[i].offset;
if(c.skeleton[i].offset<0)
c.skeleton[i].offset += 360;
}
return c
}