jquery.knob.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*!jQuery Knob*/
  2. /**
  3. * Downward compatible, touchable dial
  4. *
  5. * Version: 1.2.0 (15/07/2012)
  6. * Requires: jQuery v1.7+
  7. *
  8. * Copyright (c) 2012 Anthony Terrien
  9. * Under MIT and GPL licenses:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. * Thanks to vor, eskimoblood, spiffistan, FabrizioC
  14. */
  15. (function($) {
  16. /**
  17. * Kontrol library
  18. */
  19. "use strict";
  20. /**
  21. * Definition of globals and core
  22. */
  23. var k = {}, // kontrol
  24. max = Math.max,
  25. min = Math.min;
  26. k.c = {};
  27. k.c.d = $(document);
  28. k.c.t = function (e) {
  29. return e.originalEvent.touches.length - 1;
  30. };
  31. /**
  32. * Kontrol Object
  33. *
  34. * Definition of an abstract UI control
  35. *
  36. * Each concrete component must call this one.
  37. * <code>
  38. * k.o.call(this);
  39. * </code>
  40. */
  41. k.o = function () {
  42. var s = this;
  43. this.o = null; // array of options
  44. this.$ = null; // jQuery wrapped element
  45. this.i = null; // mixed HTMLInputElement or array of HTMLInputElement
  46. this.g = null; // 2D graphics context for 'pre-rendering'
  47. this.v = null; // value ; mixed array or integer
  48. this.cv = null; // change value ; not commited value
  49. this.x = 0; // canvas x position
  50. this.y = 0; // canvas y position
  51. this.$c = null; // jQuery canvas element
  52. this.c = null; // rendered canvas context
  53. this.t = 0; // touches index
  54. this.isInit = false;
  55. this.fgColor = null; // main color
  56. this.pColor = null; // previous color
  57. this.dH = null; // draw hook
  58. this.cH = null; // change hook
  59. this.eH = null; // cancel hook
  60. this.rH = null; // release hook
  61. this.run = function () {
  62. var cf = function (e, conf) {
  63. var k;
  64. for (k in conf) {
  65. s.o[k] = conf[k];
  66. }
  67. s.init();
  68. s._configure()
  69. ._draw();
  70. };
  71. if(this.$.data('kontroled')) return;
  72. this.$.data('kontroled', true);
  73. this.extend();
  74. this.o = $.extend(
  75. {
  76. // Config
  77. min : this.$.data('min') || 0,
  78. max : this.$.data('max') || 100,
  79. stopper : true,
  80. readOnly : this.$.data('readonly'),
  81. // UI
  82. cursor : (this.$.data('cursor') === true && 30)
  83. || this.$.data('cursor')
  84. || 0,
  85. thickness : this.$.data('thickness') || 0.35,
  86. lineCap : this.$.data('linecap') || 'butt',
  87. width : this.$.data('width') || 200,
  88. height : this.$.data('height') || 200,
  89. displayInput : this.$.data('displayinput') == null || this.$.data('displayinput'),
  90. displayPrevious : this.$.data('displayprevious'),
  91. fgColor : this.$.data('fgcolor') || '#87CEEB',
  92. inputColor: this.$.data('inputcolor') || this.$.data('fgcolor') || '#87CEEB',
  93. inline : false,
  94. step : this.$.data('step') || 1,
  95. // Hooks
  96. draw : null, // function () {}
  97. change : null, // function (value) {}
  98. cancel : null, // function () {}
  99. release : null // function (value) {}
  100. }, this.o
  101. );
  102. // routing value
  103. if(this.$.is('fieldset')) {
  104. // fieldset = array of integer
  105. this.v = {};
  106. this.i = this.$.find('input')
  107. this.i.each(function(k) {
  108. var $this = $(this);
  109. s.i[k] = $this;
  110. s.v[k] = $this.val();
  111. $this.bind(
  112. 'change'
  113. , function () {
  114. var val = {};
  115. val[k] = $this.val();
  116. s.val(val);
  117. }
  118. );
  119. });
  120. this.$.find('legend').remove();
  121. } else {
  122. // input = integer
  123. this.i = this.$;
  124. this.v = this.$.val();
  125. (this.v == '') && (this.v = this.o.min);
  126. this.$.bind(
  127. 'change'
  128. , function () {
  129. s.val(s._validate(s.$.val()));
  130. }
  131. );
  132. }
  133. (!this.o.displayInput) && this.$.hide();
  134. this.$c = $('<canvas width="' +
  135. this.o.width + 'px" height="' +
  136. this.o.height + 'px"></canvas>');
  137. this.c = this.$c[0].getContext("2d");
  138. this.$
  139. .wrap($('<div style="' + (this.o.inline ? 'display:inline;' : '') +
  140. 'width:' + this.o.width + 'px;height:' +
  141. this.o.height + 'px;"></div>'))
  142. .before(this.$c);
  143. if (this.v instanceof Object) {
  144. this.cv = {};
  145. this.copy(this.v, this.cv);
  146. } else {
  147. this.cv = this.v;
  148. }
  149. this.$
  150. .bind("configure", cf)
  151. .parent()
  152. .bind("configure", cf);
  153. this._listen()
  154. ._configure()
  155. ._xy()
  156. .init();
  157. this.isInit = true;
  158. this._draw();
  159. return this;
  160. };
  161. this._draw = function () {
  162. // canvas pre-rendering
  163. var d = true,
  164. c = document.createElement('canvas');
  165. c.width = s.o.width;
  166. c.height = s.o.height;
  167. s.g = c.getContext('2d');
  168. s.clear();
  169. s.dH
  170. && (d = s.dH());
  171. (d !== false) && s.draw();
  172. s.c.drawImage(c, 0, 0);
  173. c = null;
  174. };
  175. this._touch = function (e) {
  176. var touchMove = function (e) {
  177. var v = s.xy2val(
  178. e.originalEvent.touches[s.t].pageX,
  179. e.originalEvent.touches[s.t].pageY
  180. );
  181. if (v == s.cv) return;
  182. if (
  183. s.cH
  184. && (s.cH(v) === false)
  185. ) return;
  186. s.change(s._validate(v));
  187. s._draw();
  188. };
  189. // get touches index
  190. this.t = k.c.t(e);
  191. // First touch
  192. touchMove(e);
  193. // Touch events listeners
  194. k.c.d
  195. .bind("touchmove.k", touchMove)
  196. .bind(
  197. "touchend.k"
  198. , function () {
  199. k.c.d.unbind('touchmove.k touchend.k');
  200. if (
  201. s.rH
  202. && (s.rH(s.cv) === false)
  203. ) return;
  204. s.val(s.cv);
  205. }
  206. );
  207. return this;
  208. };
  209. this._mouse = function (e) {
  210. var mouseMove = function (e) {
  211. var v = s.xy2val(e.pageX, e.pageY);
  212. if (v == s.cv) return;
  213. if (
  214. s.cH
  215. && (s.cH(v) === false)
  216. ) return;
  217. s.change(s._validate(v));
  218. s._draw();
  219. };
  220. // First click
  221. mouseMove(e);
  222. // Mouse events listeners
  223. k.c.d
  224. .bind("mousemove.k", mouseMove)
  225. .bind(
  226. // Escape key cancel current change
  227. "keyup.k"
  228. , function (e) {
  229. if (e.keyCode === 27) {
  230. k.c.d.unbind("mouseup.k mousemove.k keyup.k");
  231. if (
  232. s.eH
  233. && (s.eH() === false)
  234. ) return;
  235. s.cancel();
  236. }
  237. }
  238. )
  239. .bind(
  240. "mouseup.k"
  241. , function (e) {
  242. k.c.d.unbind('mousemove.k mouseup.k keyup.k');
  243. if (
  244. s.rH
  245. && (s.rH(s.cv) === false)
  246. ) return;
  247. s.val(s.cv);
  248. }
  249. );
  250. return this;
  251. };
  252. this._xy = function () {
  253. var o = this.$c.offset();
  254. this.x = o.left;
  255. this.y = o.top;
  256. return this;
  257. };
  258. this._listen = function () {
  259. if (!this.o.readOnly) {
  260. this.$c
  261. .bind(
  262. "mousedown"
  263. , function (e) {
  264. e.preventDefault();
  265. s._xy()._mouse(e);
  266. }
  267. )
  268. .bind(
  269. "touchstart"
  270. , function (e) {
  271. e.preventDefault();
  272. s._xy()._touch(e);
  273. }
  274. );
  275. this.listen();
  276. } else {
  277. this.$.attr('readonly', 'readonly');
  278. }
  279. return this;
  280. };
  281. this._configure = function () {
  282. // Hooks
  283. if (this.o.draw) this.dH = this.o.draw;
  284. if (this.o.change) this.cH = this.o.change;
  285. if (this.o.cancel) this.eH = this.o.cancel;
  286. if (this.o.release) this.rH = this.o.release;
  287. if (this.o.displayPrevious) {
  288. this.pColor = this.h2rgba(this.o.fgColor, "0.4");
  289. this.fgColor = this.h2rgba(this.o.fgColor, "0.6");
  290. } else {
  291. this.fgColor = this.o.fgColor;
  292. }
  293. return this;
  294. };
  295. this._clear = function () {
  296. this.$c[0].width = this.$c[0].width;
  297. };
  298. this._validate = function(v) {
  299. return (~~ (((v < 0) ? -0.5 : 0.5) + (v/this.o.step))) * this.o.step;
  300. };
  301. // Abstract methods
  302. this.listen = function () {}; // on start, one time
  303. this.extend = function () {}; // each time configure triggered
  304. this.init = function () {}; // each time configure triggered
  305. this.change = function (v) {}; // on change
  306. this.val = function (v) {}; // on release
  307. this.xy2val = function (x, y) {}; //
  308. this.draw = function () {}; // on change / on release
  309. this.clear = function () { this._clear(); };
  310. // Utils
  311. this.h2rgba = function (h, a) {
  312. var rgb;
  313. h = h.substring(1,7)
  314. rgb = [parseInt(h.substring(0,2),16)
  315. ,parseInt(h.substring(2,4),16)
  316. ,parseInt(h.substring(4,6),16)];
  317. return "rgba(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + "," + a + ")";
  318. };
  319. this.copy = function (f, t) {
  320. for (var i in f) { t[i] = f[i]; }
  321. };
  322. };
  323. /**
  324. * k.Dial
  325. */
  326. k.Dial = function () {
  327. k.o.call(this);
  328. this.startAngle = null;
  329. this.xy = null;
  330. this.radius = null;
  331. this.lineWidth = null;
  332. this.cursorExt = null;
  333. this.w2 = null;
  334. this.PI2 = 2*Math.PI;
  335. this.extend = function () {
  336. this.o = $.extend(
  337. {
  338. bgColor : this.$.data('bgcolor') || '#EEEEEE',
  339. angleOffset : this.$.data('angleoffset') || 0,
  340. angleArc : this.$.data('anglearc') || 360,
  341. inline : true
  342. }, this.o
  343. );
  344. };
  345. this.val = function (v) {
  346. if (null != v) {
  347. this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v;
  348. this.v = this.cv;
  349. this.$.val(this.v);
  350. this._draw();
  351. } else {
  352. return this.v;
  353. }
  354. };
  355. this.xy2val = function (x, y) {
  356. var a, ret;
  357. a = Math.atan2(
  358. x - (this.x + this.w2)
  359. , - (y - this.y - this.w2)
  360. ) - this.angleOffset;
  361. if(this.angleArc != this.PI2 && (a < 0) && (a > -0.5)) {
  362. // if isset angleArc option, set to min if .5 under min
  363. a = 0;
  364. } else if (a < 0) {
  365. a += this.PI2;
  366. }
  367. ret = ~~ (0.5 + (a * (this.o.max - this.o.min) / this.angleArc))
  368. + this.o.min;
  369. this.o.stopper
  370. && (ret = max(min(ret, this.o.max), this.o.min));
  371. return ret;
  372. };
  373. this.listen = function () {
  374. // bind MouseWheel
  375. var s = this,
  376. mw = function (e) {
  377. e.preventDefault();
  378. var ori = e.originalEvent
  379. ,deltaX = ori.detail || ori.wheelDeltaX
  380. ,deltaY = ori.detail || ori.wheelDeltaY
  381. ,v = parseInt(s.$.val()) + (deltaX>0 || deltaY>0 ? s.o.step : deltaX<0 || deltaY<0 ? -s.o.step : 0);
  382. if (
  383. s.cH
  384. && (s.cH(v) === false)
  385. ) return;
  386. s.val(v);
  387. }
  388. , kval, to, m = 1, kv = {37:-s.o.step, 38:s.o.step, 39:s.o.step, 40:-s.o.step};
  389. this.$
  390. .bind(
  391. "keydown"
  392. ,function (e) {
  393. var kc = e.keyCode;
  394. // numpad support
  395. if(kc >= 96 && kc <= 105) {
  396. kc = e.keyCode = kc - 48;
  397. }
  398. kval = parseInt(String.fromCharCode(kc));
  399. if (isNaN(kval)) {
  400. (kc !== 13) // enter
  401. && (kc !== 8) // bs
  402. && (kc !== 9) // tab
  403. && (kc !== 189) // -
  404. && e.preventDefault();
  405. // arrows
  406. if ($.inArray(kc,[37,38,39,40]) > -1) {
  407. e.preventDefault();
  408. var v = parseInt(s.$.val()) + kv[kc] * m;
  409. s.o.stopper
  410. && (v = max(min(v, s.o.max), s.o.min));
  411. s.change(v);
  412. s._draw();
  413. // long time keydown speed-up
  414. to = window.setTimeout(
  415. function () { m*=2; }
  416. ,30
  417. );
  418. }
  419. }
  420. }
  421. )
  422. .bind(
  423. "keyup"
  424. ,function (e) {
  425. if (isNaN(kval)) {
  426. if (to) {
  427. window.clearTimeout(to);
  428. to = null;
  429. m = 1;
  430. s.val(s.$.val());
  431. }
  432. } else {
  433. // kval postcond
  434. (s.$.val() > s.o.max && s.$.val(s.o.max))
  435. || (s.$.val() < s.o.min && s.$.val(s.o.min));
  436. }
  437. }
  438. );
  439. this.$c.bind("mousewheel DOMMouseScroll", mw);
  440. this.$.bind("mousewheel DOMMouseScroll", mw)
  441. };
  442. this.init = function () {
  443. if (
  444. this.v < this.o.min
  445. || this.v > this.o.max
  446. ) this.v = this.o.min;
  447. this.$.val(this.v);
  448. this.w2 = this.o.width / 2;
  449. this.cursorExt = this.o.cursor / 100;
  450. this.xy = this.w2;
  451. this.lineWidth = this.xy * this.o.thickness;
  452. this.lineCap = this.o.lineCap;
  453. this.radius = this.xy - this.lineWidth / 2;
  454. this.o.angleOffset
  455. && (this.o.angleOffset = isNaN(this.o.angleOffset) ? 0 : this.o.angleOffset);
  456. this.o.angleArc
  457. && (this.o.angleArc = isNaN(this.o.angleArc) ? this.PI2 : this.o.angleArc);
  458. // deg to rad
  459. this.angleOffset = this.o.angleOffset * Math.PI / 180;
  460. this.angleArc = this.o.angleArc * Math.PI / 180;
  461. // compute start and end angles
  462. this.startAngle = 1.5 * Math.PI + this.angleOffset;
  463. this.endAngle = 1.5 * Math.PI + this.angleOffset + this.angleArc;
  464. var s = max(
  465. String(Math.abs(this.o.max)).length
  466. , String(Math.abs(this.o.min)).length
  467. , 2
  468. ) + 2;
  469. this.o.displayInput
  470. && this.i.css({
  471. 'width' : ((this.o.width / 2 + 4) >> 0) + 'px'
  472. ,'height' : ((this.o.width / 3) >> 0) + 'px'
  473. ,'position' : 'absolute'
  474. ,'vertical-align' : 'middle'
  475. ,'margin-top' : ((this.o.width / 3) >> 0) + 'px'
  476. ,'margin-left' : '-' + ((this.o.width * 3 / 4 + 2) >> 0) + 'px'
  477. ,'border' : 0
  478. ,'background' : 'none'
  479. ,'font' : 'bold ' + ((this.o.width / s) >> 0) + 'px Arial'
  480. ,'text-align' : 'center'
  481. ,'color' : this.o.inputColor || this.o.fgColor
  482. ,'padding' : '0px'
  483. ,'-webkit-appearance': 'none'
  484. })
  485. || this.i.css({
  486. 'width' : '0px'
  487. ,'visibility' : 'hidden'
  488. });
  489. };
  490. this.change = function (v) {
  491. this.cv = v;
  492. this.$.val(v);
  493. };
  494. this.angle = function (v) {
  495. return (v - this.o.min) * this.angleArc / (this.o.max - this.o.min);
  496. };
  497. this.draw = function () {
  498. var c = this.g, // context
  499. a = this.angle(this.cv) // Angle
  500. , sat = this.startAngle // Start angle
  501. , eat = sat + a // End angle
  502. , sa, ea // Previous angles
  503. , r = 1;
  504. c.lineWidth = this.lineWidth;
  505. c.lineCap = this.lineCap;
  506. this.o.cursor
  507. && (sat = eat - this.cursorExt)
  508. && (eat = eat + this.cursorExt);
  509. c.beginPath();
  510. c.strokeStyle = this.o.bgColor;
  511. c.arc(this.xy, this.xy, this.radius, this.endAngle, this.startAngle, true);
  512. c.stroke();
  513. if (this.o.displayPrevious) {
  514. ea = this.startAngle + this.angle(this.v);
  515. sa = this.startAngle;
  516. this.o.cursor
  517. && (sa = ea - this.cursorExt)
  518. && (ea = ea + this.cursorExt);
  519. c.beginPath();
  520. c.strokeStyle = this.pColor;
  521. c.arc(this.xy, this.xy, this.radius, sa, ea, false);
  522. c.stroke();
  523. r = (this.cv == this.v);
  524. }
  525. c.beginPath();
  526. c.strokeStyle = r ? this.o.fgColor : this.fgColor ;
  527. c.arc(this.xy, this.xy, this.radius, sat, eat, false);
  528. c.stroke();
  529. };
  530. this.cancel = function () {
  531. this.val(this.v);
  532. };
  533. };
  534. $.fn.dial = $.fn.knob = function (o) {
  535. return this.each(
  536. function () {
  537. var d = new k.Dial();
  538. d.o = o;
  539. d.$ = $(this);
  540. d.run();
  541. }
  542. ).parent();
  543. };
  544. })(jQuery);