Skip to content

Commit

Permalink
fix: fix custom element with stroke may cause bounds error (#1857)
Browse files Browse the repository at this point in the history
* fix: fix custom element with stroke may cause bounds error

* chore: commit changeset
  • Loading branch information
Aarebecca authored Nov 27, 2024
1 parent ee852c4 commit 1fb7ecc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-rabbits-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/g-lite': patch
---

fix: fix custom element with stroke may cause bounds error
43 changes: 29 additions & 14 deletions __tests__/demos/bugfix/group-with-stroke.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Group, Path, Rect, runtime } from '@antv/g';
import { Group, Path, Rect, runtime, CustomElement } from '@antv/g';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused imports Group, runtime.

export async function group_with_stroke(context) {
const { canvas } = context;

await canvas.ready;

const group = new Group({
class CustomGroup extends CustomElement<any> {}

const group = new CustomGroup({
style: {
stroke: 'red',
lineWidth: 6,
Expand All @@ -28,24 +30,37 @@ export async function group_with_stroke(context) {

canvas.appendChild(group);

const bounds = group.getRenderBounds();
let rect;

const {
min: [minX, minY],
max: [maxX, maxY],
} = bounds;
const width = maxX - minX;
const height = maxY - minY;
const rect = new Rect({
style: {
const upsert = () => {
const {
min: [minX, minY],
max: [maxX, maxY],
} = group.getRenderBounds();
const width = maxX - minX;
const height = maxY - minY;
const style = {
x: minX,
y: minY,
width,
height,
fill: 'green',
fillOpacity: 0.1,
},
});
zIndex: -1,
};

canvas.appendChild(rect);
if (!rect) {
rect = new Rect({ style });
canvas.appendChild(rect);
} else {
rect.attr(style);
}
};

upsert();

Object.assign(window, {
upsert,
group,
});
}
15 changes: 15 additions & 0 deletions packages/g-lite/src/display-objects/CustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ export interface BaseCustomElementStyleProps extends BaseStyleProps {}
export abstract class CustomElement<
CustomElementStyleProps,
> extends DisplayObject<CustomElementStyleProps & BaseCustomElementStyleProps> {
static PARSED_STYLE_LIST: Set<string> = new Set([
'class',
'className',
'clipPath',
'cursor',
'draggable',
'droppable',
'opacity',
'pointerEvents',
'transform',
'transformOrigin',
'zIndex',
'visibility',
]);

isCustomElement = true;

// private shadowNodes: DisplayObject[] = [];
Expand Down
2 changes: 2 additions & 0 deletions packages/g-lite/src/display-objects/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { DisplayObject } from './DisplayObject';
* @see https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
*/
export class Fragment extends DisplayObject {
static PARSED_STYLE_LIST = new Set<string>(['class', 'className']);

constructor() {
super({ type: Shape.FRAGMENT });
}
Expand Down

0 comments on commit 1fb7ecc

Please sign in to comment.