svga/SVGAPlayer-Web-Lite

drawBezier函数没有处理入参d为undefined的情况,导致部分svga会在播放时报错

Opened this issue · 0 comments

你好,我在使用svga的时候,遇到播放个别svga资源时,控制台报错,导致播放失败。
image

经排查,定位到问题出在render.ts里的drawBezier函数,该函数的第二个入参d,replace操作没有处理d为undefined的异常,才导致运行时报错。以下是修改后的代码:

function drawBezier (
  context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
  d: string | undefined,
  transform: Transform | undefined,
  styles: VideoStyles
): void {
  context.save()
  resetShapeStyles(context, styles)
  if (transform !== undefined) {
    context.transform(
      transform.a,
      transform.b,
      transform.c,
      transform.d,
      transform.tx,
      transform.ty
    )
  }
  const currentPoint: CurrentPoint = { x: 0, y: 0, x1: 0, y1: 0, x2: 0, y2: 0 }
  context.beginPath()
  if (d !== undefined) {
    d = d.replace(/([a-zA-Z])/g, '|||$1 ').replace(/,/g, ' ')
    d.split('|||').forEach(segment => {
      if (segment.length === 0) return
      const firstLetter = segment.substr(0, 1)
      if (validMethods.includes(firstLetter)) {
        const args = segment.substr(1).trim().split(' ')
        drawBezierElement(context, currentPoint, firstLetter, args)
      }
    })
  }

  if (styles.fill !== null) {
    context.fill()
  }
  if (styles.stroke !== null) {
    context.stroke()
  }
  context.restore()
}