mxCylinder.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxCylinder
  7. *
  8. * Extends <mxShape> to implement an cylinder shape. If a
  9. * custom shape with one filled area and an overlay path is
  10. * needed, then this shape's <redrawPath> should be overridden.
  11. * This shape is registered under <mxConstants.SHAPE_CYLINDER>
  12. * in <mxCellRenderer>.
  13. *
  14. * Constructor: mxCylinder
  15. *
  16. * Constructs a new cylinder shape.
  17. *
  18. * Parameters:
  19. *
  20. * bounds - <mxRectangle> that defines the bounds. This is stored in
  21. * <mxShape.bounds>.
  22. * fill - String that defines the fill color. This is stored in <fill>.
  23. * stroke - String that defines the stroke color. This is stored in <stroke>.
  24. * strokewidth - Optional integer that defines the stroke width. Default is
  25. * 1. This is stored in <strokewidth>.
  26. */
  27. function mxCylinder(bounds, fill, stroke, strokewidth)
  28. {
  29. mxShape.call(this);
  30. this.bounds = bounds;
  31. this.fill = fill;
  32. this.stroke = stroke;
  33. this.strokewidth = (strokewidth != null) ? strokewidth : 1;
  34. };
  35. /**
  36. * Extends mxShape.
  37. */
  38. mxUtils.extend(mxCylinder, mxShape);
  39. /**
  40. * Variable: maxHeight
  41. *
  42. * Defines the maximum height of the top and bottom part
  43. * of the cylinder shape.
  44. */
  45. mxCylinder.prototype.maxHeight = 40;
  46. /**
  47. * Function: paintVertexShape
  48. *
  49. * Redirects to redrawPath for subclasses to work.
  50. */
  51. mxCylinder.prototype.paintVertexShape = function(c, x, y, w, h)
  52. {
  53. c.translate(x, y);
  54. c.begin();
  55. this.redrawPath(c, x, y, w, h, false);
  56. c.fillAndStroke();
  57. if (!this.outline || this.style == null || mxUtils.getValue(
  58. this.style, mxConstants.STYLE_BACKGROUND_OUTLINE, 0) == 0)
  59. {
  60. c.setShadow(false);
  61. c.begin();
  62. this.redrawPath(c, x, y, w, h, true);
  63. c.stroke();
  64. }
  65. };
  66. /**
  67. * Function: getCylinderSize
  68. *
  69. * Returns the cylinder size.
  70. */
  71. mxCylinder.prototype.getCylinderSize = function(x, y, w, h)
  72. {
  73. return Math.min(this.maxHeight, Math.round(h / 5));
  74. };
  75. /**
  76. * Function: redrawPath
  77. *
  78. * Draws the path for this shape.
  79. */
  80. mxCylinder.prototype.redrawPath = function(c, x, y, w, h, isForeground)
  81. {
  82. var dy = this.getCylinderSize(x, y, w, h);
  83. if ((isForeground && this.fill != null) || (!isForeground && this.fill == null))
  84. {
  85. c.moveTo(0, dy);
  86. c.curveTo(0, 2 * dy, w, 2 * dy, w, dy);
  87. // Needs separate shapes for correct hit-detection
  88. if (!isForeground)
  89. {
  90. c.stroke();
  91. c.begin();
  92. }
  93. }
  94. if (!isForeground)
  95. {
  96. c.moveTo(0, dy);
  97. c.curveTo(0, -dy / 3, w, -dy / 3, w, dy);
  98. c.lineTo(w, h - dy);
  99. c.curveTo(w, h + dy / 3, 0, h + dy / 3, 0, h - dy);
  100. c.close();
  101. }
  102. };