ENewspaperItemComponent.ets 4.79 KB
import { Action, NewspaperListItemBean, NewspaperPositionItemBean, Params } from 'wdBean';
import { ExtraDTO } from 'wdBean/src/main/ets/bean/component/extra/ExtraDTO';
import { StringUtils } from 'wdKit';
import { WDRouterRule } from 'wdRouter';

@Component
export struct ENewspaperItemComponent {
  private newspaperListItemBean: NewspaperListItemBean = {} as NewspaperListItemBean
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  private startX: number = 0
  private startY: number = 0
  private itemBeanClicked: NewspaperPositionItemBean = {} as NewspaperPositionItemBean

  build() {
    Stack() {
      Image(this.newspaperListItemBean.pagePic)
        .width('100%')
        .aspectRatio(378 / 566)
        .objectFit(ImageFit.Fill)

      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Transparent)
        .onReady(() => {

        })
    }
    .padding({ top: 16, right: 16, bottom: 16, left: 16 })
    .margin({ left: 10, right: 10 })
    .backgroundColor(Color.White)
    .width('100%')
    .onTouch((event: TouchEvent) => {
      if (event.type === TouchType.Down) {
        let x = event.touches[0].x;
        let y = event.touches[0].y;
        this.startX = x;
        this.startY = y;
        let points: number[][] = this.getArea(x, y, this.newspaperListItemBean.items);
        if (points && points.length > 2) {
          let path = new Path2D();
          path.moveTo(px2vp(points[0][0]), px2vp(points[0][1]));
          for (let point of points.slice(1, points.length)) {
            path.lineTo(px2vp(point[0]), px2vp(point[1]));
          }
          path.closePath();
          // 设定填充色为蓝色
          this.context.fillStyle = '#33000000';
          // 使用填充的方式,将Path2D描述的五边形绘制在canvas组件内部
          this.context.fill(path);
        }

      }
      if (event.type === TouchType.Up) {
        this.context.clearRect(0, 0, this.context.width, this.context.height)
        if (this.itemBeanClicked && this.itemBeanClicked.newsId) {
          let taskAction: Action = {
            type: 'JUMP_INNER_NEW_PAGE',
            params: {
              contentID: '30001758346',
              pageID: 'IMAGE_TEXT_DETAIL',
              extra: {
                relType: '1',
                relId: '500000001277',
                sourcePage: '5'
              } as ExtraDTO
            } as Params,
          };
          WDRouterRule.jumpWithAction(taskAction)

          this.itemBeanClicked = {} as NewspaperPositionItemBean
        }
      }
      if (event.type === TouchType.Move) {
        let mx = event.touches[0].x;
        let my = event.touches[0].y;
        if (this.startX - mx > 5 || mx - this.startX > 5 || this.startY - my > 5 || my - this.startY > 5) {
          this.itemBeanClicked = {} as NewspaperPositionItemBean
          this.context.clearRect(0, 0, this.context.width, this.context.height)
        }

      }
    })
  }

  public getArea(x: number, y: number, itemBeans: NewspaperPositionItemBean[]): number[][] {
    if (itemBeans && itemBeans.length > 0) {
      for (let itemBean of itemBeans) {
        if (itemBean.points) {
          let area: string[] = itemBean.points.split(';')
          if (area && area.length > 0) {
            let xys: number[][] = []
            let minX: number = -1;
            let maxX: number = -1;
            let minY: number = -1;
            let maxY: number = -1;
            for (let item of area) {
              let pair: string[] = item.split(',');
              if (pair && pair.length > 1) {
                let xy: number[] = [StringUtils.parseNumber(pair[0]), StringUtils.parseNumber(pair[1])]
                if (minX < 0) {
                  minX = xy[0]
                } else {
                  if (minX > xy[0]) {
                    minX = xy[0]
                  }
                }
                if (maxX < 0) {
                  maxX = xy[0]
                } else {
                  if (maxX < xy[0]) {
                    maxX = xy[0]
                  }
                }
                if (minY < 0) {
                  minY = xy[1]
                } else {
                  if (minY > xy[1]) {
                    minY = xy[1]
                  }
                }
                if (maxY < 0) {
                  maxY = xy[1]
                } else {
                  if (maxY < xy[1]) {
                    maxY = xy[1]
                  }
                }
                xys.push(xy);
              }

            }

            if (vp2px(x) > minX && vp2px(x) < maxX && vp2px(y) > minY && vp2px(y) < maxY) {
              this.itemBeanClicked = itemBean;
              return xys;
            }
          }
        }

      }
    }
    return []
  }
}