-
Notifications
You must be signed in to change notification settings - Fork 45
/
jquery.ruler.js
163 lines (145 loc) · 6.02 KB
/
jquery.ruler.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
/**
* jQuery.Ruler v1.1
* Add Photoshop-like rulers and mouse position to a container element using jQuery.
* http://ruler.hilliuse.com
*
* Dual licensed under the MIT and GPL licenses.
* Copyright 2013 Hillius Ettinoffe http://hilliuse.com
*/
;(function( $ ){
$.fn.ruler = function(options) {
var defaults = {
vRuleSize: 18,
hRuleSize: 18,
showCrosshair : true,
showMousePos: true
};//defaults
var settings = $.extend({},defaults,options);
var hRule = '<div class="ruler hRule"></div>',
vRule = '<div class="ruler vRule"></div>',
corner = '<div class="ruler corner"></div>',
vMouse = '<div class="vMouse"></div>',
hMouse = '<div class="hMouse"></div>',
mousePosBox = '<div class="mousePosBox">x: 50%, y: 50%</div>';
if (!Modernizr.touch) {
// Mouse crosshair
if (settings.showCrosshair) {
$('body').append(vMouse, hMouse);
}
// Mouse position
if (settings.showMousePos) {
$('body').append(mousePosBox);
}
// If either, then track mouse position
if (settings.showCrosshair || settings.showMousePos) {
$(window).mousemove(function(e) {
if (settings.showCrosshair) {
$('.vMouse').css("top",e.pageY-($(document).scrollTop())+1);
$('.hMouse').css("left",e.pageX+1);
//-($(window).scrollTop())
}
if (settings.showMousePos) {
$('.mousePosBox').html("x:" + (e.pageX-settings.vRuleSize) + ", y:" + (e.pageY-settings.hRuleSize) ).css({
top: e.pageY-($(document).scrollTop()) + 16,
left: e.pageX + 12
});
}
});
}
}
//resize
$(window).resize(function(e){
var $hRule = $('.hRule');
var $vRule = $('.vRule');
$hRule.empty();
$vRule.empty().height(0).outerHeight($vRule.parent().outerHeight());
// Horizontal ruler ticks
var tickLabelPos = settings.vRuleSize;
var newTickLabel = "";
while ( tickLabelPos <= $hRule.width() ) {
if ((( tickLabelPos - settings.vRuleSize ) %50 ) == 0 ) {
newTickLabel = "<div class='tickLabel'>" + ( tickLabelPos - settings.vRuleSize ) + "</div>";
$(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
} else if ((( tickLabelPos - settings.vRuleSize ) %10 ) == 0 ) {
newTickLabel = "<div class='tickMajor'></div>";
$(newTickLabel).css("left",tickLabelPos+"px").appendTo($hRule);
} else if ((( tickLabelPos - settings.vRuleSize ) %5 ) == 0 ) {
newTickLabel = "<div class='tickMinor'></div>";
$(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
}
tickLabelPos = (tickLabelPos + 5);
}//hz ticks
// Vertical ruler ticks
tickLabelPos = settings.hRuleSize;
newTickLabel = "";
while (tickLabelPos <= $vRule.height()) {
if ((( tickLabelPos - settings.hRuleSize ) %50 ) == 0) {
newTickLabel = "<div class='tickLabel'><span>" + ( tickLabelPos - settings.hRuleSize ) + "</span></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
} else if (((tickLabelPos - settings.hRuleSize)%10) == 0) {
newTickLabel = "<div class='tickMajor'></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
} else if (((tickLabelPos - settings.hRuleSize)%5) == 0) {
newTickLabel = "<div class='tickMinor'></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
}
tickLabelPos = ( tickLabelPos + 5 );
}//vert ticks
});//resize
return this.each(function() {
var $this = $(this);
// Attach rulers
// Should not need 1 min padding-top of 1px but it does
// will figure it out some other time
$this.css("padding-top", settings.hRuleSize + 1 + "px");
if (settings.hRuleSize > 0) {
$(hRule).height(settings.hRuleSize).prependTo($this);
}
if (settings.vRuleSize > 0) {
var oldWidth = $this.outerWidth();
$this.css("padding-left", settings.vRuleSize + 1 + "px").outerWidth(oldWidth);
$(vRule).width(settings.vRuleSize).height($this.outerHeight()).prependTo($this);
}
if ( (settings.vRuleSize > 0) && (settings.hRuleSize > 0) ) {
$(corner).css({
width: settings.vRuleSize,
height: settings.hRuleSize
}).prependTo($this);
}
var $hRule = $this.children('.hRule');
var $vRule = $this.children('.vRule');
// Horizontal ruler ticks
var tickLabelPos = settings.vRuleSize;
var newTickLabel = "";
while ( tickLabelPos <= $hRule.width() ) {
if ((( tickLabelPos - settings.vRuleSize ) %50 ) == 0 ) {
newTickLabel = "<div class='tickLabel'>" + ( tickLabelPos - settings.vRuleSize ) + "</div>";
$(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
} else if ((( tickLabelPos - settings.vRuleSize ) %10 ) == 0 ) {
newTickLabel = "<div class='tickMajor'></div>";
$(newTickLabel).css("left",tickLabelPos+"px").appendTo($hRule);
} else if ((( tickLabelPos - settings.vRuleSize ) %5 ) == 0 ) {
newTickLabel = "<div class='tickMinor'></div>";
$(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
}
tickLabelPos = (tickLabelPos + 5);
}//hz ticks
// Vertical ruler ticks
tickLabelPos = settings.hRuleSize;
newTickLabel = "";
while (tickLabelPos <= $vRule.height()) {
if ((( tickLabelPos - settings.hRuleSize ) %50 ) == 0) {
newTickLabel = "<div class='tickLabel'><span>" + ( tickLabelPos - settings.hRuleSize ) + "</span></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
} else if (((tickLabelPos - settings.hRuleSize)%10) == 0) {
newTickLabel = "<div class='tickMajor'></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
} else if (((tickLabelPos - settings.hRuleSize)%5) == 0) {
newTickLabel = "<div class='tickMinor'></div>";
$(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
}
tickLabelPos = ( tickLabelPos + 5 );
}//vert ticks
});//each
};//ruler
})( jQuery );