Skip to content

Commit

Permalink
fix: visibility on group
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Nov 23, 2024
1 parent b26569d commit 8694dfc
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 38 deletions.
Binary file added __tests__/ssr/snapshots/visibility-group-hide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions __tests__/ssr/snapshots/visibility-group-hide.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __tests__/ssr/snapshots/visibility-group-show.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions __tests__/ssr/snapshots/visibility-group-show.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 47 additions & 1 deletion __tests__/ssr/visibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSDOM } from 'jsdom';
import xmlserializer from 'xmlserializer';
import { getCanvas, sleep } from '../utils';
import '../useSnapshotMatchers';
import { Canvas, ImageExporter, Rect } from '../../packages/core/src';
import { Canvas, ImageExporter, Rect, Group } from '../../packages/core/src';

const dir = `${__dirname}/snapshots`;
let $canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -90,4 +90,50 @@ describe('Visibility', () => {
'visibility-hide',
);
});

it('should account for visibility in group correctly.', async () => {
const group = new Group();
const rect1 = new Rect({
x: 0,
y: 0,
width: 100,
height: 100,
fill: 'red',
visible: false,
});
group.appendChild(rect1);
const rect2 = new Rect({
x: 50,
y: 50,
width: 100,
height: 100,
fill: 'green',
});
group.appendChild(rect2);
canvas.appendChild(group);
group.visible = false;
canvas.render();

expect($canvas.getContext('webgl1')).toMatchWebGLSnapshot(
dir,
'visibility-group-hide',
);
expect(exporter.toSVG({ grid: true })).toMatchSVGSnapshot(
dir,
'visibility-group-hide',
);

await sleep(300);

group.visible = true;
canvas.render();
expect($canvas.getContext('webgl1')).toMatchWebGLSnapshot(
dir,
'visibility-group-show',
);
expect(exporter.toSVG({ grid: true })).toMatchSVGSnapshot(
dir,
'visibility-group-show',
);
});
});
26 changes: 21 additions & 5 deletions packages/core/examples/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { set } from '@antv/util';
import {
Canvas,
Group,
Circle,
Rect,
RoughCircle,
Expand Down Expand Up @@ -33,17 +33,33 @@ const canvas = await new Canvas({
shaderCompilerPath: '/glsl_wgsl_compiler_bg.wasm',
}).initialized;

const rect = new RoughRect({
const group = new Group();
const rect1 = new Rect({
x: 0,
y: 0,
width: 100,
height: 100,
fill: 'red',
visible: false,
});
group.appendChild(rect1);
const rect2 = new Rect({
x: 50,
y: 50,
width: 100,
height: 100,
fill: 'black',
// fillStyle: 'dots',
fill: 'green',
});
canvas.appendChild(rect);
group.appendChild(rect2);
canvas.appendChild(group);
group.visible = false;
canvas.render();

setTimeout(() => {
group.visible = true;
canvas.render();
}, 2000);

// console.log(toSVGElement(serializeNode(rect1)));

// const animate = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class Canvas {
shape.renderDirtyFlag = true;
}

if (shape.renderable && shape.renderDirtyFlag) {
if (shape.renderDirtyFlag) {
modified.push(shape);
this.#renderDirtyFlag = true;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/plugins/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ export class Renderer implements Plugin {
});

hooks.render.tap((shape) => {
shape.globalRenderOrder = this.#zIndexCounter++;
this.#batchManager.add(shape);
if (shape.renderable) {
shape.globalRenderOrder = this.#zIndexCounter++;
this.#batchManager.add(shape);
}
});
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/plugins/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Plugin, PluginContext } from './interfaces';
export class Selector implements Plugin {
#selected: Shape[] = [];
#selectableMap: Record<string, AbstractSelectable> = {};
#enableContinuousBrush = true;

/**
* the topmost operation layer, which will be appended to documentElement directly
Expand All @@ -31,7 +30,7 @@ export class Selector implements Plugin {
const selected = e.target as Shape;

if (selected === root) {
if (!e.shiftKey || (e.shiftKey && !this.#enableContinuousBrush)) {
if (!e.shiftKey) {
this.deselectAllShapes();
this.#selected = [];
}
Expand Down
Loading

0 comments on commit 8694dfc

Please sign in to comment.