antvis / antvis/G

canvas 渲染器在设置 enableRenderingOptimization: true 时,Group 上设置的 clipPath 没有生效

Open
#1,915 4 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
1.2k
Forks
230
PR merge metrics
No merged PRs in 30d

Description

# 问题描述

1. 创建一个 `Group` 图元,在它下面挂载一个 `Rect` 图元
3. 给 `Group` 图元设置一个矩形的 `clipPath`
4. 使用 canvas 渲染器进行渲染,在设置 `enableRenderingOptimization: true` 时,可以观察到裁剪没有生效

复现地址:https://stackblitz.com/edit/react-j9yrujub?file=index.js,index.html

经测试使用 svg 渲染器,和不开启 enableRenderingOptimization 的 canvas 渲染时,一切正常。

如果直接把 clipPath 设置在 rect 上,结果也一切正常。

G 的官方文档似乎没有提到 enableRenderingOptimization 这个属性,我从 [其他 issue](https://github.com/antvis/G/issues/1712#issuecomment-2449097474) 中了解到该选项,自己测试时确实有明显的性能提升,不知使用方式是否正确?

## 测试代码

```ts
import { Canvas, CanvasEvent, Rect, Group } from '@antv/g';
import { Renderer as CanvasRenderer } from '@antv/g-canvas';

const canvasRenderer = new CanvasRenderer({
enableRenderingOptimization: true, // 关闭后裁剪正常生效
});

const canvas = new Canvas({
container: 'container',
width: 600,
height: 500,
renderer: canvasRenderer,
});

const group = new Group();

const rect = new Rect({
style: {
x: 150,
y: 150,
width: 200,
height: 100,
fill: 'red',
stroke: 'black',
},
});

const clipPath = new Rect({
style: {
x: 100,
y: 100,
width: 200,
height: 100,
stroke: 'black',
},
});

canvas.addEventListener(CanvasEvent.READY, () => {
canvas.appendChild(group);
group.appendChild(rect);
canvas.appendChild(clipPath);

group.style.clipPath = clipPath;
// rect.style.clipPath = clipPath; // 直接把裁剪设置在 rect 上时,一切正常

// 简单的偏移就能看出效果,不需要动画
// rect.animate(
// [{ transform: 'translateX(200)' }, { transform: 'translateX(-400)' }],
// { duration: 3000 }
// );
});
```

## 版本信息

@antv/g: 6.1.21
@antv/g-canvas: 2.0.40

Contributor guide

Open the contributing guide

Research direction

Run the StackBlitz reproduction from index.js and index.html with @antv/g 6.1.21 and @antv/g-canvas 2.0.40. Start at CanvasRenderer and the enableRenderingOptimization option, then trace how a Group clipPath is applied versus a Rect clipPath. Done means the Group clipPath clips its child with optimization enabled, while the existing SVG and non-optimized canvas behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
computer-graphics, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.