Skip to content

Commit 7393884

Browse files
committed
feat: culling optimize
1 parent e852e6c commit 7393884

File tree

12 files changed

+44
-11
lines changed

12 files changed

+44
-11
lines changed

packages/core/src/graphics/canvas.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class SuikaCanvas extends SuikaGraphics<SuikaCanvasAttrs> {
3636
}
3737
return null;
3838
}
39+
40+
protected override shouldSkipDraw() {
41+
return false;
42+
}
3943
}
4044

4145
export const isCanvasGraphics = (

packages/core/src/graphics/ellipse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export class SuikaEllipse extends SuikaGraphics<EllipseAttrs> {
4949
}
5050

5151
override draw(drawInfo: IDrawInfo) {
52+
if (this.shouldSkipDraw(drawInfo)) return;
53+
5254
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
53-
if (!this.isVisible() || opacity === 0) return;
5455
const { ctx, imgManager, smooth } = drawInfo;
5556
const attrs = this.attrs;
5657
const cx = attrs.width / 2;

packages/core/src/graphics/frame/frame.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ export class SuikaFrame extends SuikaGraphics<FrameAttrs> {
205205
}
206206

207207
override draw(drawInfo: IDrawInfo) {
208+
if (this.shouldSkipDraw(drawInfo)) return;
208209
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
209-
if (!this.isVisible() || opacity === 0) return;
210210

211211
drawInfo = {
212212
...drawInfo,
213-
opacity: opacity,
213+
opacity,
214214
};
215215

216216
if (!this.isGroup()) {

packages/core/src/graphics/graphics/graphics.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ export class SuikaGraphics<ATTRS extends GraphicsAttrs = GraphicsAttrs> {
359359
return false;
360360
}
361361

362+
protected strokeAABBIntersectWithBox(box: IBox) {
363+
return isBoxIntersect(box, this.getBboxWithStroke());
364+
}
365+
362366
/**
363367
* whether the element intersect with the box
364368
*/
@@ -476,8 +480,22 @@ export class SuikaGraphics<ATTRS extends GraphicsAttrs = GraphicsAttrs> {
476480
this.updateAttrs(rect, { finishRecomputed: true });
477481
}
478482

483+
protected shouldSkipDraw(drawInfo: IDrawInfo) {
484+
if (!this.isVisible()) return true;
485+
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
486+
if (opacity === 0) return true;
487+
488+
if (
489+
drawInfo.viewportArea &&
490+
!this.strokeAABBIntersectWithBox(drawInfo.viewportArea)
491+
) {
492+
return true;
493+
}
494+
return false;
495+
}
496+
479497
draw(drawInfo: IDrawInfo) {
480-
if (!this.isVisible()) return;
498+
if (this.shouldSkipDraw(drawInfo)) return;
481499

482500
const { ctx } = drawInfo;
483501

packages/core/src/graphics/line.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export class SuikaLine extends SuikaGraphics<LineAttrs> {
3131
}
3232

3333
override draw(drawInfo: IDrawInfo) {
34+
if (this.shouldSkipDraw(drawInfo)) return;
35+
3436
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
35-
if (!this.isVisible() || opacity === 0) return;
3637
const { ctx } = drawInfo;
3738
const { width, transform, stroke, strokeWidth } = this.attrs;
3839
ctx.save();

packages/core/src/graphics/path/path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ export class SuikaPath extends SuikaGraphics<PathAttrs> {
185185
}
186186

187187
override draw(drawInfo: IDrawInfo) {
188+
if (this.shouldSkipDraw(drawInfo)) return;
189+
188190
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
189-
if (!this.isVisible() || opacity === 0) return;
190191
this._realDraw({ ...drawInfo, opacity });
191192
}
192193

packages/core/src/graphics/rect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ export class SuikaRect extends SuikaGraphics<RectAttrs> {
143143
}
144144

145145
override draw(drawInfo: IDrawInfo) {
146+
if (this.shouldSkipDraw(drawInfo)) return;
147+
146148
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
147-
if (!this.isVisible() || opacity === 0) return;
148149
this._realDraw({ ...drawInfo, opacity });
149150
}
150151

packages/core/src/graphics/regular_polygon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ export class SuikaRegularPolygon extends SuikaGraphics<RegularPolygonAttrs> {
6868
}
6969

7070
override draw(drawInfo: IDrawInfo) {
71+
if (this.shouldSkipDraw(drawInfo)) return;
72+
7173
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
72-
if (!this.isVisible() || opacity === 0) return;
7374
this._realDraw({ ...drawInfo, opacity });
7475
}
7576

packages/core/src/graphics/star.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export class SuikaStar extends SuikaGraphics<StarAttrs> {
6969
}
7070

7171
override draw(drawInfo: IDrawInfo) {
72+
if (this.shouldSkipDraw(drawInfo)) return;
73+
7274
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
73-
if (!this.isVisible() || opacity === 0) return;
7475
this._realDraw({ ...drawInfo, opacity });
7576
}
7677

packages/core/src/graphics/text.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ export class SuikaText extends SuikaGraphics<TextAttrs> {
7474
}
7575

7676
override draw(drawInfo: IDrawInfo) {
77+
if (this.shouldSkipDraw(drawInfo)) return;
78+
7779
const opacity = this.getOpacity() * (drawInfo.opacity ?? 1);
78-
if (!this.isVisible() || opacity === 0) return;
7980
const { transform, fill, stroke, fontSize, content, fontFamily } =
8081
this.attrs;
8182
const { ctx } = drawInfo;

0 commit comments

Comments
 (0)