-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrulers.js
428 lines (376 loc) · 14 KB
/
rulers.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
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
/*
*
* TODO:
* - add a option in menu to hide rulers
* - add the possibility to show only the vertical or horizontal ruler
* - add the persistence of some menu options (unit, ...)
*/
// ugly global variable , but I don't know how to get correctly a reference
// to the current script. In $.ready(...) context execution, it's too late.
// (document.currentScript is not working // at least with Chromium)
var script__ruler_js;
{
var scripts = document.getElementsByTagName('script');
script__ruler_js = scripts[scripts.length-1];
}
$(document).ready(function() {
// "rulers" singleton, embark all internal variables+methods necessary
var rulers = {
// default options
_OPTIONS: {
unit: "px",
ref: "body",
autoshow: "none", /* (none|horiz|vert|both) */
showmenu: "false", /* (false|true) or (0|1) */
},
// helper to read boolean value, return true or false
_getBoolOption: function( name )
{
var bVal = false;
var val = this._OPTIONS[name];
if ( typeof val == "string" )
{
val = val.toLowerCase();
bVal = ( val == "true" || val == "1" );
}
else if ( typeof val == "boolean" )
{
bVal = val;
}
return bVal;
},
// contains the conversion ratio between "cm" or "in" to "pixels"
_RATIOS: { 'px':1 },
init: function() {
// there's several way to give options
this._loadScriptParams();
this._loadURLSearchParams();
this._computeRatios( ['cm','in'] );
this._addHTMLElements();
this._drawTicks();
this._moveTicks();
this._registerWindowEvents();
},
_loadScriptParams: function()
{
if ( !script__ruler_js || !script__ruler_js.attributes ) return;
for( var o in this._OPTIONS ) {
var attr = script__ruler_js.attributes.getNamedItem('data-'+o);
if ( attr )
{
this._OPTIONS[o] = attr.value;
}
}
},
// Internal: Look for "ruler-*" parameters in the URL's search
// and populate the _OPTIONS members accordingly.
_loadURLSearchParams: function() {
var urlParams = new URLSearchParams(window.location.search);
for( var o in this._OPTIONS ) {
if ( urlParams.has('ruler-'+o) )
{
this._OPTIONS[o] = urlParams.get('ruler-'+o);
}
}
},
_computeRatios: function( units )
{
var div = $('<div>')
.css("visibility", "hidden")
.css("position", "absolute")
.css("top", "0px")
.css("left", "0px")
.css("margin","0")
.css("padding","0")
.css("border","none")
.appendTo( $(document.body) );
for( var u in units ) {
var unit = units[u];
div.css("width","1"+unit)
.css("height","1"+unit);
/* On Zepto.js, width() and height() round dimensions to integer
* and this is a problem for "cm" unit. jQuery works well.
* So let's use the pure javascript getBoundingClientRect() method */
var rect = div[0].getBoundingClientRect();
this._RATIOS[unit] = rect.width;
if ( rect.width != rect.height )
{
console.error("Ouch, conversion '"+unit+"' to 'px' is different vertically/horizontally !");
}
}
div.remove();
},
_HTMLElements: {},
_addHTMLElements: function()
{
var bShowHoriz = ( this._OPTIONS['autoshow'] == "both"
|| this._OPTIONS['autoshow'] == "horiz" );
var bShowVert = ( this._OPTIONS['autoshow'] == "both"
|| this._OPTIONS['autoshow'] == "vert" );
var divRulerH = $('<div class="ruler h">');
var divRulerV = $('<div class="ruler v">');
var divRulerMenu = $('<ul class="ruler menu">');
var divRulerCorner = $('<div class="ruler corner">').text(':');
( this._getBoolOption('showmenu')
? divRulerMenu.show()
: divRulerMenu.hide() )
if ( !bShowHoriz ) divRulerH.hide();
if ( !bShowVert ) divRulerV.hide();
divRulerH.appendTo($(document.body));
divRulerV.appendTo($(document.body));
divRulerMenu.appendTo($(document.body));
divRulerCorner.appendTo($(document.body));
var chkHoriz = $('<input type="checkbox" data-ruler="horiz">')
.prop('checked', bShowHoriz);
var chkVert = $('<input type="checkbox" data-ruler="vert">')
.prop('checked', bShowVert);
var liShow = $('<li>')
.append( $('<label>').text('Show:').addClass("name") )
.append( $('<label>').text('Horiz.').prepend(chkHoriz) )
.append( $('<label>').text('Vert.').prepend(chkVert) )
.appendTo(divRulerMenu);
var liCursorPosition = $('<li>')
.append( $('<label>').text('Mouse:').addClass("name") )
.append( $('<label>').addClass("cursor-position") )
.appendTo(divRulerMenu);
var list = $('<select>');
for( var u in this._RATIOS )
{
var option = $('<option>').attr('value',u).text(u);
if ( u == this._OPTIONS['unit'] ) {
option.attr("selected", "1");
}
list.append( option );
}
var liUnit = $('<li>')
.append( $('<label>').text('Unit:').addClass("name") )
.append( list )
.appendTo(divRulerMenu);
var self = this;
divRulerCorner.on('click',function(){
self._onCornerClick();
});
list.on('change',function(){
self._onUnitChange( $(this).val() );
});
chkHoriz.on('change', function() {
self._onShowChange($(this).is(':checked'), $(this).data("ruler"));
});
chkVert.on('change', function() {
self._onShowChange($(this).is(':checked'), $(this).data("ruler"));
});
var note = $('<sub>')
.text("For now, 'unit' option is not persistent and would be lost at each reload. \
If it's annoying, add '?ruler-unit=xx' to URL." )
.appendTo(divRulerMenu);
var close = $('<a>')
.addClass("close")
.text("×")
.appendTo( divRulerMenu )
.data("ruler-menu",divRulerMenu)
.on('click',function(){
$(this).data("ruler-menu").toggle();
});
// everytime the document is resized, we would have to recompute ticks again
var divTicksH = $('<div class="ticks">')
.width($(document).width())
.css("height","100%")
.appendTo(divRulerH);
var divTicksV = $('<div class="ticks">')
.height($(document).height())
.css("width","100%")
.appendTo(divRulerV);
// Some special ticks that will follow the cursor position
var tCx = $('<s>')
.addClass("cursor")
.css("left","0px")
.append( $('<label>') )
.appendTo(divTicksH);
var tCy = $('<s>')
.addClass("cursor")
.css("top","0px")
.append( $('<label>') )
.appendTo(divTicksV);
// save HTML elements in an array
this._HTMLElements['divRulerCorner'] = divRulerCorner;
this._HTMLElements['divRulerMenu'] = divRulerMenu;
this._HTMLElements['divRulerH'] = divRulerH;
this._HTMLElements['divRulerV'] = divRulerV;
this._HTMLElements['divTicksH'] = divTicksH;
this._HTMLElements['divTicksV'] = divTicksV;
this._HTMLElements['divTickMouseH'] = tCx;
this._HTMLElements['divTickMouseV'] = tCy;
},
_onCornerClick: function () {
this._HTMLElements['divRulerMenu'].toggle();
},
_onUnitChange: function( unit ) {
this._OPTIONS['unit'] = unit;
this._clearTicks();
this._drawTicks();
this._moveTicks();
return;
/* old code, used at a time the document was reloaded at each unit-change
var urlParams = new URLSearchParams(window.location.search);
urlParams.set('ruler-unit', this._OPTIONS['unit']);
window.location.search = urlParams.toString(); */
},
_onShowChange: function( val, name ) {
if ( name == "horiz" )
{
this._HTMLElements['divRulerH'].toggle();
}
else if ( name == "vert" )
{
this._HTMLElements['divRulerV'].toggle();
}
},
_registerWindowEvents: function()
{
var self = this;
$(window).scroll( function() {
self._moveTicks();
} );
$(window).resize(function() {
self._moveTicks();
});
$(window).mousemove( function(e) {
/* There is several (X,Y) information in mouseEvent 'e' :
* clientX|Y, offsetX|Y, pageX|Y and screenX|Y/
* The one that best suits need here is 'clientX|Y'. */
self._moveCursorTicks( { x: e.clientX, y: e.clientY } );
});
},
// return the offset of the "Ref" (reference) object
// called by _moveTicks() and _moveCursorTicks()
_getRefOffset: function()
{
var ref = $(this._OPTIONS['ref']);
var offset = ref.offset();
offset.top -= $(document.body).scrollTop();
offset.left -= $(document.body).scrollLeft();
return offset;
},
// Internal: repositionate the horizontal/vertical ".ticks" objects
// so that relative x=0, y=0 match ref's {left,top}
_moveTicks: function()
{
var offset = this._getRefOffset();
this._HTMLElements['divTicksH'].css("left",offset.left+"px");
this._HTMLElements['divTicksV'].css("top",offset.top+"px");
},
_TICK_SPECS: {
'px': {
// here, we're using a decimal 'divider' =0.1 to express
// we want to group 10px in each step
divider: 0.1,
ticksSize: [ 2, 2, 2, 2, 6, 2, 2, 2, 2, 10 ],
// size of ticks at each step
// every 5 ticks (50px) -> 60%
// every 10 ticks (100px) -> 100%
label: 10 // a label every 10 steps
},
'cm': {
divider: 2, // each cm would be subdivided into 2 steps
ticksSize: [ 2, 6, 2, 6, 2, 6, 2, 6, 2, 10 ],
// size of ticks at each step
// every 2 ticks (1cm) -> 60%
// every 10 ticks (5cm) -> 100%
label: 10 // a label every 10 steps
},
'in': {
divider: 16, // each inch would be subdivided into 16 steps
ticksSize: [ 2, 4, 2, 6, 2, 4, 2, 6, 2, 4, 2, 6, 2, 4, 2, 10 ],
// size of ticks at each step
// every 2 ticks (1/8in) -> 60%
// every 4 ticks (1/4in) -> 60%
// every 16 ticks (1in) -> 100%
label: 16 // a label every 16 steps
},
},
_drawTicks: function( unit, ref )
{
// for now, param 'unit' are 'ref' are not set
// we have to initialize them here, which is weird
// (TODO: use local variables or real parameters)
if ( unit === undefined ) unit = this._OPTIONS['unit'];
if ( ref === undefined ) ref = $(this._OPTIONS['ref']);
var getTickSize = function( spec, tick ) {
var index = (tick - 1) % (spec.ticksSize.length);
if ( index < 0 ) index += spec.ticksSize.length;
return ""+spec.ticksSize[index];
}
// round (floor) the number `x` to the integer count of `b`
// the result might not be an integer (that's the case for 'cm')
// it is `N * b` (where `N` is an integer)
var floor = function(x,b) { return Math.floor(x/b)*b; };
var spec = this._TICK_SPECS[unit];
var step = this._RATIOS[unit] / spec.divider;
var offset = ref.offset();
offset['floorTop'] = floor(offset.top, step);
offset['floorLeft'] = floor(offset.left, step);
for( var w2=-offset.floorLeft ; w2<($(document).width()-offset.floorLeft) ; w2+=step )
{
var w = w2.toFixed(4);
var tick = Math.round( w / step );
var tClass = "t"+getTickSize(spec,tick);
var t = $('<s>').addClass(tClass)
.css("left",(w)+"px")
.appendTo( this._HTMLElements['divTicksH']);
if ( tick % spec.label == 0 ) {
var label = Math.round( w / (spec.divider*step) );
$('<label>').text(label).appendTo(t);
}
}
for( var h2=-offset.floorTop ; h2<($(document).height()-offset.floorTop) ; h2+=step )
{
var h = h2.toFixed(4);
var tick = Math.round( h / step );
var tClass = "t"+getTickSize(spec,tick);
var t = $('<s>')
.addClass(tClass)
.css("top",(h)+"px")
.appendTo(this._HTMLElements['divTicksV']);
if ( tick % spec.label == 0 ) {
var label = Math.round( h / (spec.divider*step) );
$('<label>').text(label).appendTo(t);
}
}
},
_clearTicks: function() {
var tmpContainer = this._HTMLElements['divRulerCorner'];
this._HTMLElements['divTickMouseH'].hide().appendTo(tmpContainer);
this._HTMLElements['divTickMouseV'].hide().appendTo(tmpContainer);
this._HTMLElements['divTicksH'].empty();
this._HTMLElements['divTicksV'].empty();
this._HTMLElements['divTickMouseH'].show().appendTo(this._HTMLElements['divTicksH']);
this._HTMLElements['divTickMouseV'].show().appendTo(this._HTMLElements['divTicksV']);
},
_moveCursorTicks: function( cursorPosition )
{
// DO NOT use "this._HTMLElements['divTicks*'].position()" as it can be
// hidden and therefore uncomputable.
// use this._getRefOffset() instead.
var offset = this._getRefOffset();
var left = cursorPosition.x - offset.left;
var top = cursorPosition.y - offset.top
var tCx = this._HTMLElements['divTickMouseH'];
var tCy = this._HTMLElements['divTickMouseV'];
tCx.css("left", left+"px");
tCy.css("top", top+"px");
left = Math.floor( left );
top = Math.floor( top );
var unit = this._OPTIONS['unit'];
if ( unit != "px" )
{
var ratio = this._RATIOS[unit];
left = (left / ratio).toFixed(2);
top = (top / ratio).toFixed(2);
}
tCx.find('label').text(left);
tCy.find('label').text(top);
$('.ruler .cursor-position').text( left+" × "+top )
},
}; // end of singleton "rulers"
rulers.init(); // TODO: add options
});