VTable icon indicating copy to clipboard operation
VTable copied to clipboard

[Feature] 希望在customRender中可以直接通过table.canvas的上下文直接绘制单元格内容,这样可实现一些复杂逻辑

Open DoctorWei opened this issue 9 months ago • 5 comments

What problem does this feature solve?

希望在customRender中可以直接通过table.canvas的上下文直接绘制单元格内容,这样可实现一些复杂逻辑

What does the proposed API look like?

  customRender(args: any) {
    // 希望在customRender中可以直接通过table.canvas的上下文直接绘制单元格内容,这样可实现一些复杂逻辑
    const { table, rect, value } = args;
    const ctx = table.canvas.getContext('2d');

    // 绘制一些东西
    ctx.font = '14px Arial';
    ctx.fillStyle = '#333';
    ctx.textBaseline = 'middle';
    const textWidth = ctx.measureText(value).width;
    const x = rect.left + (rect.width - textWidth) / 2;
    const y = rect.top + rect.height / 2;
    ctx.fillText(value, x, y);
    // 绘制结束
    return true; // 返回 true 表示已自定义渲染,阻止默认渲染
  },

DoctorWei avatar Mar 22 '25 09:03 DoctorWei

请问目前是有什么功能实现不了呢,我们用的VRender底层绘制图来绘制,所以会有个融合节点的问题。 @DoctorWei

fangsmile avatar Mar 24 '25 02:03 fangsmile

我们希望在每个单元格内,绘制很多不同的文本或图形,同时根据列宽可调整字体或图形大小,并且要求表格行高自适应(customRender中可通过计算文本精确返回单元格高度,customLayout中可以开启autoHeight)

customLayout功能也能实现,但性能会差好多。我把customRender及customLayout简单做了一些比较:

分别绘制一些内容,且添加的图元数量少于5,但table行数及列数较多时,customLayout性能表现较差,很影响体验;customRender性能可以提升,但现有的可配置项很有限,有些要求无法达到,比如:字体在 X 方向的缩放

customRender拖动调整列宽:表现正常 Image

customLayout拖动调整列宽:表现有明显卡顿;开启autoHeight后会更卡顿 Image

DoctorWei avatar Mar 24 '25 10:03 DoctorWei

你可以把这个两个放codesanbox上 我们具体看看你的写法 @DoctorWei

fangsmile avatar Mar 25 '25 02:03 fangsmile

Image 为了能及时交流回复 可以加我们的飞书群

fangsmile avatar Mar 25 '25 02:03 fangsmile

已精简放codesanbox上:

https://codesandbox.io/p/devbox/vtable-simple-demo-forked-ftpnrk

关闭autoWrapText可有效减少卡顿,但初始渲染customLayout比customRender要慢很多,写了个全量导出表格(60行,10列,导出后图片分辨率约为4600x7000)为图片的逻辑,使用customLayout的时间是customRender用时的两倍。

DoctorWei avatar Mar 25 '25 05:03 DoctorWei