-
Notifications
You must be signed in to change notification settings - Fork 0
/
flightpanel-speed.html
211 lines (182 loc) · 6.32 KB
/
flightpanel-speed.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
<link rel="import" href="../polymer/polymer.html">
<!--
The `flightpanel-speed` element renders an analog Speed indicator.
@element flightpanel-speed
@status alpha
@homepage https://github.com/hb9cwp/flightpanel-speed
-->
<dom-module id="flightpanel-speed">
<template>
<link rel="stylesheet" href="flightpanel-speed.css">
<!-- <p>Air speed is <strong>{{speed}}</strong> kts</p> -->
<div id="canvasContainer">
<!-- <canvas id="canvasID" class="canvasClass" width="{{width}}" height="{{height}}"></canvas> -->
<canvas id="canvas0" width="{{width}}" height="{{height}}"></canvas>
<canvas id="canvas1" width="{{width}}" height="{{height}}"></canvas>
</div>
<content></content>
</template>
<script>
'use strict';
Polymer({
is: "flightpanel-speed",
properties: {
/**
* Changes `width` of the indicator from its default (in px).
*
* @attribute width
* @type number
* @default 300
*/
width: 300,
/**
* Changes `height` of the indicator from its default (in px).
*
* @attribute height
* @type number
* @default 300
*/
height: 300,
/**
* The hand tracks the `speed` (in kts).
*
* @attribute speed
* @type number
* @default 0
*/
speed: {
type: Number,
notify: true,
observer: 'speedChanged',
},
},
min: 0,
max: 0,
angles: [],
created: function() {
// interpolation etc.
setup(this);
},
ctx1: null,
hand: null,
// on ready, not on created, unlike shown in some examples!
ready: function () {
var that= this
// using label ID
var canvas0 = this.$.canvas0;
var canvas1 = this.$.canvas1;
// or using class
//var canvas= this.shadowRoot.querySelector('.canvasClass')
var ctx0 = canvas0.getContext('2d')
that.ctx1 = canvas1.getContext('2d')
var dial= new Image()
that.hand= new Image()
dial.onload = function(){
ctx0.drawImage(dial,0,0,that.width,that.height); // scale to canvas size
}
//dial.src= "../flightpanel-speed/speedDial.png"
dial.src= "./speedDial.png"
that.hand.onload = function() {
/* Variant A: save/restore */
// 0. save context, e.g. offset etc.
that.ctx1.save()
// 1. Move context to the center point of rotation (defaults to upper left (0,0)).
that.ctx1.translate(that.width/2, that.height/2)
// 2. Rotate around center point (axle of hand)
that.ctx1.rotate(that.angles[that.speed]*(Math.PI/180))
// 3. draw with negative offsets
that.ctx1.drawImage(that.hand,-that.width/2,-that.height/2,that.width,that.height); // scale to canvas size
// 4. restore context for next rotation, as offset is still translated
that.ctx1.restore()
/* Variant B: translate forth and back, without save/restore */
/*
// 1. Move context to the center point of rotation (defaults to upper left (0,0)).
that.ctx1.translate(that.width/2, that.height/2)
// 2. Rotate around center point (axle of hand)
that.ctx1.rotate(that.angles[that.speed]*(Math.PI/180))
// 3. translate offset back
that.ctx1.translate(-that.width/2, -that.height/2)
// 4. draw at (0,0)
that.ctx1.drawImage(that.hand,0,0,that.width,that.height) // scale to canvas size
*/
}
//that.hand.src= "../flightpanel-speed/speedHand.png"
that.hand.src= "./speedHand.png"
},
speedChanged: function () {
var speed = Math.round(this.speed)
if (speed < this.min) {
speed = this.min
} else if (speed > this.max) {
speed = this.max
}
this.ctx1.clearRect(0, 0, this.width, this.height);
/* Variant A: save/restore */
this.ctx1.save()
this.ctx1.translate(this.width/2, this.height/2)
this.ctx1.rotate(this.angles[speed]*(Math.PI/180))
this.ctx1.drawImage(this.hand,-this.width/2,-this.height/2,this.width,this.height) // scale to canvas size
this.ctx1.restore()
/* Variant B: translate forth and back, without save/restore
Note: B fails as rotations of canvas cumulate without the restore! */
/*
this.ctx1.translate(this.width/2, this.height/2)
this.ctx1.rotate(this.angles[speed]*(Math.PI/180))
this.ctx1.translate(-this.width/2, -this.height/2)
this.ctx1.drawImage(this.hand,0,0,this.width,this.height) // scale to canvas size
*/
}
});
function setup(that) {
// from (scale modified for Si2)
// https://github.com/dmolin/flightSimPanels/blob/master/src/services/measures/speed/client/speed.js
//console.log("setup() started...")
var interp = {
"0": 0.01,
"5": 31,
"7": 50,
"10": 72,
"12": 93,
"15": 116,
"17": 140,
"20": 164,
"22": 186,
"25": 205,
"27": 221,
"30": 237,
"32": 251,
"35": 265,
"37": 278,
"40": 290,
"42": 303,
"45": 317
},
zeroAngle = 0,
angles = [];
var keys = Object.keys(interp).sort(function(a, b) {
return parseInt(a, 10) - parseInt(b, 10);
});
var min = parseInt(keys[0], 10),
max = parseInt(keys[keys.length - 1], 10);
for (var i = min; i <= max; i++) {
if (interp[i]) {
angles[i] = interp[i] + zeroAngle;
}
}
for (var i = 0, x = keys.length - 1; i < x; i++) {
var current_key = parseInt(keys[i], 10);
var current_val = parseInt(interp[current_key], 10);
var next_key = parseInt(keys[i + 1], 10);
var next_val = parseInt(interp[next_key], 10);
var diff = (next_val - current_val) / (next_key - current_key);
for (var q = current_key + 1, z = next_key; q < z; q++) {
angles[q] = angles[q - 1] + diff;
}
}
that.min = min;
that.max = max;
that.angles = angles;
console.log("setup() done!")
}
</script>
</dom-module>