diff --git a/README.md b/README.md index c8a4c3f..2be0a1e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Behaviors : * angleArc : arc size in degrees | default=360. * stopper : stop at min & max on keydown/mousewheel | default=true. * readOnly : disable input and events | default=false. +* allowFractions : do not round the output to the nearest integer. | default=false UI : * cursor : display mode "cursor" | default=gauge. diff --git a/js/jquery.knob.js b/js/jquery.knob.js index d3a6026..ba61799 100644 --- a/js/jquery.knob.js +++ b/js/jquery.knob.js @@ -25,7 +25,7 @@ var k = {}, // kontrol max = Math.max, min = Math.min; - + k.c = {}; k.c.d = $(document); k.c.t = function (e) { @@ -64,7 +64,11 @@ this.eH = null; // cancel hook this.rH = null; // release hook - this.run = function () { + this.run = function (o) { + + if (!o) // for backwards compatibility, although if you don't pass o you can run into problems + o = this.o; + var cf = function (e, conf) { var k; for (k in conf) { @@ -82,8 +86,8 @@ this.o = $.extend( { // Config - min : this.$.data('min') || 0, - max : this.$.data('max') || 100, + min : this.$.data('min') || this.$.attr('min') || 0, + max : this.$.data('max') || this.$.attr('max') || 100, stopper : true, readOnly : this.$.data('readonly'), @@ -104,9 +108,17 @@ change : null, // function (value) {} cancel : null, // function () {} release : null // function (value) {} - }, this.o + }, o ); + // extirpate the source of all frustration + this.o.max = parseFloat(this.o.max); + this.o.min = parseFloat(this.o.min); + + if (Math.abs(this.o.max - this.o.min) <= 1) { + this.o.allowFractions = true; + } + // routing value if(this.$.is('fieldset')) { @@ -151,7 +163,7 @@ this.c = this.$c[0].getContext("2d"); this.$ - .wrap($('
')) .before(this.$c); @@ -434,8 +446,11 @@ a += this.PI2; } - ret = ~~ (0.5 + (a * (this.o.max - this.o.min) / this.angleArc)) - + this.o.min; + ret = (a * (this.o.max - this.o.min) / this.angleArc); + if (!this.o.allowFractions) { + ret = ~~(0.5 + ret); + } + ret += this.o.min; this.o.stopper && (ret = max(min(ret, this.o.max), this.o.min)); @@ -543,10 +558,10 @@ this.radius = this.xy - this.lineWidth / 2; this.o.angleOffset - && (this.o.angleOffset = isNaN(this.o.angleOffset) ? 0 : this.o.angleOffset); + || (this.o.angleOffset = isNaN(this.o.angleOffset) ? 0 : this.o.angleOffset); this.o.angleArc - && (this.o.angleArc = isNaN(this.o.angleArc) ? this.PI2 : this.o.angleArc); + || (this.o.angleArc = isNaN(this.o.angleArc) ? 360 : this.o.angleArc); // deg to rad this.angleOffset = this.o.angleOffset * Math.PI / 180; @@ -642,9 +657,9 @@ return this.each( function () { var d = new k.Dial(); - d.o = o; d.$ = $(this); - d.run(); + + d.run(o); } ).parent(); };