mxRuler.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * Copyright (c) 2017, CTI LOGIC
  3. * Copyright (c) 2006-2017, JGraph Ltd
  4. * Copyright (c) 2006-2017, Gaudenz Alder
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11. *
  12. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  15. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  17. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. var mxRuler = function(graph, container, isVertical)
  20. {
  21. var ruler = this;
  22. var canvas = document.createElement("canvas");
  23. //initial sizing which is corrected by the graph size event
  24. canvas.width = container.offsetWidth;
  25. canvas.height = container.offsetHeight;
  26. container.style.overflow = 'hidden';
  27. canvas.style.position = "relative";
  28. container.appendChild(canvas);
  29. //Disable alpha to improve performance as we don't need it
  30. var ctx = canvas.getContext("2d"/*, {alpha: false}*/);
  31. this.graph = graph;
  32. this.container = container;
  33. this.canvas = canvas;
  34. var drawLine = function (x1, y1, x2, y2, text)
  35. {
  36. //remove all fractions
  37. x1 = Math.round(x1); y1 = Math.round(y1); x2 = Math.round(x2); y2 = Math.round(y2);
  38. //adding the 0.5 is necessary to prevent anti-aliasing from making lines thicker!
  39. ctx.moveTo(x1 + 0.5, y1 + 0.5);
  40. ctx.lineTo(x2 + 0.5, y2 + 0.5);
  41. ctx.stroke();
  42. if (text)
  43. {
  44. if (isVertical)
  45. {
  46. var x = x1;
  47. var y = y1 - 3;
  48. var metric = ctx.measureText(text);
  49. ctx.save();
  50. // We want to find the center of the text (or whatever point you want) and rotate about it
  51. var tx = x + (metric.width / 2) + 8;
  52. var ty = y + 5;
  53. // Translate to near the center to rotate about the center
  54. ctx.translate(tx, ty);
  55. // Then rotate...
  56. ctx.rotate(-Math.PI / 2);
  57. // Then translate back to draw in the right place!
  58. ctx.translate(-tx, -ty);
  59. ctx.fillText(text, x, y);
  60. ctx.restore();
  61. }
  62. else
  63. {
  64. ctx.fillText(text, isVertical? x1 : x1 + 3, isVertical? y1 - 3 : y1 + 9);
  65. }
  66. }
  67. };
  68. var drawRuler = function(forceErase)
  69. {
  70. //The area is automatically cleared when the canvas size is changed
  71. if (forceErase) ctx.clearRect(0, 0, canvas.width, canvas.height);
  72. ctx.beginPath();
  73. ctx.lineWidth = 0.1;
  74. ctx.strokeStyle = "#BBBBBB";
  75. ctx.font = "9px Arial";
  76. ctx.fillStyle = '#BBBBBB';
  77. var scale = graph.view.scale;
  78. var bgPages = graph.view.getBackgroundPageBounds();
  79. var t = graph.view.translate;
  80. var bounds = graph.view.getGraphBounds();
  81. var rStart = (isVertical? bgPages.y : bgPages.x);
  82. //handle negative pages
  83. if (isVertical)
  84. {
  85. var y = ((bounds.y) / scale - t.y);
  86. if (y <= -1)
  87. {
  88. rStart += Math.ceil(Math.abs(y) / graph.pageFormat.height) * graph.pageFormat.height * scale;
  89. }
  90. }
  91. else
  92. {
  93. var x = ((bounds.x) / scale - t.x);
  94. if (x <= -1)
  95. {
  96. rStart += Math.ceil(Math.abs(x) / graph.pageFormat.width) * graph.pageFormat.width * scale;
  97. }
  98. }
  99. rStart += isVertical? (graph.container.offsetTop - ruler.container.offsetTop) : (graph.container.offsetLeft - ruler.container.offsetLeft);
  100. var tickStep, tickSize, len;
  101. switch(ruler.unit)
  102. {
  103. case ruler.PIXELS:
  104. len = 10;
  105. tickStep = 10;
  106. tickSize = [25,5,5,5,5,10,5,5,5,5];
  107. break;
  108. case ruler.CENTIMETER:
  109. len = 10;
  110. tickStep = ruler.pPerCM/len;
  111. tickSize = [25,5,5,5,5,10,5,5,5,5];
  112. break;
  113. case ruler.INCHES:
  114. if (scale <=0.5 || scale >=4)
  115. len = 8;
  116. else
  117. len = 16;
  118. tickStep = ruler.pPerInch/len;
  119. tickSize = [25,5,8,5,12,5,8,5,15,5,8,5,12,5,8,5];
  120. break;
  121. }
  122. var step = tickStep;
  123. if (ruler.unit != ruler.INCHES || (scale > 2 || scale < 0.25))
  124. step = scale>= 1 ? (tickStep / Math.floor(scale)) : Math.floor(10 / scale / 10) * 10;
  125. for (var i = rStart % (step * scale); i <= (isVertical? canvas.height : canvas.width); i += step * scale)
  126. {
  127. var current = Math.round((i - rStart) / scale / step);
  128. var text = null;
  129. if (current % len == 0)
  130. {
  131. text = ruler.formatText(Math.round(current * step)) + "";
  132. }
  133. if (isVertical)
  134. {
  135. drawLine(30 - tickSize[Math.abs(current) % len], i, 30, i, text);
  136. }
  137. else
  138. {
  139. drawLine(i, 30 - tickSize[Math.abs(current) % len], i, 30, text);
  140. }
  141. }
  142. };
  143. var sizeListener = function()
  144. {
  145. var div = graph.container;
  146. var newW = isVertical? container.offsetWidth : div.scrollWidth;
  147. var newH = isVertical? div.scrollHeight : container.offsetHeight;
  148. if (newW != canvas.width || newH != canvas.height)
  149. {
  150. canvas.width = newW;
  151. canvas.height = newH;
  152. drawRuler();
  153. }
  154. };
  155. this.drawRuler = drawRuler;
  156. var efficientSizeListener = debounce(sizeListener, 10);
  157. this.sizeListener = efficientSizeListener;
  158. //Size event is called upon scaling so we may not need it
  159. //graph.view.addListener(mxEvent.SCALE, efficientSizeListener);
  160. graph.addListener(mxEvent.SIZE, efficientSizeListener);
  161. graph.container.addEventListener("scroll", function() {
  162. if (isVertical)
  163. {
  164. canvas.style.top = -graph.container.scrollTop + "px";
  165. }
  166. else
  167. {
  168. canvas.style.left = -graph.container.scrollLeft + "px";
  169. }
  170. });
  171. function debounce(func, wait, immediate)
  172. {
  173. var timeout;
  174. return function() {
  175. var context = this, args = arguments;
  176. var later = function() {
  177. timeout = null;
  178. if (!immediate) func.apply(context, args);
  179. };
  180. var callNow = immediate && !timeout;
  181. clearTimeout(timeout);
  182. timeout = setTimeout(later, wait);
  183. if (callNow) func.apply(context, args);
  184. };
  185. };
  186. function createHint()
  187. {
  188. var hint = document.createElement('div');
  189. hint.className = 'geHint';
  190. hint.style.whiteSpace = 'nowrap';
  191. hint.style.position = 'absolute';
  192. return hint;
  193. };
  194. graph.graphHandler.updateHint = function(me)
  195. {
  196. if (this.shape != null)
  197. {
  198. if (this.hint == null)
  199. {
  200. this.hint = createHint();
  201. this.graph.container.appendChild(this.hint);
  202. }
  203. var t = this.graph.view.translate;
  204. var s = this.graph.view.scale;
  205. var x = this.roundLength((this.bounds.x + this.currentDx) / s - t.x);
  206. var y = this.roundLength((this.bounds.y + this.currentDy) / s - t.y);
  207. this.hint.innerHTML = ruler.formatText(x) + ', ' + ruler.formatText(y); //Math.round(current * step)
  208. this.hint.style.left = (this.shape.bounds.x + Math.round((this.shape.bounds.width - this.hint.clientWidth) / 2)) + 'px';
  209. this.hint.style.top = (this.shape.bounds.y + this.shape.bounds.height + 12) + 'px';
  210. }
  211. };
  212. mxVertexHandler.prototype.updateHint = function(me)
  213. {
  214. if (this.index != mxEvent.LABEL_HANDLE)
  215. {
  216. if (this.hint == null)
  217. {
  218. this.hint = createHint();
  219. this.state.view.graph.container.appendChild(this.hint);
  220. }
  221. if (this.index == mxEvent.ROTATION_HANDLE)
  222. {
  223. this.hint.innerHTML = this.currentAlpha + '&deg;';
  224. }
  225. else
  226. {
  227. var s = this.state.view.scale;
  228. this.hint.innerHTML = ruler.formatText(this.roundLength(this.bounds.width / s)) + ' x ' + ruler.formatText(this.roundLength(this.bounds.height / s));
  229. }
  230. var rot = (this.currentAlpha != null) ? this.currentAlpha : this.state.style[mxConstants.STYLE_ROTATION] || '0';
  231. var bb = mxUtils.getBoundingBox(this.bounds, rot);
  232. if (bb == null)
  233. {
  234. bb = this.bounds;
  235. }
  236. this.hint.style.left = bb.x + Math.round((bb.width - this.hint.clientWidth) / 2) + 'px';
  237. this.hint.style.top = (bb.y + bb.height + 12) + 'px';
  238. }
  239. };
  240. mxEdgeHandler.prototype.updateHint = function(me, point)
  241. {
  242. if (this.hint == null)
  243. {
  244. this.hint = createHint();
  245. this.state.view.graph.container.appendChild(this.hint);
  246. }
  247. var t = this.graph.view.translate;
  248. var s = this.graph.view.scale;
  249. var x = this.roundLength(point.x / s - t.x);
  250. var y = this.roundLength(point.y / s - t.y);
  251. this.hint.innerHTML = ruler.formatText(x) + ', ' + ruler.formatText(y);
  252. this.hint.style.visibility = 'visible';
  253. if (this.isSource || this.isTarget)
  254. {
  255. if (this.constraintHandler.currentConstraint != null &&
  256. this.constraintHandler.currentFocus != null)
  257. {
  258. var pt = this.constraintHandler.currentConstraint.point;
  259. this.hint.innerHTML = '[' + Math.round(pt.x * 100) + '%, '+ Math.round(pt.y * 100) + '%]';
  260. }
  261. else if (this.marker.hasValidState())
  262. {
  263. this.hint.style.visibility = 'hidden';
  264. }
  265. }
  266. this.hint.style.left = Math.round(me.getGraphX() - this.hint.clientWidth / 2) + 'px';
  267. this.hint.style.top = (Math.max(me.getGraphY(), point.y) + this.state.view.graph.gridSize) + 'px';
  268. if (this.hideEdgeHintThread != null)
  269. {
  270. window.clearTimeout(this.hideEdgeHintThread);
  271. }
  272. this.hideEdgeHintThread = window.setTimeout(mxUtils.bind(this, function()
  273. {
  274. if (this.hint != null)
  275. {
  276. this.hint.style.visibility = 'hidden';
  277. }
  278. }), 500);
  279. };
  280. };
  281. mxRuler.prototype.PIXELS = 1;
  282. mxRuler.prototype.CENTIMETER = 2;
  283. mxRuler.prototype.INCHES = 3;
  284. mxRuler.prototype.pPerCM = 39.37;
  285. mxRuler.prototype.pPerInch = 100;
  286. mxRuler.prototype.unit = mxRuler.prototype.PIXELS;
  287. mxRuler.prototype.setUnit = function(unit) {
  288. this.unit = unit;
  289. };
  290. mxRuler.prototype.formatText = function(pixels) {
  291. switch(this.unit) {
  292. case this.PIXELS:
  293. return pixels;
  294. case this.CENTIMETER:
  295. return (pixels / this.pPerCM).toFixed(2);
  296. case this.INCHES:
  297. return (pixels / this.pPerInch).toFixed(2);
  298. }
  299. };
  300. //TODO fix this (put correct listeners)
  301. mxRuler.prototype.destroy = function() {
  302. this.graph.view.removeListener(this.drawRuler);
  303. this.graph.removeListener(this.sizeListener);
  304. this.graph.container.removeEventListener("scroll", this.drawRuler);
  305. this.graph.view.removeListener(mxEvent.SCALE, this.drawRuler);
  306. if (this.container != null && this.canvas != null) this.container.removeChild(this.canvas);
  307. };