-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruler.html
482 lines (473 loc) · 19.8 KB
/
ruler.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<!--Copyright (c) 2011, Yonathan Randolph. This is released under BSD. -->
<title>Free on-screen ruler on the Web.</title>
<!--Try to get it to work for IE6-8 users.-->
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name=description content="Ruler with centimeters and inches. Configurable for different screens.">
<style>
html, body {margin:0}
#canvas {
position:fixed;
top:0;right:0;bottom:0;left:0;
width:100%;height:100%
}
#non-ruler {margin: 1em; position:fixed; width: 100%; bottom: 0; left: 0}
/* copied from stackoverflow: */
kbd{
padding:2px 4px;
white-space:nowrap;
color:#000;
background:#eee;
border-width:1px 3px 3px 1px;
border-style:solid;
border-color:#ccc #aaa #888 #bbb;
}
</style>
<body><!--Firefox requires the body tag or else document.getElementById will fail-->
<canvas id=canvas width="600" height="400"></canvas>
<!--
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js"></script>
-->
<script>
/**
* Let's get our coordinate spaces correct.
*
* Most things are in CSS pixels. any element's style.width, clientWidth,
* offsetWidth, etc. are in CSS pixels. Some important dimensions in CSS pixels:
* * "layout viewport": document.documentElement.clientWidth and -Height.
* The width of the viewport (minus scroll bars).
* This is the value that style.width: 100% means (on an element in viewport
* element's block formatting context)
* On an iPhone, this is the width of the document when fully zoomed out.
* On mobile it doesn't change when zoomed; layout is not recalculated.
* On desktop it changes to match the size of the visual vieport (except that
* it doesn't include the scrollbar).
* * "visual viewport": window.innerWidth and -Height. It becomes small when
* the user zooms in on an iPhone because only a few CSS pixels can fit in
* the window.
* * "document width": jQuery(document).width()
* (which gets max of clientWidth, scrollWidth, offsetWidth)
* the width of the document, which is equal to the "layout viewport" width
* but if the document overflows, it should include that overflow.
* FF: getComputedStyle(document.documentElement).width
* (webkit erroneously returns width of viewport even if document overflows)
* Webkit, FF<4: document.width (but screen pixels only)
* aka: "browser pixels", "logical" pixels (IE)
*
* Within the canvas, there's a new coordinate space with
* canvas.width by canvas.height pixels (which are the same as
* canvas.getAttribute('width') and and likewise for height). These are
* also the intrinsic CSS width/height, if you don't specify them explicitly
* (similar to how an image has an intrinsic height--except that a canvas's
* intrinsic width changes whenever you set the canvas.width). Typically
* the canvas is stored as a bitmap that stores canvas pixels, but HTML5 says
* that the browser is free to use a higher-resolution bitmap to store the
* canvas pixels. Not sure how that would work though.
*
* Of course, when you issue draw commands, they go through the 2d context's
* transformation matrix before being plotted on the canvas, but I'm assuming
* you know how to use the canvas.
*
* Next, there's the screen pixels, which refer to the reported resolution of
* the screen. Normally, this is the same as the physical pixels, but on
* high-res mobile browsers the screen resolution is less than the device
* resolution. Here are the things that are measured in screen pixels:
* 1) screen.width and height: the screen resolution (e.g. 1024x768).
* (also screen.availWidth and -Height, which doesn't include the start menu
* etc.)
* 2) window.screenX and -Y (screenLeft and -Right on IE):
* the position of the outer border of the browser
* widnow from the top left of the screen.
* 3) window.outerWidth and -Height: the size of the browser window within
* the screen.
* 4) event.screenX and -Y: the position of the mouse relative to the top
* left of the screen.
* These are in CSSOM-View, although that spec doesn't even mention device
* pixels. The spec has caught up to IE 6 but is unaware of modern browsers'
* zoom (before, browsers would zoom text only without having device pixels
* separate from screen pixels).
* aka: "system" pixels (IE8+)
*
* Finally, there are the "device pixels," which are the physical pixels that
* the browser draws to. Most of the time this is the same thing as screen
* pixels, but iPhone 4 and Nexus One report a low screen-resolution (320px)
* but actually have a high device resolution. (Actually, Nexus One reports
* screen pixels = device pixels (~800px) unless you set
* <meta name=viewport content="width=device-width">
* and then it reports screen width = 320px)
* The ratio between screen pixels and device pixels is window.devicePixelRatio
* (webkit only so far).
* [1]: http://www.quirksmode.org/blog/archives/2010/09/combining_meta.html
*
* So how do they relate to one another?
* * CSS pixels / canvas pixels = canvas.clientWidth / canvas.width
* * screen pixels / CSS pixels: "zoom level" in modern desktop browsers
* IE 8: screen.systemXDPI / screen.logicalXDPI
* IE 7: var body = document.body,r = body.getBoundingClientRect(); return (r.left-r.right)/body.offsetWidth [2], [6]
* Webkit, FF<4: document.width / document.documentElement.clientWidth [5]
* (but if document overflows the viewport, Webkit gives wrong answer)
* Old webkit: getComputedStyle(document.documentElement,null).getPropertyCSSValue('width').getFloatValue(CSSPrimitiveValue.CSS_PX) / document.documentElement.clientWidth
* Opera: document.documentElement.offsetWidth / window.innerWidth [7]
* (off by scrollbar)
* (Qurksmode says it's a bug; innerWidth should be CSS px) [8]
* Other: Flash solution [4]
* Alternately, measure change in screenX / change in clientX
* FF4+: media queries binary search
* * device pixels / screen pixels:
* IE 8: screen.deviceXDPI / screen.systemXDPI
* Webkit (Nexus One, iPhone4): window.devicePixelRatio.
[2]: http://help.dottoro.com/ljgshbne.php
[3]: Adjusting Scale for Higher DPI Screens: the equivalent of
"a pixel is not a pixel"
http://msdn.microsoft.com/en-us/library/ms537625(v=vs.85).aspx
[4]: http://blog.sebastian-martens.de/2009/12/how-to-detect-the-browser-zoom-level-change-browser-zoo/
[5]: http://stackoverflow.com/questions/1713771/how-to-detect-page-zoom-level-in-all-modern-browsers
[6]: http://stackoverflow.com/questions/680596/how-to-get-zoom-level-in-internet-explorer-7-javascript
[7]: http://virtuelvis.com/archives/2005/05/opera-measure-zoom
[8]: http://www.quirksmode.org/m/widths.html
* See this guide to different kinds of pixels:
* http://www.quirksmode.org/mobile/viewports.html
* See this reference for the nitty gritty.
* http://www.quirksmode.org/m/widths.html
* more:
* http://menacingcloud.com/?c=highPixelDensityDisplays
*/
/** This function returns 96.
*
* I thought this would do the trick (and looking at other Web rulers, other
* people thought so too), but unfortunately it doesn't work on modern browsers.
*
* Originally, CSS defined "absolute" units like cm and in as the physical size
* on the screen, which you can configure by changing the dpi in the
* operating system's display preferences. However, since nobody used this
* responsibly (instead assuming the Windows default of 96dpi), CSS 2.1 has
* taken away this tool, and now one 1in = 96px by law. In other words, CSS
* can no longer provide any clues to the actual DPI. In Ian Hickson's defense,
* this is probably the *only* time I've ever wanted to get the px/in ratio.
*
* See this post for explanation.
* http://www.webkit.org/blog/57/css-units/
* As well as this one on the Mozilla side:
* http://weblogs.mozillazine.org/roc/archives/2010/01/css_absolute_le.html
*
* Note: it seems that IE has added a screen.deviceXDPI property but
* it also has nothing to do with physical DPI (it just assumes 96dpi).
* So it's just more silliness. See
* http://msdn.microsoft.com/en-us/library/cc849094(v=vs.85).aspx
*/
var cssDpi = function() {
var d = document.createElement('div');
d.style.width = '10in';
d.style.height = '10px';
document.body.appendChild(d);
var dpi = d.offsetWidth / 10;
document.body.removeChild(d);
return dpi;
};
var clampDpi = function(dpi) {
if (dpi < 5) dpi = 5;
if (dpi > 1000) dpi = 1000;
return dpi;
};
var canvas = document.getElementById('canvas');
if (!canvas.getContext) {
canvas.style.display = 'none';
document.write(
'<h1>Sorry, you\u2019re using an obsolete browser. ' +
'Come back with Chrome, Firefox, Opera,' +
'<a href="http://www.google.com/chromeframe">Chrome Frame</a>, etc.</h1>');
}
var c = canvas.getContext('2d');
var clearRuler = function() {
var rulerLength = Math.sqrt(canvas.width*canvas.width + canvas.height*canvas.height);
if (options.flipped) {
c.clearRect(10, -65, -rulerLength - 20, 130);
} else {
c.clearRect(-2, -65, rulerLength + 20, 130);
}
if (!totemMarker || !totemMarker.h) return;
var edgeX = totemMarker.w * options.dpi + 8;
if (options.flipped) {
c.clearRect(4, -4, -edgeX, totemMarker.h * options.dpi + 8);
} else {
c.clearRect(-4, -4, edgeX, totemMarker.h * options.dpi + 8);
}
};
/** @type {{w: number, h: (number|undefined)}} */
var totemMarker = null;
/**
* Draws the ticks and numbers. The tick lengths are given in an array.
*
* Caller is responsible for calling stroke(); beginPath() after this function.
*
* @param {!CanvasRenderingContext2D} c
* @param {!Array.<number>} array of heights of ticks (px)
* @param {number} tickDistance horizontal distance between each tick (px)
* @param {number} rulerLength horizontal length of ruler (px)
* @param {boolean} isAboveLine true if we should draw above the line.
*/
function drawRulerHelper(c, ticks, tickDistance, rulerLength, isAboveLine) {
var i = 0;
c.textBaseline = isAboveLine ? 'bottom' : 'top';
var numTicks = rulerLength / tickDistance;
var y0 = ticks[0];
if (! isAboveLine) y0 = -y0;
// Workaround for http://crbug.com/87097 : draw the leftmost vertical line
// twice, by itself, so that it doesn't get cut off in Chrome.
c.stroke();
c.beginPath();
c.moveTo(0,0);
c.lineTo(0,y0);
c.stroke();
c.beginPath();
for (var i = 0; i < numTicks; ++i) {
var x = i * tickDistance;
if (options.flipped) x = -x;
var y = ticks[i%ticks.length];
if (! isAboveLine) y = -y;
c.moveTo(x,0);
c.lineTo(x,y);
if (i % ticks.length == 0) {
c.fillText(i/ticks.length, x + 3, y);
}
}
}
var drawRuler = function() {
var dpi = options.dpi;
c.strokeStyle = 'black';
c.beginPath();
c.moveTo(0, 0);
var rulerLength = Math.sqrt(canvas.width*canvas.width + canvas.height*canvas.height);
if (options.flipped) c.lineTo(-rulerLength, 0);
else c.lineTo(rulerLength, 0);
if (options.drawInches) {
// 1 tick = 1/16 in = dpi/16 px.
var tickDistance = dpi/16
, isAboveLine = options.drawInches === 1
, ticks = [];
for (var i = 0; i < 16; i++) {
var y = i % 16 == 0 ? 50 : i % 8 === 0 ? 30 : i % 4 === 0 ? 20 : i % 2 === 0 ? 15 : 10;
ticks.push(y);
}
drawRulerHelper(c, ticks, tickDistance, rulerLength, isAboveLine);
}
if (options.drawMetric) {
// 1 tick = 1 mm = 1/25.4 in = dpi/25.4px
var tickDistance = dpi/25.4
, isAboveLine = options.drawMetric === 1
, ticks = [30, 10, 10, 10, 10, 20, 10, 10, 10, 10];
drawRulerHelper(c, ticks, tickDistance, rulerLength, isAboveLine);
}
c.stroke();
c.beginPath();
if (totemMarker) {
c.strokeStyle = 'green';
var marker = totemMarker;
var edgeX = marker.w * dpi;
if (options.flipped) edgeX = -edgeX;
c.moveTo(edgeX, -60);
c.lineTo(edgeX, 60);
if (marker.h) {
// Workaround for http://crbug.com/87097 : draw the leftmost vertical line
// twice, by itself, so that it doesn't get cut off in Chrome.
c.stroke();
c.beginPath();
c.moveTo(0, 0);
c.lineTo(0, marker.h*dpi);
c.stroke();
c.beginPath();
c.moveTo(0, 0);
c.lineTo(edgeX, 0);
c.lineTo(edgeX, marker.h*dpi);
c.lineTo(0, marker.h*dpi);
c.lineTo(0, 0);
}
c.stroke();
c.beginPath();
}
};
var dpiCalibrated = false;
var currentTransform = {x: 20, y: 50, angle: 0};
var options = {dpi: 96, drawMetric: 2, drawInches: 1, flipped: false};
var screenInfo = {
screenWidthPx: screen.width, screenHeightPx: screen.height};
window.onresize = function() {
// TODO: set resolution-dependent width
canvas.width = canvas.offsetWidth; //document.documentElement.clientWidth;
canvas.height = canvas.offsetHeight; // document.documentElement.clientHeight;
c.translate(currentTransform.x, currentTransform.y);
c.rotate(currentTransform.angle);
drawRuler();
};
// TODO: save this before so we don't have to do it on unload
onunload = function() {
// store cookie for 30 days
var c = 'ruleroptions='
if (dpiCalibrated) c += 'dpi=' + options.dpi + '&';
c += 'm=' + options.drawMetric + '&i=' + options.drawInches + '; Max-Age=2592000;';
document.cookie = c;
};
var parseCookie = function() {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; ++i) {
var idx = cookies[i].indexOf('=');
var name = cookies[i].substr(0, idx).replace(/^\s+|\s+$/g, '');
if (name != 'ruleroptions') continue;
var value = cookies[i].substr(idx+1).replace(/^\s+|\s+$/g, '');
var cookieParts = value.split('&');
for (var j = 0; j < cookieParts.length; ++j) {
var eq = cookieParts[j].indexOf('=');
var k = cookieParts[j].substr(0, eq);
var v = cookieParts[j].substr(eq+1);
if (k == 'dpi') {
options.dpi = clampDpi(parseFloat(v) || 96);
dpiCalibrated = true;
} else if (k == 'm') {
options.drawMetric = '1' == v ? 1 : v == '0' ? 0 : 2;
} else if (k == 'i') {
options.drawInches = '1' == v ? 1 : v == '0' ? 0 : 2;
}
}
}
if (options.drawInches == 1 && options.drawMetric == 1) {
options.drawMetric = 2;
} else if (options.drawInches == 2 && options.drawMetric == 2) {
options.drawMetric = 1;
}
};
parseCookie();
var MouseDownModes = {
MOVING: 0,
CALIBRATING: 1,
ROTATING: 2
};
canvas.addEventListener('mousedown', function(e) {
// clientX is position relative to viewport in CSS pixels.
var dragStartX = e.clientX, dragStartY = e.clientY;
var lastClientX = dragStartX, lastClientY = dragStartY;
var lastMouseAngle = Math.atan2(e.clientY - currentTransform.y,
e.clientX - currentTransform.x);
var mode = MouseDownModes.MOVING;
var resizeToTotem = function(e) {
// project the click point onto the ruler vector, which is
var a = e.clientX - currentTransform.x;
var b = e.clientY - currentTransform.y;
var r = Math.sqrt(a*a + b*b);
var theta = Math.atan2(b, a);
var projection = r * Math.cos(theta - currentTransform.angle);
if (options.flipped) {
projection = -projection;
}
var newDpi = projection / totemMarker.w;
newDpi = clampDpi(newDpi);
options.dpi = newDpi;
};
if (totemMarker) {
mode = MouseDownModes.CALIBRATING;
}
if (e.shiftKey) mode = MouseDownModes.MOVING; // even while calibrating, you can move
else if (e.ctrlKey) mode = MouseDownModes.ROTATING;
if (mode === MouseDownModes.CALIBRATING) {
clearRuler();
resizeToTotem(e);
drawRuler();
}
var onmousemove = function(e) {
e.preventDefault(); // prevent text selection
e.stopPropagation();
// canvas.width = canvas.width; // clear the canvas and also the transform
clearRuler();
if (mode === MouseDownModes.MOVING) {
var dx = e.clientX - lastClientX;
var dy = e.clientY - lastClientY;
c.rotate(-currentTransform.angle)
c.translate(dx, dy);
c.rotate(currentTransform.angle)
currentTransform.x += dx;
currentTransform.y += dy;
} else if (mode === MouseDownModes.CALIBRATING) {
resizeToTotem(e);
} else if (mode === MouseDownModes.ROTATING) {
var currentMouseAngle = Math.atan2(e.clientY - currentTransform.y,
e.clientX - currentTransform.x);
var angleChange = currentMouseAngle - lastMouseAngle;
currentTransform.angle += angleChange;
c.rotate(angleChange);
lastMouseAngle = currentMouseAngle;
}
drawRuler();
lastClientX = e.clientX;
lastClientY = e.clientY;
};
var onmouseup = function(e) {
document.removeEventListener('mousemove', onmousemove, true);
document.removeEventListener('mouseup', onmouseup, false);
window.onblur = null;
};
document.addEventListener('mousemove', onmousemove, true);
document.addEventListener('mouseup', onmouseup, false);
window.onblur = onmouseup;
}, false);
var setTotem = function(o) {
clearRuler();
totemMarker = o;
drawRuler();
};
window.onresize();
</script>
<div id=non-ruler>
A ruler on the screen with centimeters and inches.
Tips: Drag to move the ruler; <kbd>Ctrl</kbd>-drag to rotate.<br>
Options:
<label><input id=drawMetricCheckbox type=checkbox checked onchange="options.drawMetric = this.checked ? (options.drawInches == 1 ? 2 : 1) : 0; clearRuler(); drawRuler()">centimeters.</label>
<label><input id=drawInchesCheckbox type=checkbox checked onchange="options.drawInches = this.checked ? (options.drawMetric == 1 ? 2 : 1) : 0; clearRuler(); drawRuler()">inches.</label>
<label><input type=checkbox onchange="clearRuler(); options.flipped = this.checked; drawRuler();">flipped.</label>
<script>
document.getElementById('drawMetricCheckbox').checked = options.drawMetric != 0;
document.getElementById('drawInchesCheckbox').checked = options.drawInches != 0;
</script>
<a href="#" id=pleaseCalibrateLink
onclick="this.style.fontSize='1em'; var a = this; (function(s) {if (s.display=='none') {s.display='block'; a.innerHTML = 'Click when you&#x2019;re done calibrating.';} else {s.display='none'; dpiCalibrated = true; a.innerHTML = 'Calibrate.';}})(document.getElementById('calibration').style); setTotem(null); return false;"
style="font-size: 3em;">Please calibrate your ruler.</a>
<a href=rulerinfo.html>About.</a>
<div id=calibration style="display:none">
<script>
if (dpiCalibrated) {
var a = document.getElementById('pleaseCalibrateLink');
a.onclick();
a.onclick();
}
</script>
Choose your totem. Once you’ve chosen your totem, then hold it up to the
screen and click where the edge is.
<ul>
<li><a href="#" onclick="setTotem({w:6.14,h:2.61,name:'dollar bill'});return false;">The reserve currency.</a>
<li><a href="#" onclick="setTotem({w:3.370,h:2.125,name:'credit card'});return false;">We take plastic too.</a>
<li>Sheet of paper:
<a href="#" onclick="setTotem({w:8.5,h:11,name:'letter paper'});return false;">Letter</a>,
<a href="#" onclick="setTotem({w:21.0/2.54,h:29.7/2.54,name:'a4 paper'});return false;">A4</a>
<li>Some other object you know the length of:
<input size=4 min=0 step=0.1 type=number id=customTotemLength>
<select id=customTotemUnits>
<option value=cm selected>cm</option><option value=in>in</option>
</select>
<script>
var customTotemLength = document.getElementById('customTotemLength');
var customTotemUnits = document.getElementById('customTotemUnits');
var onCustomTotemChange = function() {
customTotemLength.step = customTotemUnits.value == 'cm' ? 0.1 : 0.0625;
if (!customTotemLength.value) return null;
var x = parseFloat(customTotemLength.value);
if (!x || x <= 0) return;
var unit = customTotemUnits.value == 'cm' ? 1/2.54 : 1;
setTotem({w: x*unit, name: 'custom totem'});
};
customTotemLength.addEventListener('input', onCustomTotemChange, false);
customTotemUnits.addEventListener('input', onCustomTotemChange, false);
customTotemLength.addEventListener('change', onCustomTotemChange, false);
customTotemUnits.addEventListener('change', onCustomTotemChange, false);
</script>
</ul>
</div>
</div>
</body>