-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphics.js
282 lines (264 loc) · 9.86 KB
/
graphics.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
// xLog - returns x position of value v between vMin and vMax
// in logarithmic scale with width xSize
function xLog(vMin, vMax, xSize, v) {
if (v >= vMin && v <= vMax) {
if (vMin > 0) {
return Math.log(v / vMin) / Math.log(vMax / vMin) * xSize;
} else {
return Math.log(v) / Math.log(vMax) * xSize;
}
} else {
return 0;
}
}
// drawGraphics draws the actual graphics for the given gearSet
//
function drawGraphics(canvas, gearSet, minDev, maxDev, cadence, dsplOps) {
//var nChainrings = 2;
var gWidth = canvas.width -1 ;
var gHeight = canvas.height - 1;
var gX = 0.5;//10.5;
var gY = 0.5;//10.5;
var ctx = canvas.getContext('2d');
// calculate the min and max values of the development scale ( 80%-115% of actual values);
var maxDev = maxDev * 1.15;
var minDev = minDev * 0.80;
// Draw Rectangle
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(gX, gY, gWidth, gHeight);
ctx.fillStyle = "#000000";
ctx.font = "12px sans-serif";
ctx.textAlign = "center";
ctx.strokeStyle = "#000000";
ctx.lineWidth = 1;
ctx.strokeRect(gX, gY, gWidth, gHeight);
// draw scale ticks for development (logarithmic scales) for SI oe US units
ctx.beginPath();
if (dsplOps.siUnits) {
// draw scale for Development/m
var iMinDev = Math.floor(minDev * 10);
var iMaxDev = Math.floor(maxDev * 10 + 1);
ctx.textAlign = "left";
ctx.fillText("%development".toLocaleString()+"/m", 10, 21);
ctx.textAlign = "center";
for (var i = iMinDev; i <= iMaxDev; i++) {
var x = gX + Math.round(xLog(minDev, maxDev, gWidth, i / 10));
if (x > gX) {
ctx.moveTo(x, gY);
//ctx.lineTo(x, gY+5);
if (i % 10 === 0) {
ctx.lineTo(x, gY + 10);
if (x > 80) {
ctx.fillText(i / 10, x, gY + 20);
}
} else if (i % 5 === 0) {
ctx.lineTo(x, gY + 8);
} else {
ctx.lineTo(x, gY + 5);
}
}
}
} else {
// draw scale for Gear Inches
var minGearInches = minDev * 100 / 2.54 / Math.PI;
var maxGearInches = maxDev * 100 / 2.54 / Math.PI;
var iMinGearInches = Math.floor(minGearInches);
var iMaxGearInches = Math.floor(maxGearInches + 1);
ctx.textAlign = "left";
ctx.fillText("Gear Inches", 10, 21);
ctx.textAlign = "center";
for (i = iMinGearInches; i <= iMaxGearInches; i++) {
x = gX + Math.round(xLog(minGearInches, maxGearInches, gWidth, i));
if (x > gX) {
ctx.moveTo(x, gY);
if (i % 10 === 0) {
ctx.lineTo(x, gY + 10);
if (x > 80) {
ctx.fillText(i, x, gY + 20);
}
} else if (i % 5 === 0) {
ctx.lineTo(x, gY + 8);
} else {
ctx.lineTo(x, gY + 5);
}
}
}
}
ctx.stroke();
ctx.closePath();
// draw scale ticks for speed (logarithmic scales)
var unitFactor = (dsplOps.siUnits) ? 60 / 1000 : 60 / 1609.3;
// km/h or mph
var minSpeed = minDev * cadence * unitFactor;
var maxSpeed = maxDev * cadence * unitFactor;
var iMinSpeed = Math.round(minSpeed + 0.5);
var iMaxSpeed = Math.round(maxSpeed - 0.5);
ctx.textAlign = "left";
ctx.fillText((dsplOps.siUnits)? "km/h" : "mph", 10, gHeight - 30 + 20);
ctx.textAlign = "center";
ctx.beginPath();
ctx.moveTo(gX, gY + gHeight - 30);
ctx.lineTo(gX + gWidth, gY + gHeight - 30);
for ( i = iMinSpeed; i <= iMaxSpeed; i++) {
x = gX + Math.round(xLog(minSpeed, maxSpeed, gWidth, i));
if (x > gX) {
ctx.moveTo(x, gY + gHeight - 30);
if (i % 5 === 0) {
ctx.lineTo(x, gY + gHeight - 30 + 10);
if (x > 40) {
ctx.fillText(i, x, gY + gHeight - 30 + 20);
}
} else {
ctx.lineTo(x, gY + gHeight - 30 + 5);
}
}
}
ctx.stroke();
ctx.closePath();
// Draw red Chainring lines and circles with #teeth
for ( i = 0; i < gearSet.Chainrings.length; i++) {
var y = Math.round(gHeight / (gearSet.Chainrings.length + 1) * (i + 1)) + gY -10;
ctx.strokeStyle = "#e34c26";
//ctx.strokeStyle = "#DD0000";
ctx.beginPath();
ctx.lineWidth = 1;
ctx.moveTo(gX, y);
ctx.lineTo(gX + gWidth, y);
ctx.stroke();
ctx.closePath();
//draw a circle
ctx.beginPath();
ctx.fillStyle = "#e34c26";
//ctx.fillStyle = "#DD0000";
ctx.arc(gX + gWidth - 20 , y, 10, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#FFFFFF";
ctx.font = "bold 12px sans-serif";
ctx.fillText(gearSet.Chainrings[i].toString(), gX + gWidth -20, y + 4 );
}
// Draw triangles for each Chainring Sprocket combination
var ratios = new Array();
ctx.fillStyle = "#000000";
ctx.font = "bold 11px sans-serif";
var tSize = 12;
for ( i = 0; i < gearSet.Chainrings.length; i++) {
y = Math.round(gHeight / (gearSet.Chainrings.length + 1) * (i + 1)) + gY -10.5;
for ( var j = 0; j < gearSet.Cogs.length; j++) {
if (gearSet.Chainrings[i] * gearSet.Cogs[j] !== 0) {
x = gX + Math.round(xLog(minDev, maxDev, gWidth, gearSet.Chainrings[i] / gearSet.Cogs[j] * gearSet.circumference / 1000));
if (x > gX + 12) {
ctx.beginPath();
//draw triangle for gear
ctx.moveTo(x - tSize, y - tSize);
ctx.lineTo(x + tSize, y - tSize);
ctx.lineTo(x, y + tSize);
ctx.lineTo(x - tSize, y - tSize);
// fill triangle either black or grey dep. of chain angle
ctx.fillStyle = (gearSet.ChainAngle[i][j] > dsplOps.maxChainAngle || gearSet.isGearHub)? "#E8E8E8" : "#000000" ;
ctx.fill();
ctx.closePath();
// write # sprocket teeth onto triangle
ctx.fillStyle = "rgb(200,200,200)";
ctx.fillText(gearSet.Cogs[j], x, y - 1);
ctx.fillStyle = "rgb(00,00,00)";
if (!gearSet.isGearHub){
switch(dsplOps.values) {
case "ratio":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]).toPrecision(3), x, y - 16);
break;
case "development":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.circumference/1000).toPrecision(3), x, y - 16);
break;
case "gear_inches":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.circumference/25.4/3.1415927).toPrecision(3), x, y - 16);
break;
case "speed":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.circumference/1000* cadence * unitFactor).toPrecision(3), x, y - 16);
break;
default:
}
}
// draw additional triangles for gear hubs
if (gearSet.isGearHub) {
if ( gearSet.Chainrings[i]/gearSet.Cogs[j] < gearSet.hubType.minRatio ){
ctx.textAlign = "left";
ctx.fillStyle = "#e34c26";
ctx.fillText("%torque_warning".toLocaleString(), 10, 80);
ctx.textAlign = "center";
}
for ( var k = 0; k < gearSet.HubGears.length; k++) {
var xgh = gX + Math.round(xLog(minDev, maxDev, gWidth, gearSet.Chainrings[i] / gearSet.Cogs[j]
* gearSet.HubGears[k]*gearSet.circumference / 1000));
ctx.beginPath();
//draw triangle for gear
ctx.moveTo(xgh - tSize, y - tSize);
ctx.lineTo(xgh + tSize, y - tSize);
ctx.lineTo(xgh, y + tSize);
ctx.lineTo(xgh - tSize, y - tSize);
// fill triangle either black or grey dep. of chain angle
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();
switch(dsplOps.values) {
case "ratio":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.HubGears[k]).toPrecision(3), xgh, y - 16);
break;
case "development":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.HubGears[k]*gearSet.circumference/1000).toPrecision(3), xgh, y - 16);
break;
case "gear_inches":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.HubGears[k]*gearSet.circumference/25.4/3.1415927).toPrecision(3), xgh, y - 16);
break;
case "speed":
ctx.fillText((gearSet.Chainrings[i]/gearSet.Cogs[j]*gearSet.HubGears[k]*gearSet.circumference/1000* cadence * unitFactor).toPrecision(3), xgh, y - 16);
break;
default:
}
}
ctx.fillStyle = "rgb(200,200,200)";
ctx.fillText(gearSet.Cogs[j], x, y - 1);
}
}
if (gearSet.ChainAngle[i][j] < dsplOps.maxChainAngle){
if (gearSet.isGearHub){
for ( k = 0; k < gearSet.HubGears.length; k++) {
ratios.push(gearSet.Ratios[i][j]*gearSet.HubGears[k]);
}
} else {
ratios.push(gearSet.Ratios[i][j]);
}
}
}
}
}
// draw rectangle with ticks for each possible gear and display gear steps
ratios.sort(function(a,b){return a-b;});
ctx.beginPath();
ctx.fillStyle = "rgb(150,150,150)";
ctx.textAlign = "left";
if (gearSet.hubType.id !== "DERS"){
ctx.fillText( gearSet.hubType.name, 10, 181);
}
ctx.fillText( "%wheel_size".toLocaleString() + " " + tireTypes.getNameByCircumference(gearSet.circumference), 10, 161);
ctx.fillRect(gX, gHeight -50, gWidth, 16);
ctx.fillStyle = "#FFFFFF";
ctx.strokeStyle = "#FFFFFF";
ctx.fillText( "%gear_step".toLocaleString(), 10, gHeight - 38);
ctx.fillText( Math.round(ratios[ratios.length -1 ]/ratios[0] * 100) +"%", gX + gWidth - 50, gHeight - 38);
ctx.textAlign = "center";
for ( i = 0; i < ratios.length; i++){
x = gX + Math.round(xLog(minDev, maxDev, gWidth, ratios[i] * gearSet.circumference / 1000));
ctx.moveTo(x, gHeight - 50);
ctx.lineTo(x, gHeight - 35);
if (i > 0){
var gearStep = Math.round(ratios[i] / ratios[i - 1] * 100 - 100);
if (gearStep > 1 ){
var x0 = gX + Math.round(xLog(minDev, maxDev, gWidth, ratios[i-1] * gearSet.circumference / 1000));
ctx.fillText(gearStep, (x + x0)/2, gHeight - 38);
}
}
}
ctx.stroke();
ctx.closePath();
}