forked from e-nable/Service-Station
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoinForScale.php
196 lines (148 loc) · 4.61 KB
/
CoinForScale.php
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
<!doctype html>
<html>
<head>
<title>Coin For Scale</title>
<meta charset="utf-8" />
<style type="text/css">
#canvas{background-color: #000;}
</style>
<script type="text/javascript">
var state = ''; // a state variable so we know what we are doing
var scalecircle = [];
var circlecenter = [];
var circleScalePixels = '';
var circleScale = '';
var lastClick = '';
document.addEventListener("DOMContentLoaded", init, false);
function init()
{
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", getPosition, false);
}
function getPosition(event)
{
var x = new Number();
var y = new Number();
var canvas = document.getElementById("canvas");
if (event.x != undefined && event.y != undefined)
{
x = event.x;
y = event.y;
}
else // Firefox method to get the position
{
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
if(state == 'scalecircle')
{
storeCoordinate(x, y, scalecircle);
if(scalecircle.length >= 3)
{
state = ''; // quit doing this by changing the state
var circlecenter = findCircleCenter(scalecircle[0],scalecircle[1],scalecircle[2]);
circleScalePixels = lineDistance(circlecenter, scalecircle[0] );
// lets draw this thing
var ctx=canvas.getContext("2d");
ctx.beginPath();
ctx.arc(circlecenter.x,circlecenter.y,circleScalePixels,0,2*Math.PI);
ctx.stroke();
circleScale = (circleScalePixels * 2) / document.getElementById('circleRealSize').value ;
}
}else
{
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(x, y);
context.lineTo(lastClick.x, lastClick.y);
context.stroke();
lineSize = lineDistance({x: x, y: y}, lastClick);
distance = lineSize / circleScale;
}
//alert("x: " + x + " y: " + y);
document.getElementById('console').innerHTML="Last line measured " + distance + "mm<br>Most Recent Click Coordinates x: " + x + " y: " + y + " previous x: " + lastClick.x + " y: " + lastClick.y + "<br>" + /*Scale Circle Center " + JSON.stringify(circlecenter, null, 4) + " giving us " +*/ circleScale + " pixels per mm";
lastClick = {x: x, y: y};
}
function loadAndDrawImage(url)
{
// Create an image object. This is not attached to the DOM and is not part of the page.
var image = new Image();
// When the image has loaded, draw it to the canvas
image.onload = function()
{
canvas.width = image.width;///4;
canvas.height = image.height;///4;
var context = canvas.getContext("2d");
//context.scale(.25,.25);
context.drawImage(image, 0, 0);
}
// Now set the source of the image that we want to load
image.src = url;
}
loadAndDrawImage("hand.png");
//loadAndDrawImage("http://hexagonmetrology.us/images/absolute/romer-absolute-arm-feature-points.jpg");
function defineCircle()
{
state = 'scalecircle';
// Clear the current circle
while(scalecircle.length > 0) {
scalecircle.pop();
}
document.getElementById('console').innerHTML="Click 3 points around the circle.";
}
function storeCoordinate(xVal, yVal, array) {
array.push({x: xVal, y: yVal});
}
function sqr(a) {
return a * a;
}
function dist(a, b) {
return Math.sqrt(sqr(a.x-b.x) + sqr(a.y - b.y));
}
function findCircleCenter(b, c, d) {
var temp = sqr(c.x) + sqr(c.y);
var bc = (sqr(b.x) + sqr(b.y) - temp) / 2.;
var cd = (temp - sqr(d.x) - sqr(d.y)) / 2.;
var det = (b.x-c.x) * (c.y-d.y) - (c.x-d.x) * (b.y-c.y);
if (Math.abs(det) < 1e-14)
{
alert("error in finding circle center");
return false;
}
var circ = new Object({
x: (bc * (c.y-d.y) - cd * (b.y-c.y)) / det,
y: ((b.x-c.x) * cd - (c.x-d.x) * bc) / det
});
//alert(JSON.stringify(circ, null, 4));
return circ;
}
function lineDistance( point1, point2 )
{
var xs = 0;
var ys = 0;
xs = point2.x - point1.x;
xs = xs * xs;
ys = point2.y - point1.y;
ys = ys * ys;
return Math.sqrt( xs + ys );
}
</script>
</head>
<body>
<canvas id="canvas" width="640" height="360">Unfortunatly, you cannot use this tool as your browser doesn't support it.</canvas>
<div id="console"></div>
<button type="button" name='circle' onclick='defineCircle()'>Circle</button>
<input type="number" step="any" min='0' placeholder='Diameter in mm' list='coins' id='circleRealSize' name='Diameter' required>
<datalist id='coins'>
<option value='42'>Logo</option>
<option value='19.05'>US Penny</option>
<option value='21.21'>US Nickel</option>
<option value='17.91'>US Dime</option>
<option value='24.26'>US Quarter</option>
</datalist>
</body>
</html>