lodlive.custom-lines.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (function($) {
  2. var methods = {
  3. isSameAsLine : function(label, x1, y1, x2, y2, canvas, toId) {
  4. // eseguo i calcoli e scrivo la riga di connessione tra i cerchi
  5. var lineangle = (Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI) + 180;
  6. var x2bis = x1 - Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) + 60;
  7. //canvas.detectPixelRatio();
  8. canvas.rotateCanvas({
  9. rotate : lineangle,
  10. x : x1,
  11. y : y1
  12. }).drawLine({
  13. strokeStyle : "#000",
  14. strokeWidth : 1,
  15. strokeCap : 'bevel',
  16. x1 : x1 - 60,
  17. y1 : y1,
  18. x2 : x2bis,
  19. y2 : y1
  20. });
  21. if (lineangle > 90 && lineangle < 270) {
  22. canvas.rotateCanvas({
  23. rotate : 180,
  24. x : (x2bis + x1) / 2,
  25. y : (y1 + y1) / 2
  26. });
  27. }
  28. label = $.trim(label).replace(/\n/g, ', ');
  29. canvas.drawText({// inserisco l'etichetta
  30. fillStyle : "#000",
  31. strokeStyle : "#000",
  32. x : (x2bis + x1 + ((x1 + 60) > x2 ? -60 : +60)) / 2,
  33. y : (y1 + y1 - ((x1 + 60) > x2 ? 18 : -18)) / 2,
  34. text : ((x1 + 60) > x2 ? " « " : "") + label + ((x1 + 60) > x2 ? "" : " » "),
  35. align : "center",
  36. strokeWidth : 0.01,
  37. fontSize : 11,
  38. fontFamily : "'Open Sans',Verdana"
  39. }).restoreCanvas().restoreCanvas();
  40. // ed inserisco la freccia per determinarne il verso della
  41. // relazione
  42. lineangle = Math.atan2(y2 - y1, x2 - x1);
  43. var angle = 0.79;
  44. var h = Math.abs(8 / Math.cos(angle));
  45. var fromx = x2 - 60 * Math.cos(lineangle);
  46. var fromy = y2 - 60 * Math.sin(lineangle);
  47. var angle1 = lineangle + Math.PI + angle;
  48. var topx = (x2 + Math.cos(angle1) * h) - 60 * Math.cos(lineangle);
  49. var topy = (y2 + Math.sin(angle1) * h) - 60 * Math.sin(lineangle);
  50. var angle2 = lineangle + Math.PI - angle;
  51. var botx = (x2 + Math.cos(angle2) * h) - 60 * Math.cos(lineangle);
  52. var boty = (y2 + Math.sin(angle2) * h) - 60 * Math.sin(lineangle);
  53. canvas.drawLine({
  54. strokeStyle : "#000",
  55. strokeWidth : 1,
  56. x1 : fromx,
  57. y1 : fromy,
  58. x2 : botx,
  59. y2 : boty
  60. });
  61. canvas.drawLine({
  62. strokeStyle : "#000",
  63. strokeWidth : 1,
  64. x1 : fromx,
  65. y1 : fromy,
  66. x2 : topx,
  67. y2 : topy
  68. });
  69. }
  70. };
  71. $.fn.customLines = function(context, method) {
  72. if (methods[method]) {
  73. return methods[method].apply(this, Array.prototype.slice.call(arguments, 2));
  74. } else if ( typeof method === 'object' || !method) {
  75. return methods.init.apply(this, arguments);
  76. } else {
  77. var args = Array.prototype.slice.call(arguments, 2);
  78. args.unshift('standardLine');
  79. context.lodlive.apply(null, args);
  80. }
  81. };
  82. })(jQuery);