sausage.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var _graphic = require("../graphic");
  20. var extendShape = _graphic.extendShape;
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. /**
  40. * Sausage: similar to sector, but have half circle on both sides
  41. * @public
  42. */
  43. var _default = extendShape({
  44. type: 'sausage',
  45. shape: {
  46. cx: 0,
  47. cy: 0,
  48. r0: 0,
  49. r: 0,
  50. startAngle: 0,
  51. endAngle: Math.PI * 2,
  52. clockwise: true
  53. },
  54. buildPath: function (ctx, shape) {
  55. var x = shape.cx;
  56. var y = shape.cy;
  57. var r0 = Math.max(shape.r0 || 0, 0);
  58. var r = Math.max(shape.r, 0);
  59. var dr = (r - r0) * 0.5;
  60. var rCenter = r0 + dr;
  61. var startAngle = shape.startAngle;
  62. var endAngle = shape.endAngle;
  63. var clockwise = shape.clockwise;
  64. var unitStartX = Math.cos(startAngle);
  65. var unitStartY = Math.sin(startAngle);
  66. var unitEndX = Math.cos(endAngle);
  67. var unitEndY = Math.sin(endAngle);
  68. var lessThanCircle = clockwise ? endAngle - startAngle < Math.PI * 2 : startAngle - endAngle < Math.PI * 2;
  69. if (lessThanCircle) {
  70. ctx.moveTo(unitStartX * r0 + x, unitStartY * r0 + y);
  71. ctx.arc(unitStartX * rCenter + x, unitStartY * rCenter + y, dr, -Math.PI + startAngle, startAngle, !clockwise);
  72. }
  73. ctx.arc(x, y, r, startAngle, endAngle, !clockwise);
  74. ctx.moveTo(unitEndX * r + x, unitEndY * r + y);
  75. ctx.arc(unitEndX * rCenter + x, unitEndY * rCenter + y, dr, endAngle - Math.PI * 2, endAngle - Math.PI, !clockwise);
  76. if (r0 !== 0) {
  77. ctx.arc(x, y, r0, endAngle, startAngle, clockwise);
  78. ctx.moveTo(unitStartX * r0 + x, unitEndY * r0 + y);
  79. }
  80. ctx.closePath();
  81. }
  82. });
  83. module.exports = _default;