mxCylinder.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * Variable: svgStrokeTolerance
  48. *
  49. * Sets stroke tolerance to 0 for SVG.
  50. */
  51. mxCylinder.prototype.svgStrokeTolerance = 0;
  52. /**
  53. * Function: paintVertexShape
  54. *
  55. * Redirects to redrawPath for subclasses to work.
  56. */
  57. mxCylinder.prototype.paintVertexShape = function(c, x, y, w, h)
  58. {
  59. c.translate(x, y);
  60. c.begin();
  61. this.redrawPath(c, x, y, w, h, false);
  62. c.fillAndStroke();
  63. if (!this.outline || this.style == null || mxUtils.getValue(
  64. this.style, mxConstants.STYLE_BACKGROUND_OUTLINE, 0) == 0)
  65. {
  66. c.setShadow(false);
  67. c.begin();
  68. this.redrawPath(c, x, y, w, h, true);
  69. c.stroke();
  70. }
  71. };
  72. /**
  73. * Function: getCylinderSize
  74. *
  75. * Returns the cylinder size.
  76. */
  77. mxCylinder.prototype.getCylinderSize = function(x, y, w, h)
  78. {
  79. return Math.min(this.maxHeight, Math.round(h / 5));
  80. };
  81. /**
  82. * Function: redrawPath
  83. *
  84. * Draws the path for this shape.
  85. */
  86. mxCylinder.prototype.redrawPath = function(c, x, y, w, h, isForeground)
  87. {
  88. var dy = this.getCylinderSize(x, y, w, h);
  89. if ((isForeground && this.fill != null) || (!isForeground && this.fill == null))
  90. {
  91. c.moveTo(0, dy);
  92. c.curveTo(0, 2 * dy, w, 2 * dy, w, dy);
  93. // Needs separate shapes for correct hit-detection
  94. if (!isForeground)
  95. {
  96. c.stroke();
  97. c.begin();
  98. }
  99. }
  100. if (!isForeground)
  101. {
  102. c.moveTo(0, dy);
  103. c.curveTo(0, -dy / 3, w, -dy / 3, w, dy);
  104. c.lineTo(w, h - dy);
  105. c.curveTo(w, h + dy / 3, 0, h + dy / 3, 0, h - dy);
  106. c.close();
  107. }
  108. };
  109. __mxOutput.mxCylinder = typeof mxCylinder !== 'undefined' ? mxCylinder : undefined;