Skip to content

Commit

Permalink
fix: use lit async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Jul 28, 2024
1 parent f2dbe3f commit 5509b54
Show file tree
Hide file tree
Showing 37 changed files with 1,900 additions and 549 deletions.
3 changes: 1 addition & 2 deletions packages/lesson_004/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"gl-matrix": "^3.4.3"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0"
"@types/d3-color": "^3.1.0"
}
}
3 changes: 1 addition & 2 deletions packages/lesson_005/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"gl-matrix": "^3.4.3"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0"
"@types/d3-color": "^3.1.0"
}
}
3 changes: 1 addition & 2 deletions packages/lesson_006/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"gl-matrix": "^3.4.3"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0"
"@types/d3-color": "^3.1.0"
}
}
8 changes: 4 additions & 4 deletions packages/lesson_007/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
},
"dependencies": {
"@antv/g-device-api": "^1.6.12",
"@lit/context": "^1.1.2",
"@lit/task": "^1.0.1",
"@pixi/math": "^7.4.2",
"bezier-easing": "^2.1.0",
"d3-color": "^3.1.0",
"eventemitter3": "^5.0.1",
"gl-matrix": "^3.4.3",
"lit": "^3.1.3",
"@lit/context": "latest"
"lit": "^3.1.3"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0"
"@types/d3-color": "^3.1.0"
}
}
102 changes: 68 additions & 34 deletions packages/lesson_007/src/components/infinite-canvas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { html, css, LitElement } from 'lit';
import { ContextProvider } from '@lit/context';
import { customElement, property, state, query } from 'lit/decorators.js';
import { Task } from '@lit/task';
import { customElement, property, state } from 'lit/decorators.js';
import { Canvas } from '../Canvas';
import { canvasContext } from './context';

async function checkWebGPUSupport() {
if ('gpu' in navigator) {
const gpu = await navigator.gpu.requestAdapter();
if (!gpu) {
throw new Error('No WebGPU adapter available.');
}
} else {
throw new Error('WebGPU is not supported by the browser.');
}
}

@customElement('ic-canvas-lesson7')
export class InfiniteCanvas extends LitElement {
static styles = css`
Expand All @@ -24,12 +36,13 @@ export class InfiniteCanvas extends LitElement {
@property()
renderer = 'webgl';

@property()
shaderCompilerPath =
'https://unpkg.com/@antv/[email protected]/dist/pkg/glsl_wgsl_compiler_bg.wasm';

@state()
zoom = 100;

@query('canvas', true)
$canvas: HTMLCanvasElement;

#provider = new ContextProvider(this, { context: canvasContext });

#canvas: Canvas;
Expand All @@ -50,49 +63,70 @@ export class InfiniteCanvas extends LitElement {
super.disconnectedCallback();
}

async firstUpdated() {
this.#canvas = await new Canvas({
canvas: this.$canvas,
renderer: this.renderer as 'webgl' | 'webgpu',
}).initialized;

this.#provider.setValue(this.#canvas);

this.#canvas.camera.onchange = () => {
this.zoom = Math.round(this.#canvas.camera.zoom * 100);
};

this.dispatchEvent(new CustomEvent('ic-ready', { detail: this.#canvas }));

const animate = (time?: DOMHighResTimeStamp) => {
this.dispatchEvent(new CustomEvent('ic-frame', { detail: time }));

this.#canvas.render();
this.#rafHandle = window.requestAnimationFrame(animate);
};
animate();
}

private resize(event: CustomEvent) {
const detail = event.detail as { entries: ResizeObserverEntry[] };
const { width, height } = detail.entries[0].contentRect;
const dpr = window.devicePixelRatio;

if (width && height) {
const $canvas = this.$canvas;
const $canvas = this.#canvas.getDOM();
$canvas.width = width * dpr;
$canvas.height = height * dpr;
this.#canvas?.resize(width, height);
}
}

private initCanvas = new Task(this, {
task: async ([renderer, shaderCompilerPath]) => {
if (renderer === 'webgpu') {
await checkWebGPUSupport();
}

const canvas = document.createElement('canvas');

this.#canvas = await new Canvas({
canvas,
renderer,
shaderCompilerPath,
}).initialized;

this.#provider.setValue(this.#canvas);

this.#canvas.camera.onchange = () => {
this.zoom = Math.round(this.#canvas.camera.zoom * 100);
};

this.dispatchEvent(new CustomEvent('ic-ready', { detail: this.#canvas }));

const animate = (time?: DOMHighResTimeStamp) => {
this.dispatchEvent(new CustomEvent('ic-frame', { detail: time }));

this.#canvas.render();
this.#rafHandle = window.requestAnimationFrame(animate);
};
animate();

return this.#canvas.getDOM();
},
args: () =>
[this.renderer as 'webgl' | 'webgpu', this.shaderCompilerPath] as const,
});

render() {
return html`
<sl-resize-observer>
<canvas></canvas>
<ic-zoom-toolbar-lesson7 zoom=${this.zoom}></ic-zoom-toolbar-lesson7>
</sl-resize-observer>
`;
return this.initCanvas.render({
pending: () => html`<sl-spinner></sl-spinner>`,
complete: ($canvas) => html`
<sl-resize-observer>
${$canvas}
<ic-zoom-toolbar-lesson7 zoom=${this.zoom}></ic-zoom-toolbar-lesson7>
</sl-resize-observer>
`,
error: (e: Error) => html`<sl-alert variant="danger" open>
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
<strong>Initialize canvas failed</strong><br />
${e.message}
</sl-alert>`,
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/lesson_008/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@
},
"dependencies": {
"@antv/g-device-api": "^1.6.12",
"@lit/context": "^1.1.2",
"@lit/task": "^1.0.1",
"@pixi/math": "^7.4.2",
"bezier-easing": "^2.1.0",
"d3-color": "^3.1.0",
"eventemitter3": "^5.0.1",
"gl-matrix": "^3.4.3",
"lit": "^3.1.3",
"@lit/context": "latest",
"rbush": "^3.0.1"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0",
"@types/rbush": "^3.0.0"
}
}
102 changes: 68 additions & 34 deletions packages/lesson_008/src/components/infinite-canvas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { html, css, LitElement } from 'lit';
import { ContextProvider } from '@lit/context';
import { customElement, property, state, query } from 'lit/decorators.js';
import { Task } from '@lit/task';
import { customElement, property, state } from 'lit/decorators.js';
import { Canvas } from '../Canvas';
import { canvasContext } from './context';

async function checkWebGPUSupport() {
if ('gpu' in navigator) {
const gpu = await navigator.gpu.requestAdapter();
if (!gpu) {
throw new Error('No WebGPU adapter available.');
}
} else {
throw new Error('WebGPU is not supported by the browser.');
}
}

@customElement('ic-canvas-lesson8')
export class InfiniteCanvas extends LitElement {
static styles = css`
Expand All @@ -24,12 +36,13 @@ export class InfiniteCanvas extends LitElement {
@property()
renderer = 'webgl';

@property()
shaderCompilerPath =
'https://unpkg.com/@antv/[email protected]/dist/pkg/glsl_wgsl_compiler_bg.wasm';

@state()
zoom = 100;

@query('canvas', true)
$canvas: HTMLCanvasElement;

#provider = new ContextProvider(this, { context: canvasContext });

#canvas: Canvas;
Expand All @@ -50,49 +63,70 @@ export class InfiniteCanvas extends LitElement {
super.disconnectedCallback();
}

async firstUpdated() {
this.#canvas = await new Canvas({
canvas: this.$canvas,
renderer: this.renderer as 'webgl' | 'webgpu',
}).initialized;

this.#provider.setValue(this.#canvas);

this.#canvas.pluginContext.hooks.cameraChange.tap(() => {
this.zoom = Math.round(this.#canvas.camera.zoom * 100);
});

