Skip to content

canvas回放时img空白 #851

@pjxxcc

Description

@pjxxcc

问题

canvas回放时里面的img部分显示空白,控制台可以看到如下打印:

index.mjs?c1c8:29 [replayer] Has error on update canvas '1027' Object TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.
    at e.applyIncremental (webpack-internal:///./node_modules/rrweb-player/dist/index.mjs:30:27725)
    at n (webpack-internal:///./node_modules/rrweb-player/dist/index.mjs:30:17695)
    at eval (webpack-internal:///./node_modules/rrweb-player/dist/index.mjs:30:18455)
    at Object.doAction (webpack-internal:///./node_modules/rrweb-player/dist/index.mjs:30:5036)
    at r (webpack-internal:///./node_modules/rrweb-player/dist/index.mjs:16:22300)

排查发现是因为录制的canvas事件中带有一个img元素:

{"type":3,"data":{"source":9,"id":978,"property":"drawImage","args":[img,-129.3076171875,-13,26,26]},"timestamp":1646289935254}

经过JSON.stringify转为json字符串后,dom元素img被处理成了{}

{"type":3,"data":{"source":9,"id":978,"property":"drawImage","args":[{},-129.3076171875,-13,26,26]},"timestamp":1646289935254}

解决

目前想到的方案是在JSON.stringify中对dom元素做转换,仅保留必要的属性,然后在JSON.parse时再逆向恢复为dom元素:

JSON.stringify(item, (name, value) => {
  if (value instanceof HTMLImageElement) {
    return { isHTMLImageElement: true, src: value.src };
  }
  return value;
})

这个方案应该能解决问题,但是感觉不是最佳方案。
我看本项目的文档示例中也是对录制数据进行JSON.stringify处理后存放,是否应该在rrweb.record得到的envent中不返回dom元素,而是简单的数据对象呢?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions