diff --git a/js/jquery.knob.js b/js/jquery.knob.js index 88a4d47..9eb68fa 100755 --- a/js/jquery.knob.js +++ b/js/jquery.knob.js @@ -12,7 +12,16 @@ * * Thanks to vor, eskimoblood, spiffistan, FabrizioC */ -(function($) { + +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as anonymous module. + define(['jquery'], factory); + } else { + // Browser globals. + factory(jQuery); + } +}(function ($) { /** * Kontrol library @@ -48,13 +57,11 @@ this.o = null; // array of options this.$ = null; // jQuery wrapped element this.i = null; // mixed HTMLInputElement or array of HTMLInputElement - this.g = null; // deprecated 2D graphics context for 'pre-rendering' + this.g = null; // 2D graphics context for 'pre-rendering' this.v = null; // value ; mixed array or integer this.cv = null; // change value ; not commited value this.x = 0; // canvas x position this.y = 0; // canvas y position - this.w = 0; // canvas width - this.h = 0; // canvas height this.$c = null; // jQuery canvas element this.c = null; // rendered canvas context this.t = 0; // touches index @@ -65,11 +72,6 @@ this.cH = null; // change hook this.eH = null; // cancel hook this.rH = null; // release hook - this.scale = 1; // scale factor - this.relative = false; - this.relativeWidth = false; - this.relativeHeight = false; - this.$div = null; // component div this.run = function () { var cf = function (e, conf) { @@ -92,7 +94,7 @@ min : this.$.data('min') || 0, max : this.$.data('max') || 100, stopper : true, - readOnly : this.$.data('readonly') || (this.$.attr('readonly') == 'readonly'), + readOnly : this.$.data('readonly'), // UI cursor : (this.$.data('cursor') === true && 30) @@ -106,17 +108,13 @@ displayPrevious : this.$.data('displayprevious'), fgColor : this.$.data('fgcolor') || '#87CEEB', inputColor: this.$.data('inputcolor') || this.$.data('fgcolor') || '#87CEEB', - font: this.$.data('font') || 'Arial', - fontWeight: this.$.data('font-weight') || 'bold', inline : false, - step : this.$.data('step') || 1, // Hooks draw : null, // function () {} change : null, // function (value) {} cancel : null, // function () {} - release : null, // function (value) {} - error : null // function () {} + release : null // function (value) {} }, this.o ); @@ -143,7 +141,6 @@ this.$.find('legend').remove(); } else { - // input = integer this.i = this.$; this.v = this.$.val(); @@ -152,55 +149,24 @@ this.$.bind( 'change' , function () { - s.val(s._validate(s.$.val())); + s.val(s.$.val()); } ); - } (!this.o.displayInput) && this.$.hide(); - // adds needed DOM elements (canvas, div) - this.$c = $(document.createElement('canvas')); - if (typeof G_vmlCanvasManager !== 'undefined') { - G_vmlCanvasManager.initElement(this.$c[0]); - } - this.c = this.$c[0].getContext ? this.$c[0].getContext('2d') : null; - if (!this.c) { - this.o.error && this.o.error(); - return; - } - - // hdpi support - this.scale = (window.devicePixelRatio || 1) / - ( - this.c.webkitBackingStorePixelRatio || - this.c.mozBackingStorePixelRatio || - this.c.msBackingStorePixelRatio || - this.c.oBackingStorePixelRatio || - this.c.backingStorePixelRatio || 1 - ); - - // detects relative width / height - this.relativeWidth = ((this.o.width % 1 !== 0) - && this.o.width.indexOf('%')); - this.relativeHeight = ((this.o.height % 1 !== 0) - && this.o.height.indexOf('%')); + this.$c = $(''); + this.c = this.$c[0].getContext("2d"); - this.relative = (this.relativeWidth || this.relativeHeight); - - // wraps all elements in a div - this.$div = $('
'); - - this.$.wrap(this.$div).before(this.$c); - this.$div = this.$.parent(); - - // computes size and carves the component - this._carve(); + this.$ + .wrap($('')) + .before(this.$c); - // prepares props for transaction if (this.v instanceof Object) { this.cv = {}; this.copy(this.v, this.cv); @@ -208,13 +174,11 @@ this.cv = this.v; } - // binds configure event this.$ .bind("configure", cf) .parent() .bind("configure", cf); - // finalize init this._listen() ._configure() ._xy() @@ -222,59 +186,20 @@ this.isInit = true; - // the most important ! this._draw(); return this; }; - this._carve = function() { - if(this.relative) { - var w = this.relativeWidth - ? this.$div.parent().width() - * parseInt(this.o.width) / 100 - : this.$div.parent().width(), - h = this.relativeHeight - ? this.$div.parent().height() - * parseInt(this.o.height) / 100 - : this.$div.parent().height(); - - // apply relative - this.w = this.h = Math.min(w, h); - } else { - this.w = this.o.width; - this.h = this.o.height; - } - - // finalize div - this.$div.css({ - 'width': this.w + 'px', - 'height': this.h + 'px' - }); - - // finalize canvas with computed width - this.$c.attr({ - width: this.w, - height: this.h - }); - - // scaling - if (this.scale !== 1) { - this.$c[0].width = this.$c[0].width * this.scale; - this.$c[0].height = this.$c[0].height * this.scale; - this.$c.width(this.w); - this.$c.height(this.h); - } - - return this; - } - this._draw = function () { // canvas pre-rendering - var d = true; + var d = true, + c = document.createElement('canvas'); - s.g = s.c; + c.width = s.o.width; + c.height = s.o.height; + s.g = c.getContext('2d'); s.clear(); @@ -283,6 +208,8 @@ (d !== false) && s.draw(); + s.c.drawImage(c, 0, 0); + c = null; }; this._touch = function (e) { @@ -302,7 +229,7 @@ ) return; - s.change(s._validate(v)); + s.change(v); s._draw(); }; @@ -343,7 +270,7 @@ && (s.cH(v) === false) ) return; - s.change(s._validate(v)); + s.change(v); s._draw(); }; @@ -411,15 +338,6 @@ s._xy()._touch(e); } ); - - if(this.relative) { - $(window).resize(function() { - s._carve() - .init(); - s._draw(); - }); - } - this.listen(); } else { this.$.attr('readonly', 'readonly'); @@ -450,10 +368,6 @@ this.$c[0].width = this.$c[0].width; }; - this._validate = function(v) { - return (~~ (((v < 0) ? -0.5 : 0.5) + (v/this.o.step))) * this.o.step; - }; - // Abstract methods this.listen = function () {}; // on start, one time this.extend = function () {}; // each time configure triggered @@ -545,10 +459,11 @@ var s = this, mw = function (e) { e.preventDefault(); + var ori = e.originalEvent ,deltaX = ori.detail || ori.wheelDeltaX ,deltaY = ori.detail || ori.wheelDeltaY - ,v = parseInt(s.$.val()) + (deltaX>0 || deltaY>0 ? s.o.step : deltaX<0 || deltaY<0 ? -s.o.step : 0); + ,v = parseInt(s.$.val()) + (deltaX>0 || deltaY>0 ? 1 : deltaX<0 || deltaY<0 ? -1 : 0); if ( s.cH @@ -557,7 +472,7 @@ s.val(v); } - , kval, to, m = 1, kv = {37:-s.o.step, 38:s.o.step, 39:s.o.step, 40:-s.o.step}; + , kval, to, m = 1, kv = {37:-1, 38:1, 39:1, 40:-1}; this.$ .bind( @@ -632,9 +547,9 @@ ) this.v = this.o.min; this.$.val(this.v); - this.w2 = this.w / 2; + this.w2 = this.o.width / 2; this.cursorExt = this.o.cursor / 100; - this.xy = this.w2 * this.scale; + this.xy = this.w2; this.lineWidth = this.xy * this.o.thickness; this.lineCap = this.o.lineCap; this.radius = this.xy - this.lineWidth / 2; @@ -661,15 +576,15 @@ this.o.displayInput && this.i.css({ - 'width' : ((this.w / 2 + 4) >> 0) + 'px' - ,'height' : ((this.w / 3) >> 0) + 'px' + 'width' : ((this.o.width / 2 + 4) >> 0) + 'px' + ,'height' : ((this.o.width / 3) >> 0) + 'px' ,'position' : 'absolute' ,'vertical-align' : 'middle' - ,'margin-top' : ((this.w / 3) >> 0) + 'px' - ,'margin-left' : '-' + ((this.w * 3 / 4 + 2) >> 0) + 'px' + ,'margin-top' : ((this.o.width / 3) >> 0) + 'px' + ,'margin-left' : '-' + ((this.o.width * 3 / 4 + 2) >> 0) + 'px' ,'border' : 0 ,'background' : 'none' - ,'font' : this.o.fontWeight + ' ' + ((this.w / s) >> 0) + 'px ' + this.o.font + ,'font' : 'bold ' + ((this.o.width / s) >> 0) + 'px Arial' ,'text-align' : 'center' ,'color' : this.o.inputColor || this.o.fgColor ,'padding' : '0px' @@ -748,4 +663,7 @@ ).parent(); }; -})(jQuery); +//})(jQuery); + +})); + diff --git a/knob.jquery.json b/knob.jquery.json index 28f295d..b002997 100755 --- a/knob.jquery.json +++ b/knob.jquery.json @@ -9,10 +9,10 @@ "ui", "input" ], - "version": "1.2.2", + "version": "1.2.0", "author": { "name": "Anthony Terrien", - "url": "https://github.com/aterrien" + "url": "https://github.com/romansk" }, "maintainers": [ { @@ -28,9 +28,9 @@ } ], "bugs": "https://github.com/aterrien/jQuery-Knob/issues", - "homepage": "https://github.com/aterrien/jQuery-Knob", + "homepage": "https://github.com/romansk/jQuery-Knob", "docs": "https://github.com/aterrien/jQuery-Knob", - "download": "https://github.com/aterrien/jQuery-Knob/tags", + "download": "https://github.com/romansk/jQuery-Knob/tags", "dependencies": { "jquery": ">=1.7.0" }