├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── dist └── jquery.knob.min.js ├── excanvas.js ├── index.html ├── js └── jquery.knob.js ├── knob.jquery.json ├── package.json └── secretplan.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | nbproject/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Anthony Terrien 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jQuery Knob 2 | ============= 3 | 4 | - canvas based ; no png or jpg sprites. 5 | - touch, mouse and mousewheel, keyboard events implemented. 6 | - downward compatible ; overloads an input element. 7 | 8 | Example 9 | ------- 10 | 11 | ```html 12 | 13 | 14 | 19 | ``` 20 | 21 | Options 22 | ------- 23 | 24 | Options are provided as attributes 'data-option': 25 | 26 | ```html 27 | 28 | ``` 29 | 30 | ... or in the "knob()" call : 31 | 32 | ```javascript 33 | $(".dial").knob({ 34 | 'min':-50, 35 | 'max':50 36 | }); 37 | ``` 38 | 39 | The following options are supported : 40 | 41 | Behaviors : 42 | * min : min value | default=0. 43 | * max : max value | default=100. 44 | * step : step size | default=1. 45 | * angleOffset : starting angle in degrees | default=0. 46 | * angleArc : arc size in degrees | default=360. 47 | * stopper : stop at min & max on keydown/mousewheel | default=true. 48 | * readOnly : disable input and events | default=false. 49 | * rotation : direction of progression | default=clockwise. 50 | 51 | UI : 52 | * cursor : display mode "cursor", cursor size could be changed passing a numeric value to the option, default width is used when passing boolean value "true" | default=gauge. 53 | * thickness : gauge thickness. 54 | * lineCap : gauge stroke endings. | default=butt, round=rounded line endings 55 | * width : dial width. 56 | * height : dial height. 57 | * displayInput : default=true | false=hide input. 58 | * displayPrevious : default=false | true=displays the previous value with transparency. 59 | * fgColor : foreground color. 60 | * inputColor : input value (number) color. 61 | * font : font family. 62 | * fontWeight : font weight. 63 | * bgColor : background color. 64 | 65 | Hooks 66 | ------- 67 | 68 | ```html 69 | 74 | ``` 75 | 76 | * 'release' : executed on release 77 | 78 | Parameters : 79 | + value : int, current value 80 | 81 | * 'change' : executed at each change of the value 82 | 83 | Parameters : 84 | + value : int, current value 85 | 86 | * 'draw' : when drawing the canvas 87 | 88 | Context : 89 | - this.g : canvas context 2D (see Canvas documentation) 90 | - this.$ : jQuery wrapped element 91 | - this.o : options 92 | - this.i : input 93 | - ... console.log(this); 94 | 95 | * 'cancel' : triggered on [esc] keydown 96 | 97 | * 'format' : allows to format output (add unit %, ms ...) 98 | 99 | The scope (this) of each hook function is the current Knob instance (refer to the demo code). 100 | 101 | Example 102 | ------- 103 | 104 | ```html 105 | 106 | 107 | 112 | ``` 113 | 114 | Dynamically configure 115 | ------- 116 | 117 | ```html 118 | 130 | ``` 131 | 132 | Set the value 133 | ------- 134 | 135 | ```html 136 | 141 | ``` 142 | 143 | Supported browser 144 | ------- 145 | 146 | Tested on Chrome, Safari, Firefox, IE>=8.0 (IE8.0 with excanvas). 147 | 148 | ![secretplan](https://raw.github.com/aterrien/jQuery-Knob/master/secretplan.jpg) 149 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-knob", 3 | "version": "1.2.13", 4 | "main": "js/jquery.knob.js", 5 | "description": "Nice, downward compatible, touchable, jQuery dial.", 6 | "license": "MIT", 7 | "ignore": [], 8 | "dependencies": { 9 | "jquery": ">=1.7.0" 10 | }, 11 | "devDependencies": {} 12 | } 13 | 14 | -------------------------------------------------------------------------------- /dist/jquery.knob.min.js: -------------------------------------------------------------------------------- 1 | (function(e){if(typeof define==="function"&&define.amd){define(["jquery"],e)}else{e(jQuery)}})(function(e){"use strict";var t={},n=Math.max,r=Math.min;t.c={};t.c.d=e(document);t.c.t=function(e){return e.originalEvent.touches.length-1};t.o=function(){var n=this;this.o=null;this.$=null;this.i=null;this.g=null;this.v=null;this.cv=null;this.x=0;this.y=0;this.w=0;this.h=0;this.$c=null;this.c=null;this.t=0;this.isInit=false;this.fgColor=null;this.pColor=null;this.dH=null;this.cH=null;this.eH=null;this.rH=null;this.scale=1;this.relative=false;this.relativeWidth=false;this.relativeHeight=false;this.$div=null;this.run=function(){var t=function(e,t){var r;for(r in t){n.o[r]=t[r]}n._carve().init();n._configure()._draw()};if(this.$.data("kontroled"))return;this.$.data("kontroled",true);this.extend();this.o=e.extend({min:this.$.data("min")!==undefined?this.$.data("min"):0,max:this.$.data("max")!==undefined?this.$.data("max"):100,stopper:true,readOnly:this.$.data("readonly")||this.$.attr("readonly")==="readonly",cursor:this.$.data("cursor")===true&&30||this.$.data("cursor")||0,thickness:this.$.data("thickness")&&Math.max(Math.min(this.$.data("thickness"),1),.01)||.35,lineCap:this.$.data("linecap")||"butt",width:this.$.data("width")||200,height:this.$.data("height")||200,displayInput:this.$.data("displayinput")==null||this.$.data("displayinput"),displayPrevious:this.$.data("displayprevious"),fgColor:this.$.data("fgcolor")||"#87CEEB",inputColor:this.$.data("inputcolor"),font:this.$.data("font")||"Arial",fontWeight:this.$.data("font-weight")||"bold",inline:false,step:this.$.data("step")||1,rotation:this.$.data("rotation"),draw:null,change:null,cancel:null,release:null,format:function(e){return e},parse:function(e){return parseFloat(e)}},this.o);this.o.flip=this.o.rotation==="anticlockwise"||this.o.rotation==="acw";if(!this.o.inputColor){this.o.inputColor=this.o.fgColor}if(this.$.is("fieldset")){this.v={};this.i=this.$.find("input");this.i.each(function(t){var r=e(this);n.i[t]=r;n.v[t]=n.o.parse(r.val());r.bind("change blur",function(){var e={};e[t]=r.val();n.val(n._validate(e))})});this.$.find("legend").remove()}else{this.i=this.$;this.v=this.o.parse(this.$.val());this.v===""&&(this.v=this.o.min);this.$.bind("change blur",function(){n.val(n._validate(n.o.parse(n.$.val())))})}!this.o.displayInput&&this.$.hide();this.$c=e(document.createElement("canvas")).attr({width:this.o.width,height:this.o.height});this.$div=e('
');this.$.wrap(this.$div).before(this.$c);this.$div=this.$.parent();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){throw{name:"CanvasNotSupportedException",message:"Canvas not supported. Please use excanvas on IE8.0.",toString:function(){return this.name+": "+this.message}}}this.scale=(window.devicePixelRatio||1)/(this.c.webkitBackingStorePixelRatio||this.c.mozBackingStorePixelRatio||this.c.msBackingStorePixelRatio||this.c.oBackingStorePixelRatio||this.c.backingStorePixelRatio||1);this.relativeWidth=this.o.width%1!==0&&this.o.width.indexOf("%");this.relativeHeight=this.o.height%1!==0&&this.o.height.indexOf("%");this.relative=this.relativeWidth||this.relativeHeight;this._carve();if(this.v instanceof Object){this.cv={};this.copy(this.v,this.cv)}else{this.cv=this.v}this.$.bind("configure",t).parent().bind("configure",t);this._listen()._configure()._xy().init();this.isInit=true;this.$.val(this.o.format(this.v));this._draw();return this};this._carve=function(){if(this.relative){var e=this.relativeWidth?this.$div.parent().width()*parseInt(this.o.width)/100:this.$div.parent().width(),t=this.relativeHeight?this.$div.parent().height()*parseInt(this.o.height)/100:this.$div.parent().height();this.w=this.h=Math.min(e,t)}else{this.w=this.o.width;this.h=this.o.height}this.$div.css({width:this.w+"px",height:this.h+"px"});this.$c.attr({width:this.w,height:this.h});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(){var e=true;n.g=n.c;n.clear();n.dH&&(e=n.dH());e!==false&&n.draw()};this._touch=function(e){var r=function(e){var t=n.xy2val(e.originalEvent.touches[n.t].pageX,e.originalEvent.touches[n.t].pageY);if(t==n.cv)return;if(n.cH&&n.cH(t)===false)return;n.change(n._validate(t));n._draw()};this.t=t.c.t(e);r(e);t.c.d.bind("touchmove.k",r).bind("touchend.k",function(){t.c.d.unbind("touchmove.k touchend.k");n.val(n.cv)});return this};this._mouse=function(e){var r=function(e){var t=n.xy2val(e.pageX,e.pageY);if(t==n.cv)return;if(n.cH&&n.cH(t)===false)return;n.change(n._validate(t));n._draw()};r(e);t.c.d.bind("mousemove.k",r).bind("keyup.k",function(e){if(e.keyCode===27){t.c.d.unbind("mouseup.k mousemove.k keyup.k");if(n.eH&&n.eH()===false)return;n.cancel()}}).bind("mouseup.k",function(e){t.c.d.unbind("mousemove.k mouseup.k keyup.k");n.val(n.cv)});return this};this._xy=function(){var e=this.$c.offset();this.x=e.left;this.y=e.top;return this};this._listen=function(){if(!this.o.readOnly){this.$c.bind("mousedown",function(e){e.preventDefault();n._xy()._mouse(e)}).bind("touchstart",function(e){e.preventDefault();n._xy()._touch(e)});this.listen()}else{this.$.attr("readonly","readonly")}if(this.relative){e(window).resize(function(){n._carve().init();n._draw()})}return this};this._configure=function(){if(this.o.draw)this.dH=this.o.draw;if(this.o.change)this.cH=this.o.change;if(this.o.cancel)this.eH=this.o.cancel;if(this.o.release)this.rH=this.o.release;if(this.o.displayPrevious){this.pColor=this.h2rgba(this.o.fgColor,"0.4");this.fgColor=this.h2rgba(this.o.fgColor,"0.6")}else{this.fgColor=this.o.fgColor}return this};this._clear=function(){this.$c[0].width=this.$c[0].width};this._validate=function(e){var t=~~((e<0?-.5:.5)+e/this.o.step)*this.o.step;return Math.round(t*100)/100};this.listen=function(){};this.extend=function(){};this.init=function(){};this.change=function(e){};this.val=function(e){};this.xy2val=function(e,t){};this.draw=function(){};this.clear=function(){this._clear()};this.h2rgba=function(e,t){var n;e=e.substring(1,7);n=[parseInt(e.substring(0,2),16),parseInt(e.substring(2,4),16),parseInt(e.substring(4,6),16)];return"rgba("+n[0]+","+n[1]+","+n[2]+","+t+")"};this.copy=function(e,t){for(var n in e){t[n]=e[n]}}};t.Dial=function(){t.o.call(this);this.startAngle=null;this.xy=null;this.radius=null;this.lineWidth=null;this.cursorExt=null;this.w2=null;this.PI2=2*Math.PI;this.extend=function(){this.o=e.extend({bgColor:this.$.data("bgcolor")||"#EEEEEE",angleOffset:this.$.data("angleoffset")||0,angleArc:this.$.data("anglearc")||360,inline:true},this.o)};this.val=function(e,t){if(null!=e){e=this.o.parse(e);if(t!==false&&e!=this.v&&this.rH&&this.rH(e)===false){return}this.cv=this.o.stopper?n(r(e,this.o.max),this.o.min):e;this.v=this.cv;this.$.val(this.o.format(this.v));this._draw()}else{return this.v}};this.xy2val=function(e,t){var i,s;i=Math.atan2(e-(this.x+this.w2),-(t-this.y-this.w2))-this.angleOffset;if(this.o.flip){i=this.angleArc-i-this.PI2}if(this.angleArc!=this.PI2&&i<0&&i>-.5){i=0}else if(i<0){i+=this.PI2}s=i*(this.o.max-this.o.min)/this.angleArc+this.o.min;this.o.stopper&&(s=n(r(s,this.o.max),this.o.min));return s};this.listen=function(){var t=this,i,s,o=function(e){e.preventDefault();var o=e.originalEvent,u=o.detail||o.wheelDeltaX,a=o.detail||o.wheelDeltaY,f=t._validate(t.o.parse(t.$.val()))+(u>0||a>0?t.o.step:u<0||a<0?-t.o.step:0);f=n(r(f,t.o.max),t.o.min);t.val(f,false);if(t.rH){clearTimeout(i);i=setTimeout(function(){t.rH(f);i=null},100);if(!s){s=setTimeout(function(){if(i)t.rH(f);s=null},200)}}},u,a,f=1,l={37:-t.o.step,38:t.o.step,39:t.o.step,40:-t.o.step};this.$.bind("keydown",function(i){var s=i.keyCode;if(s>=96&&s<=105){s=i.keyCode=s-48}u=parseInt(String.fromCharCode(s));if(isNaN(u)){s!==13&&s!==8&&s!==9&&s!==189&&(s!==190||t.$.val().match(/\./))&&i.preventDefault();if(e.inArray(s,[37,38,39,40])>-1){i.preventDefault();var o=t.o.parse(t.$.val())+l[s]*f;t.o.stopper&&(o=n(r(o,t.o.max),t.o.min));t.change(t._validate(o));t._draw();a=window.setTimeout(function(){f*=2},30)}}}).bind("keyup",function(e){if(isNaN(u)){if(a){window.clearTimeout(a);a=null;f=1;t.val(t.$.val())}}else{t.$.val()>t.o.max&&t.$.val(t.o.max)||t.$.val()this.o.max){this.v=this.o.min}this.$.val(this.v);this.w2=this.w/2;this.cursorExt=this.o.cursor/100;this.xy=this.w2*this.scale;this.lineWidth=this.xy*this.o.thickness;this.lineCap=this.o.lineCap;this.radius=this.xy-this.lineWidth/2;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.angleOffset=this.o.angleOffset*Math.PI/180;this.angleArc=this.o.angleArc*Math.PI/180;this.startAngle=1.5*Math.PI+this.angleOffset;this.endAngle=1.5*Math.PI+this.angleOffset+this.angleArc;var e=n(String(Math.abs(this.o.max)).length,String(Math.abs(this.o.min)).length,2)+2;this.o.displayInput&&this.i.css({width:(this.w/2+4>>0)+"px",height:(this.w/3>>0)+"px",position:"absolute","vertical-align":"middle","margin-top":(this.w/3>>0)+"px","margin-left":"-"+(this.w*3/4+2>>0)+"px",border:0,background:"none",font:this.o.fontWeight+" "+(this.w/e>>0)+"px "+this.o.font,"text-align":"center",color:this.o.inputColor||this.o.fgColor,padding:"0px","-webkit-appearance":"none"})||this.i.css({width:"0px",visibility:"hidden"})};this.change=function(e){this.cv=e;this.$.val(this.o.format(e))};this.angle=function(e){return(e-this.o.min)*this.angleArc/(this.o.max-this.o.min)};this.arc=function(e){var t,n;e=this.angle(e);if(this.o.flip){t=this.endAngle+1e-5;n=t-e-1e-5}else{t=this.startAngle-1e-5;n=t+e+1e-5}this.o.cursor&&(t=n-this.cursorExt)&&(n=n+this.cursorExt);return{s:t,e:n,d:this.o.flip&&!this.o.cursor}};this.draw=function(){var e=this.g,t=this.arc(this.cv),n,r=1;e.lineWidth=this.lineWidth;e.lineCap=this.lineCap;if(this.o.bgColor!=="none"){e.beginPath();e.strokeStyle=this.o.bgColor;e.arc(this.xy,this.xy,this.radius,this.endAngle-1e-5,this.startAngle+1e-5,true);e.stroke()}if(this.o.displayPrevious){n=this.arc(this.v);e.beginPath();e.strokeStyle=this.pColor;e.arc(this.xy,this.xy,this.radius,n.s,n.e,n.d);e.stroke();r=this.cv==this.v}e.beginPath();e.strokeStyle=r?this.o.fgColor:this.fgColor;e.arc(this.xy,this.xy,this.radius,t.s,t.e,t.d);e.stroke()};this.cancel=function(){this.val(this.v)}};e.fn.dial=e.fn.knob=function(n){return this.each(function(){var r=new t.Dial;r.o=n;r.$=e(this);r.run()}).parent()}}) -------------------------------------------------------------------------------- /excanvas.js: -------------------------------------------------------------------------------- 1 | // Copyright 2006 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | 16 | // Known Issues: 17 | // 18 | // * Patterns are not implemented. 19 | // * Radial gradient are not implemented. The VML version of these look very 20 | // different from the canvas one. 21 | // * Clipping paths are not implemented. 22 | // * Coordsize. The width and height attribute have higher priority than the 23 | // width and height style values which isn't correct. 24 | // * Painting mode isn't implemented. 25 | // * Canvas width/height should is using content-box by default. IE in 26 | // Quirks mode will draw the canvas using border-box. Either change your 27 | // doctype to HTML5 28 | // (http://www.whatwg.org/specs/web-apps/current-work/#the-doctype) 29 | // or use Box Sizing Behavior from WebFX 30 | // (http://webfx.eae.net/dhtml/boxsizing/boxsizing.html) 31 | // * Non uniform scaling does not correctly scale strokes. 32 | // * Optimize. There is always room for speed improvements. 33 | 34 | // Only add this code if we do not already have a canvas implementation 35 | if (!document.createElement('canvas').getContext) { 36 | 37 | (function() { 38 | 39 | // alias some functions to make (compiled) code shorter 40 | var m = Math; 41 | var mr = m.round; 42 | var ms = m.sin; 43 | var mc = m.cos; 44 | var abs = m.abs; 45 | var sqrt = m.sqrt; 46 | 47 | // this is used for sub pixel precision 48 | var Z = 10; 49 | var Z2 = Z / 2; 50 | 51 | /** 52 | * This funtion is assigned to the elements as element.getContext(). 53 | * @this {HTMLElement} 54 | * @return {CanvasRenderingContext2D_} 55 | */ 56 | function getContext() { 57 | return this.context_ || 58 | (this.context_ = new CanvasRenderingContext2D_(this)); 59 | } 60 | 61 | var slice = Array.prototype.slice; 62 | 63 | /** 64 | * Binds a function to an object. The returned function will always use the 65 | * passed in {@code obj} as {@code this}. 66 | * 67 | * Example: 68 | * 69 | * g = bind(f, obj, a, b) 70 | * g(c, d) // will do f.call(obj, a, b, c, d) 71 | * 72 | * @param {Function} f The function to bind the object to 73 | * @param {Object} obj The object that should act as this when the function 74 | * is called 75 | * @param {*} var_args Rest arguments that will be used as the initial 76 | * arguments when the function is called 77 | * @return {Function} A new function that has bound this 78 | */ 79 | function bind(f, obj, var_args) { 80 | var a = slice.call(arguments, 2); 81 | return function() { 82 | return f.apply(obj, a.concat(slice.call(arguments))); 83 | }; 84 | } 85 | 86 | var G_vmlCanvasManager_ = { 87 | init: function(opt_doc) { 88 | if (/MSIE/.test(navigator.userAgent) && !window.opera) { 89 | var doc = opt_doc || document; 90 | // Create a dummy element so that IE will allow canvas elements to be 91 | // recognized. 92 | doc.createElement('canvas'); 93 | doc.attachEvent('onreadystatechange', bind(this.init_, this, doc)); 94 | } 95 | }, 96 | 97 | init_: function(doc) { 98 | // create xmlns 99 | if (!doc.namespaces['g_vml_']) { 100 | doc.namespaces.add('g_vml_', 'urn:schemas-microsoft-com:vml', 101 | '#default#VML'); 102 | 103 | } 104 | if (!doc.namespaces['g_o_']) { 105 | doc.namespaces.add('g_o_', 'urn:schemas-microsoft-com:office:office', 106 | '#default#VML'); 107 | } 108 | 109 | // Setup default CSS. Only add one style sheet per document 110 | if (!doc.styleSheets['ex_canvas_']) { 111 | var ss = doc.createStyleSheet(); 112 | ss.owningElement.id = 'ex_canvas_'; 113 | ss.cssText = 'canvas{display:inline-block;overflow:hidden;' + 114 | // default size is 300x150 in Gecko and Opera 115 | 'text-align:left;width:300px;height:150px}' + 116 | 'g_vml_\\:*{behavior:url(#default#VML)}' + 117 | 'g_o_\\:*{behavior:url(#default#VML)}'; 118 | 119 | } 120 | 121 | // find all canvas elements 122 | var els = doc.getElementsByTagName('canvas'); 123 | for (var i = 0; i < els.length; i++) { 124 | this.initElement(els[i]); 125 | } 126 | }, 127 | 128 | /** 129 | * Public initializes a canvas element so that it can be used as canvas 130 | * element from now on. This is called automatically before the page is 131 | * loaded but if you are creating elements using createElement you need to 132 | * make sure this is called on the element. 133 | * @param {HTMLElement} el The canvas element to initialize. 134 | * @return {HTMLElement} the element that was created. 135 | */ 136 | initElement: function(el) { 137 | if (!el.getContext) { 138 | 139 | el.getContext = getContext; 140 | 141 | // Remove fallback content. There is no way to hide text nodes so we 142 | // just remove all childNodes. We could hide all elements and remove 143 | // text nodes but who really cares about the fallback content. 144 | el.innerHTML = ''; 145 | 146 | // do not use inline function because that will leak memory 147 | el.attachEvent('onpropertychange', onPropertyChange); 148 | el.attachEvent('onresize', onResize); 149 | 150 | var attrs = el.attributes; 151 | if (attrs.width && attrs.width.specified) { 152 | // TODO: use runtimeStyle and coordsize 153 | // el.getContext().setWidth_(attrs.width.nodeValue); 154 | el.style.width = attrs.width.nodeValue + 'px'; 155 | } else { 156 | el.width = el.clientWidth; 157 | } 158 | if (attrs.height && attrs.height.specified) { 159 | // TODO: use runtimeStyle and coordsize 160 | // el.getContext().setHeight_(attrs.height.nodeValue); 161 | el.style.height = attrs.height.nodeValue + 'px'; 162 | } else { 163 | el.height = el.clientHeight; 164 | } 165 | //el.getContext().setCoordsize_() 166 | } 167 | return el; 168 | } 169 | }; 170 | 171 | function onPropertyChange(e) { 172 | var el = e.srcElement; 173 | 174 | switch (e.propertyName) { 175 | case 'width': 176 | el.style.width = el.attributes.width.nodeValue + 'px'; 177 | el.getContext().clearRect(); 178 | break; 179 | case 'height': 180 | el.style.height = el.attributes.height.nodeValue + 'px'; 181 | el.getContext().clearRect(); 182 | break; 183 | } 184 | } 185 | 186 | function onResize(e) { 187 | var el = e.srcElement; 188 | if (el.firstChild) { 189 | el.firstChild.style.width = el.clientWidth + 'px'; 190 | el.firstChild.style.height = el.clientHeight + 'px'; 191 | } 192 | } 193 | 194 | G_vmlCanvasManager_.init(); 195 | 196 | // precompute "00" to "FF" 197 | var dec2hex = []; 198 | for (var i = 0; i < 16; i++) { 199 | for (var j = 0; j < 16; j++) { 200 | dec2hex[i * 16 + j] = i.toString(16) + j.toString(16); 201 | } 202 | } 203 | 204 | function createMatrixIdentity() { 205 | return [ 206 | [1, 0, 0], 207 | [0, 1, 0], 208 | [0, 0, 1] 209 | ]; 210 | } 211 | 212 | function matrixMultiply(m1, m2) { 213 | var result = createMatrixIdentity(); 214 | 215 | for (var x = 0; x < 3; x++) { 216 | for (var y = 0; y < 3; y++) { 217 | var sum = 0; 218 | 219 | for (var z = 0; z < 3; z++) { 220 | sum += m1[x][z] * m2[z][y]; 221 | } 222 | 223 | result[x][y] = sum; 224 | } 225 | } 226 | return result; 227 | } 228 | 229 | function copyState(o1, o2) { 230 | o2.fillStyle = o1.fillStyle; 231 | o2.lineCap = o1.lineCap; 232 | o2.lineJoin = o1.lineJoin; 233 | o2.lineWidth = o1.lineWidth; 234 | o2.miterLimit = o1.miterLimit; 235 | o2.shadowBlur = o1.shadowBlur; 236 | o2.shadowColor = o1.shadowColor; 237 | o2.shadowOffsetX = o1.shadowOffsetX; 238 | o2.shadowOffsetY = o1.shadowOffsetY; 239 | o2.strokeStyle = o1.strokeStyle; 240 | o2.globalAlpha = o1.globalAlpha; 241 | o2.arcScaleX_ = o1.arcScaleX_; 242 | o2.arcScaleY_ = o1.arcScaleY_; 243 | o2.lineScale_ = o1.lineScale_; 244 | } 245 | 246 | function processStyle(styleString) { 247 | var str, alpha = 1; 248 | 249 | styleString = String(styleString); 250 | if (styleString.substring(0, 3) == 'rgb') { 251 | var start = styleString.indexOf('(', 3); 252 | var end = styleString.indexOf(')', start + 1); 253 | var guts = styleString.substring(start + 1, end).split(','); 254 | 255 | str = '#'; 256 | for (var i = 0; i < 3; i++) { 257 | str += dec2hex[Number(guts[i])]; 258 | } 259 | 260 | if (guts.length == 4 && styleString.substr(3, 1) == 'a') { 261 | alpha = guts[3]; 262 | } 263 | } else { 264 | str = styleString; 265 | } 266 | 267 | return {color: str, alpha: alpha}; 268 | } 269 | 270 | function processLineCap(lineCap) { 271 | switch (lineCap) { 272 | case 'butt': 273 | return 'flat'; 274 | case 'round': 275 | return 'round'; 276 | case 'square': 277 | default: 278 | return 'square'; 279 | } 280 | } 281 | 282 | /** 283 | * This class implements CanvasRenderingContext2D interface as described by 284 | * the WHATWG. 285 | * @param {HTMLElement} surfaceElement The element that the 2D context should 286 | * be associated with 287 | */ 288 | function CanvasRenderingContext2D_(surfaceElement) { 289 | this.m_ = createMatrixIdentity(); 290 | 291 | this.mStack_ = []; 292 | this.aStack_ = []; 293 | this.currentPath_ = []; 294 | 295 | // Canvas context properties 296 | this.strokeStyle = '#000'; 297 | this.fillStyle = '#000'; 298 | 299 | this.lineWidth = 1; 300 | this.lineJoin = 'miter'; 301 | this.lineCap = 'butt'; 302 | this.miterLimit = Z * 1; 303 | this.globalAlpha = 1; 304 | this.canvas = surfaceElement; 305 | 306 | var el = surfaceElement.ownerDocument.createElement('div'); 307 | el.style.width = surfaceElement.clientWidth + 'px'; 308 | el.style.height = surfaceElement.clientHeight + 'px'; 309 | el.style.overflow = 'hidden'; 310 | el.style.position = 'absolute'; 311 | surfaceElement.appendChild(el); 312 | 313 | this.element_ = el; 314 | this.arcScaleX_ = 1; 315 | this.arcScaleY_ = 1; 316 | this.lineScale_ = 1; 317 | } 318 | 319 | var contextPrototype = CanvasRenderingContext2D_.prototype; 320 | contextPrototype.clearRect = function() { 321 | this.element_.innerHTML = ''; 322 | }; 323 | 324 | contextPrototype.beginPath = function() { 325 | // TODO: Branch current matrix so that save/restore has no effect 326 | // as per safari docs. 327 | this.currentPath_ = []; 328 | }; 329 | 330 | contextPrototype.moveTo = function(aX, aY) { 331 | var p = this.getCoords_(aX, aY); 332 | this.currentPath_.push({type: 'moveTo', x: p.x, y: p.y}); 333 | this.currentX_ = p.x; 334 | this.currentY_ = p.y; 335 | }; 336 | 337 | contextPrototype.lineTo = function(aX, aY) { 338 | var p = this.getCoords_(aX, aY); 339 | this.currentPath_.push({type: 'lineTo', x: p.x, y: p.y}); 340 | 341 | this.currentX_ = p.x; 342 | this.currentY_ = p.y; 343 | }; 344 | 345 | contextPrototype.bezierCurveTo = function(aCP1x, aCP1y, 346 | aCP2x, aCP2y, 347 | aX, aY) { 348 | var p = this.getCoords_(aX, aY); 349 | var cp1 = this.getCoords_(aCP1x, aCP1y); 350 | var cp2 = this.getCoords_(aCP2x, aCP2y); 351 | bezierCurveTo(this, cp1, cp2, p); 352 | }; 353 | 354 | // Helper function that takes the already fixed cordinates. 355 | function bezierCurveTo(self, cp1, cp2, p) { 356 | self.currentPath_.push({ 357 | type: 'bezierCurveTo', 358 | cp1x: cp1.x, 359 | cp1y: cp1.y, 360 | cp2x: cp2.x, 361 | cp2y: cp2.y, 362 | x: p.x, 363 | y: p.y 364 | }); 365 | self.currentX_ = p.x; 366 | self.currentY_ = p.y; 367 | } 368 | 369 | contextPrototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) { 370 | // the following is lifted almost directly from 371 | // http://developer.mozilla.org/en/docs/Canvas_tutorial:Drawing_shapes 372 | 373 | var cp = this.getCoords_(aCPx, aCPy); 374 | var p = this.getCoords_(aX, aY); 375 | 376 | var cp1 = { 377 | x: this.currentX_ + 2.0 / 3.0 * (cp.x - this.currentX_), 378 | y: this.currentY_ + 2.0 / 3.0 * (cp.y - this.currentY_) 379 | }; 380 | var cp2 = { 381 | x: cp1.x + (p.x - this.currentX_) / 3.0, 382 | y: cp1.y + (p.y - this.currentY_) / 3.0 383 | }; 384 | 385 | bezierCurveTo(this, cp1, cp2, p); 386 | }; 387 | 388 | contextPrototype.arc = function(aX, aY, aRadius, 389 | aStartAngle, aEndAngle, aClockwise) { 390 | aRadius *= Z; 391 | var arcType = aClockwise ? 'at' : 'wa'; 392 | 393 | var xStart = aX + mc(aStartAngle) * aRadius - Z2; 394 | var yStart = aY + ms(aStartAngle) * aRadius - Z2; 395 | 396 | var xEnd = aX + mc(aEndAngle) * aRadius - Z2; 397 | var yEnd = aY + ms(aEndAngle) * aRadius - Z2; 398 | 399 | // IE won't render arches drawn counter clockwise if xStart == xEnd. 400 | if (xStart == xEnd && !aClockwise) { 401 | xStart += 0.125; // Offset xStart by 1/80 of a pixel. Use something 402 | // that can be represented in binary 403 | } 404 | 405 | var p = this.getCoords_(aX, aY); 406 | var pStart = this.getCoords_(xStart, yStart); 407 | var pEnd = this.getCoords_(xEnd, yEnd); 408 | 409 | this.currentPath_.push({type: arcType, 410 | x: p.x, 411 | y: p.y, 412 | radius: aRadius, 413 | xStart: pStart.x, 414 | yStart: pStart.y, 415 | xEnd: pEnd.x, 416 | yEnd: pEnd.y}); 417 | 418 | }; 419 | 420 | contextPrototype.rect = function(aX, aY, aWidth, aHeight) { 421 | this.moveTo(aX, aY); 422 | this.lineTo(aX + aWidth, aY); 423 | this.lineTo(aX + aWidth, aY + aHeight); 424 | this.lineTo(aX, aY + aHeight); 425 | this.closePath(); 426 | }; 427 | 428 | contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) { 429 | var oldPath = this.currentPath_; 430 | this.beginPath(); 431 | 432 | this.moveTo(aX, aY); 433 | this.lineTo(aX + aWidth, aY); 434 | this.lineTo(aX + aWidth, aY + aHeight); 435 | this.lineTo(aX, aY + aHeight); 436 | this.closePath(); 437 | this.stroke(); 438 | 439 | this.currentPath_ = oldPath; 440 | }; 441 | 442 | contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) { 443 | var oldPath = this.currentPath_; 444 | this.beginPath(); 445 | 446 | this.moveTo(aX, aY); 447 | this.lineTo(aX + aWidth, aY); 448 | this.lineTo(aX + aWidth, aY + aHeight); 449 | this.lineTo(aX, aY + aHeight); 450 | this.closePath(); 451 | this.fill(); 452 | 453 | this.currentPath_ = oldPath; 454 | }; 455 | 456 | contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) { 457 | var gradient = new CanvasGradient_('gradient'); 458 | gradient.x0_ = aX0; 459 | gradient.y0_ = aY0; 460 | gradient.x1_ = aX1; 461 | gradient.y1_ = aY1; 462 | return gradient; 463 | }; 464 | 465 | contextPrototype.createRadialGradient = function(aX0, aY0, aR0, 466 | aX1, aY1, aR1) { 467 | var gradient = new CanvasGradient_('gradientradial'); 468 | gradient.x0_ = aX0; 469 | gradient.y0_ = aY0; 470 | gradient.r0_ = aR0; 471 | gradient.x1_ = aX1; 472 | gradient.y1_ = aY1; 473 | gradient.r1_ = aR1; 474 | return gradient; 475 | }; 476 | 477 | contextPrototype.drawImage = function(image, var_args) { 478 | var dx, dy, dw, dh, sx, sy, sw, sh; 479 | 480 | // to find the original width we overide the width and height 481 | var oldRuntimeWidth = image.runtimeStyle.width; 482 | var oldRuntimeHeight = image.runtimeStyle.height; 483 | image.runtimeStyle.width = 'auto'; 484 | image.runtimeStyle.height = 'auto'; 485 | 486 | // get the original size 487 | var w = image.width; 488 | var h = image.height; 489 | 490 | // and remove overides 491 | image.runtimeStyle.width = oldRuntimeWidth; 492 | image.runtimeStyle.height = oldRuntimeHeight; 493 | 494 | if (arguments.length == 3) { 495 | dx = arguments[1]; 496 | dy = arguments[2]; 497 | sx = sy = 0; 498 | sw = dw = w; 499 | sh = dh = h; 500 | } else if (arguments.length == 5) { 501 | dx = arguments[1]; 502 | dy = arguments[2]; 503 | dw = arguments[3]; 504 | dh = arguments[4]; 505 | sx = sy = 0; 506 | sw = w; 507 | sh = h; 508 | } else if (arguments.length == 9) { 509 | sx = arguments[1]; 510 | sy = arguments[2]; 511 | sw = arguments[3]; 512 | sh = arguments[4]; 513 | dx = arguments[5]; 514 | dy = arguments[6]; 515 | dw = arguments[7]; 516 | dh = arguments[8]; 517 | } else { 518 | throw Error('Invalid number of arguments'); 519 | } 520 | 521 | var d = this.getCoords_(dx, dy); 522 | 523 | var w2 = sw / 2; 524 | var h2 = sh / 2; 525 | 526 | var vmlStr = []; 527 | 528 | var W = 10; 529 | var H = 10; 530 | 531 | // For some reason that I've now forgotten, using divs didn't work 532 | vmlStr.push(' ' , 571 | '', 579 | ''); 580 | 581 | this.element_.insertAdjacentHTML('BeforeEnd', 582 | vmlStr.join('')); 583 | }; 584 | 585 | contextPrototype.stroke = function(aFill) { 586 | var lineStr = []; 587 | var lineOpen = false; 588 | var a = processStyle(aFill ? this.fillStyle : this.strokeStyle); 589 | var color = a.color; 590 | var opacity = a.alpha * this.globalAlpha; 591 | 592 | var W = 10; 593 | var H = 10; 594 | 595 | lineStr.push(''); 662 | 663 | if (!aFill) { 664 | var lineWidth = this.lineScale_ * this.lineWidth; 665 | 666 | // VML cannot correctly render a line if the width is less than 1px. 667 | // In that case, we dilute the color to make the line look thinner. 668 | if (lineWidth < 1) { 669 | opacity *= lineWidth; 670 | } 671 | 672 | lineStr.push( 673 | '' 680 | ); 681 | } else if (typeof this.fillStyle == 'object') { 682 | var fillStyle = this.fillStyle; 683 | var angle = 0; 684 | var focus = {x: 0, y: 0}; 685 | 686 | // additional offset 687 | var shift = 0; 688 | // scale factor for offset 689 | var expansion = 1; 690 | 691 | if (fillStyle.type_ == 'gradient') { 692 | var x0 = fillStyle.x0_ / this.arcScaleX_; 693 | var y0 = fillStyle.y0_ / this.arcScaleY_; 694 | var x1 = fillStyle.x1_ / this.arcScaleX_; 695 | var y1 = fillStyle.y1_ / this.arcScaleY_; 696 | var p0 = this.getCoords_(x0, y0); 697 | var p1 = this.getCoords_(x1, y1); 698 | var dx = p1.x - p0.x; 699 | var dy = p1.y - p0.y; 700 | angle = Math.atan2(dx, dy) * 180 / Math.PI; 701 | 702 | // The angle should be a non-negative number. 703 | if (angle < 0) { 704 | angle += 360; 705 | } 706 | 707 | // Very small angles produce an unexpected result because they are 708 | // converted to a scientific notation string. 709 | if (angle < 1e-6) { 710 | angle = 0; 711 | } 712 | } else { 713 | var p0 = this.getCoords_(fillStyle.x0_, fillStyle.y0_); 714 | var width = max.x - min.x; 715 | var height = max.y - min.y; 716 | focus = { 717 | x: (p0.x - min.x) / width, 718 | y: (p0.y - min.y) / height 719 | }; 720 | 721 | width /= this.arcScaleX_ * Z; 722 | height /= this.arcScaleY_ * Z; 723 | var dimension = m.max(width, height); 724 | shift = 2 * fillStyle.r0_ / dimension; 725 | expansion = 2 * fillStyle.r1_ / dimension - shift; 726 | } 727 | 728 | // We need to sort the color stops in ascending order by offset, 729 | // otherwise IE won't interpret it correctly. 730 | var stops = fillStyle.colors_; 731 | stops.sort(function(cs1, cs2) { 732 | return cs1.offset - cs2.offset; 733 | }); 734 | 735 | var length = stops.length; 736 | var color1 = stops[0].color; 737 | var color2 = stops[length - 1].color; 738 | var opacity1 = stops[0].alpha * this.globalAlpha; 739 | var opacity2 = stops[length - 1].alpha * this.globalAlpha; 740 | 741 | var colors = []; 742 | for (var i = 0; i < length; i++) { 743 | var stop = stops[i]; 744 | colors.push(stop.offset * expansion + shift + ' ' + stop.color); 745 | } 746 | 747 | // When colors attribute is used, the meanings of opacity and o:opacity2 748 | // are reversed. 749 | lineStr.push(''); 758 | } else { 759 | lineStr.push(''); 761 | } 762 | 763 | lineStr.push(''); 764 | 765 | this.element_.insertAdjacentHTML('beforeEnd', lineStr.join('')); 766 | }; 767 | 768 | contextPrototype.fill = function() { 769 | this.stroke(true); 770 | } 771 | 772 | contextPrototype.closePath = function() { 773 | this.currentPath_.push({type: 'close'}); 774 | }; 775 | 776 | /** 777 | * @private 778 | */ 779 | contextPrototype.getCoords_ = function(aX, aY) { 780 | var m = this.m_; 781 | return { 782 | x: Z * (aX * m[0][0] + aY * m[1][0] + m[2][0]) - Z2, 783 | y: Z * (aX * m[0][1] + aY * m[1][1] + m[2][1]) - Z2 784 | } 785 | }; 786 | 787 | contextPrototype.save = function() { 788 | var o = {}; 789 | copyState(this, o); 790 | this.aStack_.push(o); 791 | this.mStack_.push(this.m_); 792 | this.m_ = matrixMultiply(createMatrixIdentity(), this.m_); 793 | }; 794 | 795 | contextPrototype.restore = function() { 796 | copyState(this.aStack_.pop(), this); 797 | this.m_ = this.mStack_.pop(); 798 | }; 799 | 800 | function matrixIsFinite(m) { 801 | for (var j = 0; j < 3; j++) { 802 | for (var k = 0; k < 2; k++) { 803 | if (!isFinite(m[j][k]) || isNaN(m[j][k])) { 804 | return false; 805 | } 806 | } 807 | } 808 | return true; 809 | } 810 | 811 | function setM(ctx, m, updateLineScale) { 812 | if (!matrixIsFinite(m)) { 813 | return; 814 | } 815 | ctx.m_ = m; 816 | 817 | if (updateLineScale) { 818 | // Get the line scale. 819 | // Determinant of this.m_ means how much the area is enlarged by the 820 | // transformation. So its square root can be used as a scale factor 821 | // for width. 822 | var det = m[0][0] * m[1][1] - m[0][1] * m[1][0]; 823 | ctx.lineScale_ = sqrt(abs(det)); 824 | } 825 | } 826 | 827 | contextPrototype.translate = function(aX, aY) { 828 | var m1 = [ 829 | [1, 0, 0], 830 | [0, 1, 0], 831 | [aX, aY, 1] 832 | ]; 833 | 834 | setM(this, matrixMultiply(m1, this.m_), false); 835 | }; 836 | 837 | contextPrototype.rotate = function(aRot) { 838 | var c = mc(aRot); 839 | var s = ms(aRot); 840 | 841 | var m1 = [ 842 | [c, s, 0], 843 | [-s, c, 0], 844 | [0, 0, 1] 845 | ]; 846 | 847 | setM(this, matrixMultiply(m1, this.m_), false); 848 | }; 849 | 850 | contextPrototype.scale = function(aX, aY) { 851 | this.arcScaleX_ *= aX; 852 | this.arcScaleY_ *= aY; 853 | var m1 = [ 854 | [aX, 0, 0], 855 | [0, aY, 0], 856 | [0, 0, 1] 857 | ]; 858 | 859 | setM(this, matrixMultiply(m1, this.m_), true); 860 | }; 861 | 862 | contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) { 863 | var m1 = [ 864 | [m11, m12, 0], 865 | [m21, m22, 0], 866 | [dx, dy, 1] 867 | ]; 868 | 869 | setM(this, matrixMultiply(m1, this.m_), true); 870 | }; 871 | 872 | contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) { 873 | var m = [ 874 | [m11, m12, 0], 875 | [m21, m22, 0], 876 | [dx, dy, 1] 877 | ]; 878 | 879 | setM(this, m, true); 880 | }; 881 | 882 | /******** STUBS ********/ 883 | contextPrototype.clip = function() { 884 | // TODO: Implement 885 | }; 886 | 887 | contextPrototype.arcTo = function() { 888 | // TODO: Implement 889 | }; 890 | 891 | contextPrototype.createPattern = function() { 892 | return new CanvasPattern_; 893 | }; 894 | 895 | // Gradient / Pattern Stubs 896 | function CanvasGradient_(aType) { 897 | this.type_ = aType; 898 | this.x0_ = 0; 899 | this.y0_ = 0; 900 | this.r0_ = 0; 901 | this.x1_ = 0; 902 | this.y1_ = 0; 903 | this.r1_ = 0; 904 | this.colors_ = []; 905 | } 906 | 907 | CanvasGradient_.prototype.addColorStop = function(aOffset, aColor) { 908 | aColor = processStyle(aColor); 909 | this.colors_.push({offset: aOffset, 910 | color: aColor.color, 911 | alpha: aColor.alpha}); 912 | }; 913 | 914 | function CanvasPattern_() {} 915 | 916 | // set up externs 917 | G_vmlCanvasManager = G_vmlCanvasManager_; 918 | CanvasRenderingContext2D = CanvasRenderingContext2D_; 919 | CanvasGradient = CanvasGradient_; 920 | CanvasPattern = CanvasPattern_; 921 | 922 | })(); 923 | 924 | } // if 925 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jQuery Knob demo 5 | 6 | 7 | 8 | 92 | 104 | 105 | 106 |
107 |

jQuery Knob

108 |
109 |
110 |

Nice, downward compatible, touchable, jQuery dial. Flattr this

111 |

* implemented interactions : mouse click and wheel mouse, keyboard (on focus) and fingers (touch events)

112 |
113 |
114 |

× Disable display input

115 |
116 | data-width="100"
117 | data-displayInput=false
118 |             
119 | 120 |
121 |
122 |

× 'cursor' mode

123 |
124 | data-width="150"
125 | data-cursor=true
126 | data-thickness=.3
127 | data-fgColor="#222222"
128 |             
129 | 130 |
131 |
132 |

× Display previous value

133 |
134 | data-displayPrevious=true
135 | data-min="-100"
136 |             
137 | 138 |
139 |
140 |
141 |

× Angle offset

142 |
143 | data-angleOffset=90
144 | data-linecap=round
145 |             
146 | 147 |
148 |
149 |

× Angle offset and arc

150 |
151 | data-fgColor="#66CC66"
152 | data-angleOffset=-125
153 | data-angleArc=250
154 | data-rotation=anticlockwise
155 |             
156 | 157 |
158 |
159 |

× 4-digit, step 0.1

160 |
161 | data-step=".1"
162 | data-min="-10000"
163 | data-max="10000"
164 | value="0"
165 | data-displayPrevious=true
166 |             
167 | 168 |
169 |
170 |
171 |

× Overloaded 'draw' method

172 |
173 |
174 |
175 |
176 |     data-width="75"
177 |     data-fgColor="#ffec03"
178 |     data-skin="tron"
179 |     data-thickness=".2"
180 |     data-displayPrevious=true
181 |                 
182 | 183 |
184 |
185 |
186 |     data-width="150"
187 |     data-fgColor="#ffec03"
188 |     data-skin="tron"
189 |     data-thickness=".2"
190 |     data-displayPrevious=true
191 |                 
192 | 193 |
194 |
195 |
196 |     data-width="150"
197 |     data-fgColor="#C0ffff"
198 |     data-skin="tron"
199 |     data-thickness=".1"
200 |     data-angleOffset="180"
201 |                 
202 | 203 |
204 |
205 |
206 |
207 |

× Responsive

208 |
209 | data-width="80%"
210 |             
211 |
212 | 213 | Current div width is 30% of window width.
214 | Knob width is 80% of current div.
215 | Knob width is 80% of 30% of window width.
216 | Test resizing window. 217 |
218 |
219 |
220 | 221 |
222 |
223 |
224 | 240 |
241 |

× Superpose (clock)

242 |
243 |
244 | 245 |
246 |
247 | 248 |
249 |
250 | 251 |
252 |
253 |
254 |
255 |
256 |

× Readonly

257 |
258 | readonly (or data-readOnly=true)
259 | data-thickness=".4"
260 | data-fgColor="chartreuse"
261 |             
262 | 263 |
264 |
265 |

× Dynamic

266 |
267 | data-width="200"
268 |             
269 | 270 | 271 |
272 | data-width="50"
273 | data-cursor=true
274 |             
275 | 276 | 277 |
278 |
279 |

× Infinite || iPod click wheel

280 |
281 |
282 | data-width="150"
283 | data-cursor=true
284 | data-thickness=".5"
285 | data-fgColor="#AAAAAA"
286 | data-bgColor="#FFFFFF"
287 | data-displayInput="false"
288 | + some code
289 |                 
290 | 291 |
292 |
293 |
0
294 |
295 |
296 |
297 |
298 |
299 | 300 |

jQuery Knob is © 2012 Anthony Terrien - MIT License

301 |
302 | 303 | 304 | -------------------------------------------------------------------------------- /js/jquery.knob.js: -------------------------------------------------------------------------------- 1 | /*!jQuery Knob*/ 2 | /** 3 | * Downward compatible, touchable dial 4 | * 5 | * Version: 1.2.12 6 | * Requires: jQuery v1.7+ 7 | * 8 | * Copyright (c) 2012 Anthony Terrien 9 | * Under MIT License (http://www.opensource.org/licenses/mit-license.php) 10 | * 11 | * Thanks to vor, eskimoblood, spiffistan, FabrizioC 12 | */ 13 | (function (factory) { 14 | if (typeof exports === 'object') { 15 | // CommonJS 16 | module.exports = factory(require('jquery')); 17 | } else if (typeof define === 'function' && define.amd) { 18 | // AMD. Register as an anonymous module. 19 | define(['jquery'], factory); 20 | } else { 21 | // Browser globals 22 | factory(jQuery); 23 | } 24 | }(function ($) { 25 | 26 | /** 27 | * Kontrol library 28 | */ 29 | "use strict"; 30 | 31 | /** 32 | * Definition of globals and core 33 | */ 34 | var k = {}, // kontrol 35 | max = Math.max, 36 | min = Math.min; 37 | 38 | k.c = {}; 39 | k.c.d = $(document); 40 | k.c.t = function (e) { 41 | return e.originalEvent.touches.length - 1; 42 | }; 43 | 44 | /** 45 | * Kontrol Object 46 | * 47 | * Definition of an abstract UI control 48 | * 49 | * Each concrete component must call this one. 50 | * 51 | * k.o.call(this); 52 | * 53 | */ 54 | k.o = function () { 55 | var s = this; 56 | 57 | this.o = null; // array of options 58 | this.$ = null; // jQuery wrapped element 59 | this.i = null; // mixed HTMLInputElement or array of HTMLInputElement 60 | this.g = null; // deprecated 2D graphics context for 'pre-rendering' 61 | this.v = null; // value ; mixed array or integer 62 | this.cv = null; // change value ; not commited value 63 | this.x = 0; // canvas x position 64 | this.y = 0; // canvas y position 65 | this.w = 0; // canvas width 66 | this.h = 0; // canvas height 67 | this.$c = null; // jQuery canvas element 68 | this.c = null; // rendered canvas context 69 | this.t = 0; // touches index 70 | this.isInit = false; 71 | this.fgColor = null; // main color 72 | this.pColor = null; // previous color 73 | this.dH = null; // draw hook 74 | this.cH = null; // change hook 75 | this.eH = null; // cancel hook 76 | this.rH = null; // release hook 77 | this.scale = 1; // scale factor 78 | this.relative = false; 79 | this.relativeWidth = false; 80 | this.relativeHeight = false; 81 | this.$div = null; // component div 82 | 83 | this.run = function () { 84 | var cf = function (e, conf) { 85 | var k; 86 | for (k in conf) { 87 | s.o[k] = conf[k]; 88 | } 89 | s._carve().init(); 90 | s._configure() 91 | ._draw(); 92 | }; 93 | 94 | if (this.$.data('kontroled')) return; 95 | this.$.data('kontroled', true); 96 | 97 | this.extend(); 98 | this.o = $.extend({ 99 | // Config 100 | min: this.$.data('min') !== undefined ? this.$.data('min') : 0, 101 | max: this.$.data('max') !== undefined ? this.$.data('max') : 100, 102 | stopper: true, 103 | readOnly: this.$.data('readonly') || (this.$.attr('readonly') === 'readonly'), 104 | 105 | // UI 106 | cursor: this.$.data('cursor') === true && 30 107 | || this.$.data('cursor') || 0, 108 | thickness: this.$.data('thickness') 109 | && Math.max(Math.min(this.$.data('thickness'), 1), 0.01) 110 | || 0.35, 111 | lineCap: this.$.data('linecap') || 'butt', 112 | width: this.$.data('width') || 200, 113 | height: this.$.data('height') || 200, 114 | displayInput: this.$.data('displayinput') == null || this.$.data('displayinput'), 115 | displayPrevious: this.$.data('displayprevious'), 116 | fgColor: this.$.data('fgcolor') || '#87CEEB', 117 | inputColor: this.$.data('inputcolor'), 118 | font: this.$.data('font') || 'Arial', 119 | fontWeight: this.$.data('font-weight') || 'bold', 120 | inline: false, 121 | step: this.$.data('step') || 1, 122 | rotation: this.$.data('rotation'), 123 | 124 | // Hooks 125 | draw: null, // function () {} 126 | change: null, // function (value) {} 127 | cancel: null, // function () {} 128 | release: null, // function (value) {} 129 | 130 | // Output formatting, allows to add unit: %, ms ... 131 | format: function(v) { 132 | return v; 133 | }, 134 | parse: function (v) { 135 | return parseFloat(v); 136 | } 137 | }, this.o 138 | ); 139 | 140 | // finalize options 141 | this.o.flip = this.o.rotation === 'anticlockwise' || this.o.rotation === 'acw'; 142 | if (!this.o.inputColor) { 143 | this.o.inputColor = this.o.fgColor; 144 | } 145 | 146 | // routing value 147 | if (this.$.is('fieldset')) { 148 | 149 | // fieldset = array of integer 150 | this.v = {}; 151 | this.i = this.$.find('input'); 152 | this.i.each(function(k) { 153 | var $this = $(this); 154 | s.i[k] = $this; 155 | s.v[k] = s.o.parse($this.val()); 156 | 157 | $this.bind( 158 | 'change blur', 159 | function () { 160 | var val = {}; 161 | val[k] = $this.val(); 162 | s.val(s._validate(val)); 163 | } 164 | ); 165 | }); 166 | this.$.find('legend').remove(); 167 | } else { 168 | 169 | // input = integer 170 | this.i = this.$; 171 | this.v = this.o.parse(this.$.val()); 172 | this.v === '' && (this.v = this.o.min); 173 | this.$.bind( 174 | 'change blur', 175 | function () { 176 | s.val(s._validate(s.o.parse(s.$.val()))); 177 | } 178 | ); 179 | 180 | } 181 | 182 | !this.o.displayInput && this.$.hide(); 183 | 184 | // adds needed DOM elements (canvas, div) 185 | this.$c = $(document.createElement('canvas')).attr({ 186 | width: this.o.width, 187 | height: this.o.height 188 | }); 189 | 190 | // wraps all elements in a div 191 | // add to DOM before Canvas init is triggered 192 | this.$div = $('
'); 196 | 197 | this.$.wrap(this.$div).before(this.$c); 198 | this.$div = this.$.parent(); 199 | 200 | if (typeof G_vmlCanvasManager !== 'undefined') { 201 | G_vmlCanvasManager.initElement(this.$c[0]); 202 | } 203 | 204 | this.c = this.$c[0].getContext ? this.$c[0].getContext('2d') : null; 205 | 206 | if (!this.c) { 207 | throw { 208 | name: "CanvasNotSupportedException", 209 | message: "Canvas not supported. Please use excanvas on IE8.0.", 210 | toString: function(){return this.name + ": " + this.message} 211 | } 212 | } 213 | 214 | // hdpi support 215 | this.scale = (window.devicePixelRatio || 1) / ( 216 | this.c.webkitBackingStorePixelRatio || 217 | this.c.mozBackingStorePixelRatio || 218 | this.c.msBackingStorePixelRatio || 219 | this.c.oBackingStorePixelRatio || 220 | this.c.backingStorePixelRatio || 1 221 | ); 222 | 223 | // detects relative width / height 224 | this.relativeWidth = this.o.width % 1 !== 0 225 | && this.o.width.indexOf('%'); 226 | this.relativeHeight = this.o.height % 1 !== 0 227 | && this.o.height.indexOf('%'); 228 | this.relative = this.relativeWidth || this.relativeHeight; 229 | 230 | // computes size and carves the component 231 | this._carve(); 232 | 233 | // prepares props for transaction 234 | if (this.v instanceof Object) { 235 | this.cv = {}; 236 | this.copy(this.v, this.cv); 237 | } else { 238 | this.cv = this.v; 239 | } 240 | 241 | // binds configure event 242 | this.$ 243 | .bind("configure", cf) 244 | .parent() 245 | .bind("configure", cf); 246 | 247 | // finalize init 248 | this._listen() 249 | ._configure() 250 | ._xy() 251 | .init(); 252 | 253 | this.isInit = true; 254 | 255 | this.$.val(this.o.format(this.v)); 256 | this._draw(); 257 | 258 | return this; 259 | }; 260 | 261 | this._carve = function() { 262 | if (this.relative) { 263 | var w = this.relativeWidth ? 264 | this.$div.parent().width() * 265 | parseInt(this.o.width) / 100 266 | : this.$div.parent().width(), 267 | h = this.relativeHeight ? 268 | this.$div.parent().height() * 269 | parseInt(this.o.height) / 100 270 | : this.$div.parent().height(); 271 | 272 | // apply relative 273 | this.w = this.h = Math.min(w, h); 274 | } else { 275 | this.w = this.o.width; 276 | this.h = this.o.height; 277 | } 278 | 279 | // finalize div 280 | this.$div.css({ 281 | 'width': this.w + 'px', 282 | 'height': this.h + 'px' 283 | }); 284 | 285 | // finalize canvas with computed width 286 | this.$c.attr({ 287 | width: this.w, 288 | height: this.h 289 | }); 290 | 291 | // scaling 292 | if (this.scale !== 1) { 293 | this.$c[0].width = this.$c[0].width * this.scale; 294 | this.$c[0].height = this.$c[0].height * this.scale; 295 | this.$c.width(this.w); 296 | this.$c.height(this.h); 297 | } 298 | 299 | return this; 300 | }; 301 | 302 | this._draw = function () { 303 | 304 | // canvas pre-rendering 305 | var d = true; 306 | 307 | s.g = s.c; 308 | 309 | s.clear(); 310 | 311 | s.dH && (d = s.dH()); 312 | 313 | d !== false && s.draw(); 314 | }; 315 | 316 | this._touch = function (e) { 317 | var touchMove = function (e) { 318 | var v = s.xy2val( 319 | e.originalEvent.touches[s.t].pageX, 320 | e.originalEvent.touches[s.t].pageY 321 | ); 322 | 323 | if (v == s.cv) return; 324 | 325 | if (s.cH && s.cH(v) === false) return; 326 | 327 | s.change(s._validate(v)); 328 | s._draw(); 329 | }; 330 | 331 | // get touches index 332 | this.t = k.c.t(e); 333 | 334 | // First touch 335 | touchMove(e); 336 | 337 | // Touch events listeners 338 | k.c.d 339 | .bind("touchmove.k", touchMove) 340 | .bind( 341 | "touchend.k", 342 | function () { 343 | k.c.d.unbind('touchmove.k touchend.k'); 344 | s.val(s.cv); 345 | } 346 | ); 347 | 348 | return this; 349 | }; 350 | 351 | this._mouse = function (e) { 352 | var mouseMove = function (e) { 353 | var v = s.xy2val(e.pageX, e.pageY); 354 | 355 | if (v == s.cv) return; 356 | 357 | if (s.cH && (s.cH(v) === false)) return; 358 | 359 | s.change(s._validate(v)); 360 | s._draw(); 361 | }; 362 | 363 | // First click 364 | mouseMove(e); 365 | 366 | // Mouse events listeners 367 | k.c.d 368 | .bind("mousemove.k", mouseMove) 369 | .bind( 370 | // Escape key cancel current change 371 | "keyup.k", 372 | function (e) { 373 | if (e.keyCode === 27) { 374 | k.c.d.unbind("mouseup.k mousemove.k keyup.k"); 375 | 376 | if (s.eH && s.eH() === false) 377 | return; 378 | 379 | s.cancel(); 380 | } 381 | } 382 | ) 383 | .bind( 384 | "mouseup.k", 385 | function (e) { 386 | k.c.d.unbind('mousemove.k mouseup.k keyup.k'); 387 | s.val(s.cv); 388 | } 389 | ); 390 | 391 | return this; 392 | }; 393 | 394 | this._xy = function () { 395 | var o = this.$c.offset(); 396 | this.x = o.left; 397 | this.y = o.top; 398 | 399 | return this; 400 | }; 401 | 402 | this._listen = function () { 403 | if (!this.o.readOnly) { 404 | this.$c 405 | .bind( 406 | "mousedown", 407 | function (e) { 408 | e.preventDefault(); 409 | s._xy()._mouse(e); 410 | } 411 | ) 412 | .bind( 413 | "touchstart", 414 | function (e) { 415 | e.preventDefault(); 416 | s._xy()._touch(e); 417 | } 418 | ); 419 | 420 | this.listen(); 421 | } else { 422 | this.$.attr('readonly', 'readonly'); 423 | } 424 | 425 | if (this.relative) { 426 | $(window).resize(function() { 427 | s._carve().init(); 428 | s._draw(); 429 | }); 430 | } 431 | 432 | return this; 433 | }; 434 | 435 | this._configure = function () { 436 | 437 | // Hooks 438 | if (this.o.draw) this.dH = this.o.draw; 439 | if (this.o.change) this.cH = this.o.change; 440 | if (this.o.cancel) this.eH = this.o.cancel; 441 | if (this.o.release) this.rH = this.o.release; 442 | 443 | if (this.o.displayPrevious) { 444 | this.pColor = this.h2rgba(this.o.fgColor, "0.4"); 445 | this.fgColor = this.h2rgba(this.o.fgColor, "0.6"); 446 | } else { 447 | this.fgColor = this.o.fgColor; 448 | } 449 | 450 | return this; 451 | }; 452 | 453 | this._clear = function () { 454 | this.$c[0].width = this.$c[0].width; 455 | }; 456 | 457 | this._validate = function (v) { 458 | var val = (~~ (((v < 0) ? -0.5 : 0.5) + (v/this.o.step))) * this.o.step; 459 | return Math.round(val * 100) / 100; 460 | }; 461 | 462 | // Abstract methods 463 | this.listen = function () {}; // on start, one time 464 | this.extend = function () {}; // each time configure triggered 465 | this.init = function () {}; // each time configure triggered 466 | this.change = function (v) {}; // on change 467 | this.val = function (v) {}; // on release 468 | this.xy2val = function (x, y) {}; // 469 | this.draw = function () {}; // on change / on release 470 | this.clear = function () { this._clear(); }; 471 | 472 | // Utils 473 | this.h2rgba = function (h, a) { 474 | var rgb; 475 | h = h.substring(1,7); 476 | rgb = [ 477 | parseInt(h.substring(0,2), 16), 478 | parseInt(h.substring(2,4), 16), 479 | parseInt(h.substring(4,6), 16) 480 | ]; 481 | 482 | return "rgba(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + "," + a + ")"; 483 | }; 484 | 485 | this.copy = function (f, t) { 486 | for (var i in f) { 487 | t[i] = f[i]; 488 | } 489 | }; 490 | }; 491 | 492 | 493 | /** 494 | * k.Dial 495 | */ 496 | k.Dial = function () { 497 | k.o.call(this); 498 | 499 | this.startAngle = null; 500 | this.xy = null; 501 | this.radius = null; 502 | this.lineWidth = null; 503 | this.cursorExt = null; 504 | this.w2 = null; 505 | this.PI2 = 2*Math.PI; 506 | 507 | this.extend = function () { 508 | this.o = $.extend({ 509 | bgColor: this.$.data('bgcolor') || '#EEEEEE', 510 | angleOffset: this.$.data('angleoffset') || 0, 511 | angleArc: this.$.data('anglearc') || 360, 512 | inline: true 513 | }, this.o); 514 | }; 515 | 516 | this.val = function (v, triggerRelease) { 517 | if (null != v) { 518 | 519 | // reverse format 520 | v = this.o.parse(v); 521 | 522 | if (triggerRelease !== false 523 | && v != this.v 524 | && this.rH 525 | && this.rH(v) === false) { return; } 526 | 527 | this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v; 528 | this.v = this.cv; 529 | this.$.val(this.o.format(this.v)); 530 | this._draw(); 531 | } else { 532 | return this.v; 533 | } 534 | }; 535 | 536 | this.xy2val = function (x, y) { 537 | var a, ret; 538 | 539 | a = Math.atan2( 540 | x - (this.x + this.w2), 541 | - (y - this.y - this.w2) 542 | ) - this.angleOffset; 543 | 544 | if (this.o.flip) { 545 | a = this.angleArc - a - this.PI2; 546 | } 547 | 548 | if (this.angleArc != this.PI2 && (a < 0) && (a > -0.5)) { 549 | 550 | // if isset angleArc option, set to min if .5 under min 551 | a = 0; 552 | } else if (a < 0) { 553 | a += this.PI2; 554 | } 555 | 556 | ret = (a * (this.o.max - this.o.min) / this.angleArc) + this.o.min; 557 | 558 | this.o.stopper && (ret = max(min(ret, this.o.max), this.o.min)); 559 | 560 | return ret; 561 | }; 562 | 563 | this.listen = function () { 564 | 565 | // bind MouseWheel 566 | var s = this, mwTimerStop, 567 | mwTimerRelease, 568 | mw = function (e) { 569 | e.preventDefault(); 570 | 571 | var ori = e.originalEvent, 572 | deltaX = ori.detail || ori.wheelDeltaX, 573 | deltaY = ori.detail || ori.wheelDeltaY, 574 | v = s._validate(s.o.parse(s.$.val())) 575 | + ( 576 | deltaX > 0 || deltaY > 0 577 | ? s.o.step 578 | : deltaX < 0 || deltaY < 0 ? -s.o.step : 0 579 | ); 580 | 581 | v = max(min(v, s.o.max), s.o.min); 582 | 583 | s.val(v, false); 584 | 585 | if (s.rH) { 586 | // Handle mousewheel stop 587 | clearTimeout(mwTimerStop); 588 | mwTimerStop = setTimeout(function () { 589 | s.rH(v); 590 | mwTimerStop = null; 591 | }, 100); 592 | 593 | // Handle mousewheel releases 594 | if (!mwTimerRelease) { 595 | mwTimerRelease = setTimeout(function () { 596 | if (mwTimerStop) 597 | s.rH(v); 598 | mwTimerRelease = null; 599 | }, 200); 600 | } 601 | } 602 | }, 603 | kval, 604 | to, 605 | m = 1, 606 | kv = { 607 | 37: -s.o.step, 608 | 38: s.o.step, 609 | 39: s.o.step, 610 | 40: -s.o.step 611 | }; 612 | 613 | this.$ 614 | .bind( 615 | "keydown", 616 | function (e) { 617 | var kc = e.keyCode; 618 | 619 | // numpad support 620 | if (kc >= 96 && kc <= 105) { 621 | kc = e.keyCode = kc - 48; 622 | } 623 | 624 | kval = parseInt(String.fromCharCode(kc)); 625 | 626 | if (isNaN(kval)) { 627 | (kc !== 13) // enter 628 | && kc !== 8 // bs 629 | && kc !== 9 // tab 630 | && kc !== 189 // - 631 | && (kc !== 190 632 | || s.$.val().match(/\./)) // . allowed once 633 | && e.preventDefault(); 634 | 635 | // arrows 636 | if ($.inArray(kc,[37,38,39,40]) > -1) { 637 | e.preventDefault(); 638 | 639 | var v = s.o.parse(s.$.val()) + kv[kc] * m; 640 | s.o.stopper && (v = max(min(v, s.o.max), s.o.min)); 641 | 642 | s.change(s._validate(v)); 643 | s._draw(); 644 | 645 | // long time keydown speed-up 646 | to = window.setTimeout(function () { 647 | m *= 2; 648 | }, 30); 649 | } 650 | } 651 | } 652 | ) 653 | .bind( 654 | "keyup", 655 | function (e) { 656 | if (isNaN(kval)) { 657 | if (to) { 658 | window.clearTimeout(to); 659 | to = null; 660 | m = 1; 661 | s.val(s.$.val()); 662 | } 663 | } else { 664 | // kval postcond 665 | (s.$.val() > s.o.max && s.$.val(s.o.max)) 666 | || (s.$.val() < s.o.min && s.$.val(s.o.min)); 667 | } 668 | } 669 | ); 670 | 671 | this.$c.bind("mousewheel DOMMouseScroll", mw); 672 | this.$.bind("mousewheel DOMMouseScroll", mw); 673 | }; 674 | 675 | this.init = function () { 676 | if (this.v < this.o.min 677 | || this.v > this.o.max) { this.v = this.o.min; } 678 | 679 | this.$.val(this.v); 680 | this.w2 = this.w / 2; 681 | this.cursorExt = this.o.cursor / 100; 682 | this.xy = this.w2 * this.scale; 683 | this.lineWidth = this.xy * this.o.thickness; 684 | this.lineCap = this.o.lineCap; 685 | this.radius = this.xy - this.lineWidth / 2; 686 | 687 | this.o.angleOffset 688 | && (this.o.angleOffset = isNaN(this.o.angleOffset) ? 0 : this.o.angleOffset); 689 | 690 | this.o.angleArc 691 | && (this.o.angleArc = isNaN(this.o.angleArc) ? this.PI2 : this.o.angleArc); 692 | 693 | // deg to rad 694 | this.angleOffset = this.o.angleOffset * Math.PI / 180; 695 | this.angleArc = this.o.angleArc * Math.PI / 180; 696 | 697 | // compute start and end angles 698 | this.startAngle = 1.5 * Math.PI + this.angleOffset; 699 | this.endAngle = 1.5 * Math.PI + this.angleOffset + this.angleArc; 700 | 701 | var s = max( 702 | String(Math.abs(this.o.max)).length, 703 | String(Math.abs(this.o.min)).length, 704 | 2 705 | ) + 2; 706 | 707 | this.o.displayInput 708 | && this.i.css({ 709 | 'width' : ((this.w / 2 + 4) >> 0) + 'px', 710 | 'height' : ((this.w / 3) >> 0) + 'px', 711 | 'position' : 'absolute', 712 | 'vertical-align' : 'middle', 713 | 'margin-top' : ((this.w / 3) >> 0) + 'px', 714 | 'margin-left' : '-' + ((this.w * 3 / 4 + 2) >> 0) + 'px', 715 | 'border' : 0, 716 | 'background' : 'none', 717 | 'font' : this.o.fontWeight + ' ' + ((this.w / s) >> 0) + 'px ' + this.o.font, 718 | 'text-align' : 'center', 719 | 'color' : this.o.inputColor || this.o.fgColor, 720 | 'padding' : '0px', 721 | '-webkit-appearance': 'none' 722 | }) || this.i.css({ 723 | 'width': '0px', 724 | 'visibility': 'hidden' 725 | }); 726 | }; 727 | 728 | this.change = function (v) { 729 | this.cv = v; 730 | this.$.val(this.o.format(v)); 731 | }; 732 | 733 | this.angle = function (v) { 734 | return (v - this.o.min) * this.angleArc / (this.o.max - this.o.min); 735 | }; 736 | 737 | this.arc = function (v) { 738 | var sa, ea; 739 | v = this.angle(v); 740 | if (this.o.flip) { 741 | sa = this.endAngle + 0.00001; 742 | ea = sa - v - 0.00001; 743 | } else { 744 | sa = this.startAngle - 0.00001; 745 | ea = sa + v + 0.00001; 746 | } 747 | this.o.cursor 748 | && (sa = ea - this.cursorExt) 749 | && (ea = ea + this.cursorExt); 750 | 751 | return { 752 | s: sa, 753 | e: ea, 754 | d: this.o.flip && !this.o.cursor 755 | }; 756 | }; 757 | 758 | this.draw = function () { 759 | var c = this.g, // context 760 | a = this.arc(this.cv), // Arc 761 | pa, // Previous arc 762 | r = 1; 763 | 764 | c.lineWidth = this.lineWidth; 765 | c.lineCap = this.lineCap; 766 | 767 | if (this.o.bgColor !== "none") { 768 | c.beginPath(); 769 | c.strokeStyle = this.o.bgColor; 770 | c.arc(this.xy, this.xy, this.radius, this.endAngle - 0.00001, this.startAngle + 0.00001, true); 771 | c.stroke(); 772 | } 773 | 774 | if (this.o.displayPrevious) { 775 | pa = this.arc(this.v); 776 | c.beginPath(); 777 | c.strokeStyle = this.pColor; 778 | c.arc(this.xy, this.xy, this.radius, pa.s, pa.e, pa.d); 779 | c.stroke(); 780 | r = this.cv == this.v; 781 | } 782 | 783 | c.beginPath(); 784 | c.strokeStyle = r ? this.o.fgColor : this.fgColor ; 785 | c.arc(this.xy, this.xy, this.radius, a.s, a.e, a.d); 786 | c.stroke(); 787 | }; 788 | 789 | this.cancel = function () { 790 | this.val(this.v); 791 | }; 792 | }; 793 | 794 | $.fn.dial = $.fn.knob = function (o) { 795 | return this.each( 796 | function () { 797 | var d = new k.Dial(); 798 | d.o = o; 799 | d.$ = $(this); 800 | d.run(); 801 | } 802 | ).parent(); 803 | }; 804 | 805 | })); 806 | -------------------------------------------------------------------------------- /knob.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "knob", 3 | "title": "jQuery Knob", 4 | "description": "Nice, downward compatible, touchable, jQuery dial.", 5 | "keywords": [ 6 | "dial", 7 | "button", 8 | "knob", 9 | "ui", 10 | "input" 11 | ], 12 | "version": "1.2.13", 13 | "author": { 14 | "name": "Anthony Terrien", 15 | "url": "https://github.com/aterrien" 16 | }, 17 | "maintainers": [ 18 | { 19 | "name": "Anthony Terrien", 20 | "email": "kontrol@anthonyterrien.com", 21 | "url": "http://anthonyterrien.com/knob" 22 | } 23 | ], 24 | "licenses": [ 25 | { 26 | "type": "MIT", 27 | "url": "http://opensource.org/licenses/mit-license.php" 28 | } 29 | ], 30 | "bugs": "https://github.com/aterrien/jQuery-Knob/issues", 31 | "homepage": "https://github.com/aterrien/jQuery-Knob", 32 | "docs": "https://github.com/aterrien/jQuery-Knob", 33 | "download": "https://github.com/aterrien/jQuery-Knob/tags", 34 | "dependencies": { 35 | "jquery": ">=1.7.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-knob", 3 | "version": "1.2.13", 4 | "description": "Nice, downward compatible, touchable, jQuery dial", 5 | "main": "dist/jquery.knob.min.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/aterrien/jQuery-Knob.git" 12 | }, 13 | "keywords": [ 14 | "jquery", 15 | "knob", 16 | "dial" 17 | ], 18 | "author": "Anthony Terrien", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/aterrien/jQuery-Knob/issues" 22 | }, 23 | "homepage": "https://github.com/aterrien/jQuery-Knob#readme" 24 | } 25 | -------------------------------------------------------------------------------- /secretplan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aterrien/jQuery-Knob/755309e933d326ffaa5a2d758dc377147b766515/secretplan.jpg --------------------------------------------------------------------------------