Skip to content

Commit

Permalink
chore: add docs for ui component
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Jan 27, 2025
1 parent 6aa085f commit 777a962
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 23 deletions.
8 changes: 8 additions & 0 deletions packages/site/docs/.vitepress/config/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export const en = defineConfig({
text: 'Camera',
link: 'camera',
},
{
text: 'UI',
link: 'ui',
},
{
text: 'Image exporter',
link: 'image-exporter',
},
{
text: 'Shape',
items: [
Expand Down
8 changes: 8 additions & 0 deletions packages/site/docs/.vitepress/config/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ export const zh = defineConfig({
text: '画布',
link: 'canvas',
},
{
text: 'UI 组件',
link: 'ui',
},
{
text: '相机',
link: 'camera',
},
{
text: '图片导出器',
link: 'image-exporter',
},
{
text: '图形',
items: [
Expand Down
16 changes: 1 addition & 15 deletions packages/site/docs/.vitepress/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme';
import '@shoelace-style/shoelace/dist/themes/light.css';
import './custom.css';
import Layout from 'genji-theme-vitepress';
import { h } from 'vue';
Expand Down Expand Up @@ -78,24 +77,11 @@ const props = {
Stats,
Core,
UI,
GUI
GUI,
},
};

export default {
extends: DefaultTheme,
Layout: () => h(Layout, props),
async enhanceApp({ app }) {
// @see https://vitepress.dev/guide/ssr-compat#conditional-import
if (!import.meta.env.SSR) {
// @see https://shoelace.style/tutorials/integrating-with-nextjs/#defining-custom-elements
const { setBasePath } = await import(
'@shoelace-style/shoelace/dist/utilities/base-path'
);
setBasePath(
'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.0/cdn/',
);
await import('@shoelace-style/shoelace');
}
},
};
20 changes: 20 additions & 0 deletions packages/site/docs/reference/camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ outline: deep
publish: false
---

See [Lesson 4 - Camera] for more information.

Import the `Camera` class from `@infinite-canvas-tutorial/core`.

```ts
import { Camera } from '@infinite-canvas-tutorial/core';
const camera = new Camera(100, 100);
```

Or get it from the already created [Canvas]:

```ts
const camera = canvas.camera;
```

## constructor

## projectionMatrix

## viewMatrix
Expand Down Expand Up @@ -34,3 +51,6 @@ publish: false
## viewport2Canvas

## canvas2Viewport

[Lesson 4 - Camera]: /guide/lesson-004
[Canvas]: /reference/canvas
13 changes: 12 additions & 1 deletion packages/site/docs/reference/canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ outline: deep
publish: false
---

Import the `Canvas` class from `@infinite-canvas-tutorial/core`.

```ts
import { Canvas } from '@infinite-canvas-tutorial/core';
const canvas = new Canvas({});
```

## constructor

```ts
export interface CanvasConfig {
canvas: HTMLCanvasElement;
canvas: HTMLCanvasElement | OffscreenCanvas;
renderer?: 'webgl' | 'webgpu';
shaderCompilerPath?: string;
devicePixelRatio?: number;
Expand All @@ -16,6 +23,10 @@ export interface CanvasConfig {
}
```

## canvas

In the browser environment, pass in `HTMLCanvasElement`, in the WebWorker environment, pass in `OffscreenCanvas`, and in the Node.js environment, you can use `node-canvas`.

## render

Creates an animation loop for rendering the canvas.
Expand Down
81 changes: 81 additions & 0 deletions packages/site/docs/reference/image-exporter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
outline: deep
publish: false
---

See [Lesson 10 - Importing and Exporting Images] for more information.

```ts
import { ImageExporter } from '@infinite-canvas-tutorial/core';
const exporter = new ImageExporter({ canvas });
```

## constructor

- Required parameter `canvas`, the [Canvas] instance.
- Optional parameter `defaultFilename`, default value is `g`.

```ts
export interface ImageExporterOptions {
canvas: Canvas;
defaultFilename?: string;
}
```

## toCanvas

Returns an `HTMLCanvasElement`, which can be called [toDataURL] later.

```ts
const canvas = await exporter.toCanvas();
canvas.toDataURL(); // data:image/png;base64,...
```

The meaning of each configuration item is as follows:

- `grid` Whether to include a grid
- `beforeDrawImage` Called before drawing the content of the canvas, suitable for drawing the background color.
- `afterDrawImage` Called after drawing the content of the canvas, suitable for drawing watermarks.

```ts
export interface CanvasOptions {
grid: boolean;
beforeDrawImage: (context: CanvasRenderingContext2D) => void;
afterDrawImage: (context: CanvasRenderingContext2D) => void;
}
```

## toSVG

Generate an SVGElement. The `grid` parameter indicates whether to display the grid.

```ts
export interface SVGOptions {
grid: boolean;
}
```

## toSVGDataURL

Generate an SVG's `dataURL`. The `grid` parameter indicates whether to display the grid.

```ts
export interface SVGOptions {
grid: boolean;
}
```

## downloadImage

Triggers the browser's download behavior.

```ts
export interface DownloadImageOptions {
dataURL: string;
name?: string;
}
```

[Lesson 10 - Importing and Exporting Images]: /guide/lesson-010
[Canvas]: /reference/canvas
[toDataURL]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
46 changes: 46 additions & 0 deletions packages/site/docs/reference/ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
outline: deep
publish: false
---

Our canvas includes UI components implemented using Web Components.

```ts
import '@infinite-canvas-tutorial/ui';
```

Use this component in HTML:

```html
<ic-canvas renderer="webgpu"></ic-canvas>
```

`<ic-canvas>` component supports the following properties, all of which are strings.

## renderer

Support `webgpu` and `webgl` renderers, default to `webgl`.

```html
<ic-canvas renderer="webgpu"></ic-canvas>
```

## shaderCompilerPath

Specify the path of the WebGPU shader compiler, which compiles GLSL to WGSL. The default value is `https://unpkg.com/@antv/g-device-api@1.6.8/dist/pkg/glsl_wgsl_compiler_bg.wasm`

## shoelaceBasePath

Our UI components use the Shoelace component library, which specifies the CDN path of Shoelace. The default value is `https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.19.1/cdn`

## zoom

The zoom level of the canvas, default to `100`.

## mode

The interaction mode of the canvas, default to `CanvasMode.HAND`.

## modes

The interaction mode of the canvas, default to `[CanvasMode.HAND, CanvasMode.SELECT, CanvasMode.DRAW_RECT]`.
4 changes: 4 additions & 0 deletions packages/site/docs/zh/guide/lesson-017.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ interface CRDT<T, S> {
### 数据结构设计

参考 [dgmjs-plugin-yjs]

## 扩展阅读 {#extended-reading}

- [Learn Yjs]
Expand All @@ -60,6 +62,7 @@ interface CRDT<T, S> {
- [An Interactive Intro to CRDTs]
- [Building a Collaborative Pixel Art Editor with CRDTs]
- [Making CRDTs 98% More Efficient]
- [dgmjs-plugin-yjs]

[Movable tree CRDTs and Loro's implementation]: https://news.ycombinator.com/item?id=41099901
[CRDTs: The Hard Parts]: https://www.youtube.com/watch?v=x7drE24geUw
Expand All @@ -77,3 +80,4 @@ interface CRDT<T, S> {
[Building a Collaborative Pixel Art Editor with CRDTs]: https://jakelazaroff.com/words/building-a-collaborative-pixel-art-editor-with-crdts/
[Making CRDTs 98% More Efficient]: https://jakelazaroff.com/words/making-crdts-98-percent-more-efficient/
[Learn Yjs]: https://learn.yjs.dev/
[dgmjs-plugin-yjs]: https://github.com/dgmjs/dgmjs/tree/main/packages/dgmjs-plugin-yjs
20 changes: 20 additions & 0 deletions packages/site/docs/zh/reference/camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ outline: deep
publish: false
---

关于相机的相关内容可以参考:[课程 4 - 相机]

`@infinite-canvas-tutorial/core` 中导入 `Camera` 类并创建。

```ts
import { Camera } from '@infinite-canvas-tutorial/core';
const camera = new Camera(100, 100);
```

或者从已经创建的[画布]中获取:

```ts
const camera = canvas.camera;
```

## constructor

## projectionMatrix

## viewMatrix
Expand Down Expand Up @@ -34,3 +51,6 @@ publish: false
## viewport2Canvas

## canvas2Viewport

[课程 4 - 相机]: /zh/guide/lesson-004
[画布]: /zh/reference/canvas
15 changes: 14 additions & 1 deletion packages/site/docs/zh/reference/canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ outline: deep
publish: false
---

`@infinite-canvas-tutorial/core` 中导入 `Canvas` 类。

```ts
import { Canvas } from '@infinite-canvas-tutorial/core';
const canvas = new Canvas({});
```

## constructor

Canvas 的构造函数参数:

```ts
export interface CanvasConfig {
canvas: HTMLCanvasElement;
canvas: HTMLCanvasElement | OffscreenCanvas;
renderer?: 'webgl' | 'webgpu';
shaderCompilerPath?: string;
devicePixelRatio?: number;
Expand All @@ -16,6 +25,10 @@ export interface CanvasConfig {
}
```

## canvas

在浏览器环境传入 `HTMLCanvasElement`,在 WebWorker 环境传入 `OffscreenCanvas`,在 Node.js 环境可以使用 `node-canvas`

## render

手动创建一个动画循环不断调用 `render` 方法。
Expand Down
Loading

0 comments on commit 777a962

Please sign in to comment.