this.dispatchEvent(new CustomEvent('ic-ready', { detail: this.#canvas }));

const animate = (time?: DOMHighResTimeStamp) => {
this.dispatchEvent(new CustomEvent('ic-frame', { detail: time }));

this.#canvas.render();
this.#rafHandle = window.requestAnimationFrame(animate);
};
animate();
}

private resize(event: CustomEvent) {
const detail = event.detail as { entries: ResizeObserverEntry[] };
const { width, height } = detail.entries[0].contentRect;
const dpr = window.devicePixelRatio;

if (width && height) {
const $canvas = this.$canvas;
const $canvas = this.#canvas.getDOM();
$canvas.width = width * dpr;
$canvas.height = height * dpr;
this.#canvas?.resize(width, height);
}
}

private initCanvas = new Task(this, {
task: async ([renderer, shaderCompilerPath]) => {
if (renderer === 'webgpu') {
await checkWebGPUSupport();
}

const canvas = document.createElement('canvas');

this.#canvas = await new Canvas({
canvas,
renderer,
shaderCompilerPath,
}).initialized;

this.#provider.setValue(this.#canvas);

this.#canvas.pluginContext.hooks.cameraChange.tap(() => {
this.zoom = Math.round(this.#canvas.camera.zoom * 100);
});

this.dispatchEvent(new CustomEvent('ic-ready', { detail: this.#canvas }));

const animate = (time?: DOMHighResTimeStamp) => {
this.dispatchEvent(new CustomEvent('ic-frame', { detail: time }));

this.#canvas.render();
this.#rafHandle = window.requestAnimationFrame(animate);
};
animate();

return this.#canvas.getDOM();
},
args: () =>
[this.renderer as 'webgl' | 'webgpu', this.shaderCompilerPath] as const,
});

render() {
return html`
<sl-resize-observer>
<canvas></canvas>
<ic-zoom-toolbar-lesson8 zoom=${this.zoom}></ic-zoom-toolbar-lesson8>
</sl-resize-observer>
`;
return this.initCanvas.render({
pending: () => html`<sl-spinner></sl-spinner>`,
complete: ($canvas) => html`
<sl-resize-observer>
${$canvas}
<ic-zoom-toolbar-lesson8 zoom=${this.zoom}></ic-zoom-toolbar-lesson8>
</sl-resize-observer>
`,
error: (e: Error) => html`<sl-alert variant="danger" open>
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
<strong>Initialize canvas failed</strong><br />
${e.message}
</sl-alert>`,
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/lesson_009/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@
},
"dependencies": {
"@antv/g-device-api": "^1.6.12",
"@lit/context": "^1.1.2",
"@lit/task": "^1.0.1",
"@pixi/math": "^7.4.2",
"bezier-easing": "^2.1.0",
"d3-color": "^3.1.0",
"eventemitter3": "^5.0.1",
"gl-matrix": "^3.4.3",
"lit": "^3.1.3",
"@lit/context": "latest",
"rbush": "^3.0.1"
},
"devDependencies": {
"@types/d3-color": "^3.1.0",
"@types/gl-matrix": "^3.2.0",
"@types/rbush": "^3.0.0"
}
}
Loading

0 comments on commit 5509b54

Please sign in to comment.