jquery.knob.js 20 KB

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