SGraphyPolygonItem.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 不规则多边形,元空间
  3. */
  4. import SGraphyItem from '../SGraphyItem'
  5. import SRect from './../types/SRect';
  6. export default class SGraphyPolygonItem extends SGraphyItem {
  7. /**
  8. *
  9. * 构造函数
  10. *
  11. * @param jsonArr 空间线条数组
  12. * @param lineWidth 空间线条的宽度
  13. * @param color 空间线条的颜色
  14. * @param fillColor 空间的填充颜色
  15. *
  16. */
  17. constructor(jsonArr, lineWidth, color, fillColor, parent = null) {
  18. super(parent)
  19. this.jsonArr = jsonArr
  20. this.lineWidth = lineWidth
  21. this.color = color
  22. this.fillColor = fillColor
  23. } //constructor
  24. /**
  25. * Item的边界区域
  26. *
  27. * @return SRect
  28. */
  29. boundingRect() {
  30. return new SRect(0, 0, this.width, this.height)
  31. }
  32. /**
  33. * 绘制不规则多边形
  34. *
  35. * @param canvas 画布
  36. * @param rect 绘制区域
  37. */
  38. onDraw(canvas, rect) {
  39. if (this.jsonArr && this.jsonArr.length) {
  40. canvas.beginPath();
  41. canvas.lineWidth = this.lineWidth || 1
  42. canvas.strokeStyle = this.color || '#000'
  43. canvas.fillStyle = this.fillColor || '#fff'
  44. canvas.moveTo(this.jsonArr[0].X / 120 * -1, this.jsonArr[0].Y / 120)
  45. for (let i = 1; i < this.jsonArr.length - 1; i++) {
  46. canvas.lineTo(this.jsonArr[i].X / 120 * -1, this.jsonArr[i].Y / 120)
  47. }
  48. canvas.lineTo(this.jsonArr[0].X / 120 * -1, this.jsonArr[0].Y / 120)
  49. canvas.closePath()
  50. canvas.fill()
  51. canvas.stroke()
  52. }
  53. }
  54. }