@ue-too/board 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ <h1 align="center">
2
+ uē-tôo
3
+ </h1>
4
+ <p align="center">
5
+ pan, zoom, rotate, and more with your html canvas.
6
+ </p>
7
+
8
+ <div align="center">
9
+
10
+ [![npm version](https://img.shields.io/npm/v/@ue-too/board.svg?style=for-the-badge)](https://www.npmjs.com/package/@ue-too/board)
11
+ [![ci tests](https://img.shields.io/github/actions/workflow/status/ue-too/ue-too/ci-test.yml?label=test&style=for-the-badge)](https://github.com/ue-too/ue-too/actions/workflows/ci-test.yml)
12
+ [![License](https://img.shields.io/github/license/ue-too/ue-too?style=for-the-badge)](https://github.com/ue-too/ue-too/blob/main/LICENSE.txt)
13
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@ue-too/board?color=success&label=gzipped%20bundle%20size&style=for-the-badge)](https://bundlephobia.com/package/@ue-too/board)
14
+
15
+ </div>
16
+
17
+ <p align="center">
18
+ <a href="#quick-demo">Quick Demo</a> •
19
+ <a href="#installation-and-usage">Install</a> •
20
+ <a href="#key-features">Key Features</a> •
21
+ <a href="#quick-start-html-canvas">Quick Start</a> •
22
+ <a href="#development">Development</a> •
23
+ <a href="#under-the-hood">Basic API Overview</a>
24
+ </p>
25
+
26
+ ![small-demo](../../doc-media/small-demo-with-cursor.gif)
27
+
28
+ <p align="center">
29
+ A demonstration of uē-tôo's core functionality.
30
+ </p>
31
+
32
+ > Note: This library is under active development. Some APIs may change in future releases.
33
+
34
+ ## Overview
35
+
36
+ ### What This Library Provides
37
+ - Transforms HTML canvas into a near-infinite canvas with panning, zooming, and rotation capabilities
38
+ - Provides utility functions that simplify the complex mathematics required for infinite canvas operations
39
+ - Compatible with multiple canvas frameworks (vanilla, Pixi.js, Fabric.js, Konva) as the underlying mathematical principles remain consistent
40
+ - Serves as a foundation library for building your own infinite canvas applications
41
+ - Accomplishes the same goal as pixi-viewport but without pixi.js dependency
42
+
43
+ ### What This Library Is Not
44
+ - A complete drawing application like Excalidraw or tldraw
45
+ - A full-featured package with built-in drawing tools and user interfaces
46
+
47
+ ## Motivation
48
+
49
+ Consider this scenario:
50
+
51
+ You're building a web application that allows users to draw on a canvas. You have your pen and eraser tools ready. During testing, you notice that users need to zoom in to work on fine details. After implementing zoom functionality, you realize users can't see other parts of the drawing when zoomed in, necessitating a pan feature.
52
+
53
+ As you add these features, the code becomes increasingly complex, especially when handling different input methods (mouse, touch, trackpad). This is where `ue-too` comes in - it handles all the panning and zooming logic, allowing you to focus on your application's core functionality.
54
+
55
+ Even if you're not building a drawing app, `ue-too` is useful for any canvas that requires panning functionality. It works with various frameworks including pixi.js, fabric.js, Konva, vanilla JavaScript canvas API, and even headless canvas in Node.js.
56
+
57
+ ## Quick Demo
58
+ [Stackblitz example link](https://stackblitz.com/edit/vitejs-vite-jpxrtxzg?file=index.html): This example demonstrates the basic functionality shown in the [Quick Start](#quick-start-using-only-html-canvas) section.
59
+
60
+ Additional examples in the [`devserver`](https://github.com/niuee/board/tree/main/devserver) directory show integration with pixi.js, fabric.js, and Konva (incomplete but providing general implementation guidance).
61
+
62
+ ## Documentation
63
+ - [API Documentation](https://ue-too.github.io/ue-too/) (Deprecated)
64
+ - [中文文件連結](https://ue-too.github.io/ue-too/tw/index.html) (還在努力補沒翻完的,還要開發新功能,時間真的不太夠 u.u) (Deprecated)
65
+
66
+ ## Installation and Usage
67
+
68
+ ### Package Manager
69
+ ```bash
70
+ npm install @ue-too/board
71
+ ```
72
+
73
+ ```javascript
74
+ import { Board } from "@ue-too/board";
75
+ ```
76
+
77
+ ### Import from jsdelivr
78
+ ```javascript
79
+ import { Board } from "https://cdn.jsdelivr.net/npm/@ue-too/board@latest/index.js";
80
+ ```
81
+
82
+ > Note: IIFE format is no longer supported.
83
+
84
+ ## Key Features
85
+ - Modularity: Use only the components you need (details in the [Under the Hood](#under-the-hood) section)
86
+ - Comprehensive input support: touch, trackpad (macOS), keyboard, and mouse, with customizable behavior
87
+ - Framework-agnostic: Works with HTML and JavaScript, and can be integrated with frontend frameworks/libraries
88
+ - Multi-framework compatibility: Works with pixi.js, fabric.js, Konva, and vanilla HTML canvas
89
+
90
+ ## Quick Start (HTML Canvas)
91
+
92
+ This example is based on the MDN documentation for the [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API). (turning the MDN example into an infinite canvas)
93
+
94
+ HTML:
95
+ ```html
96
+ <canvas id="graph"></canvas>
97
+ ```
98
+
99
+ ```javascript
100
+ import { Board } from "@ue-too/board";
101
+
102
+ const canvas = document.getElementById("graph");
103
+
104
+ const board = new Board(canvas);
105
+
106
+ function draw(timestamp) {
107
+ // step the board
108
+ board.step(timestamp);
109
+
110
+ // add the rectangle back to the canvas, the drawing steps is the same as the MDN example but we're using the context from the board instance.
111
+ board.context.fillStyle = "green";
112
+ board.context.fillRect(10, 10, 150, 100);
113
+
114
+ // request the next frame
115
+ requestAnimationFrame(draw);
116
+ }
117
+
118
+ // call the draw function every frame
119
+ requestAnimationFrame(draw);
120
+ ```
121
+
122
+ ### Default Input Controls
123
+
124
+ Pan:
125
+ - Mouse + Keyboard: Drag while holding spacebar or use scroll wheel button
126
+ - Trackpad: Two-finger swipe
127
+ - Touch: Two-finger swipe
128
+
129
+ Zoom:
130
+ - Mouse + Keyboard: Ctrl + scroll wheel
131
+ - Trackpad: Two-finger pinch
132
+ - Touch: Two-finger pinch
133
+
134
+ ### Important Notes
135
+ - All drawing operations should be performed in the `requestAnimationFrame` callback after the `step` function
136
+ - The `Board` class is designed for minimal setup but offers less flexibility
137
+ - For more customization, refer to the [Under the Hood](#under-the-hood) section
138
+
139
+ The `Board` class handles:
140
+ - Input event interpretation
141
+ - Automatic camera zoom boundary adjustments
142
+ - And more...
143
+
144
+ All components and utility functions are accessible, allowing you to create your own board implementation without using the `requestAnimationFrame` callback method.
145
+
146
+ For detailed camera control information, refer to the [Board Camera](./src/board-camera/README.md) section.
147
+
148
+ ## Development
149
+
150
+ > This section is for working directly with the library's source code. If you're using the library and need to customize component behavior, skip to the [Under the Hood](#under-the-hood) section.
151
+
152
+ > Currently not ready for contribution. If you have any suggestions or ideas, please let me know by creating an issue.
153
+
154
+ Please refer to the [README](./README.md) in the root directory for the overall development setup.
155
+
156
+ 1. This package is within a monorepo, and is managed by nx and pnpm. I am not super familiar with nx or monorepo; this is kind of an experiment and a learning experience for me. (if you have any suggestions on how to improve the setup, please let me know!)
157
+ 2. Bundling the package is done through rollup and testing through jest.
158
+
159
+
160
+ ## Under the Hood
161
+
162
+ ue-too consists of 3 core components:
163
+
164
+ - `Board Camera (viewport)`: This is the core of the cores xD; It's the class that holds the information about the viewport.
165
+ - `Camera Input Multiplexer`: This is the part that determines which kind of input should be passed through based on the current condition. This is to support multiple input methods. For example, user input would take precedence over the transition animation input and so on.
166
+ - `User Input Interpretation`: This is the part that handles the user input events from the canvas element (pointer, keyboard, touch, etc.), and based on the events determine what the user intentions are.
167
+
168
+ To see detail of each component navigate to the respective readme in the subdirectories.
169
+ - [Board Camera](./src/camera/README.md)
170
+ - [Camera Mux](./src/camera/camera-mux/README.md)
171
+ - [User Input Interpreter](./src/input-interpretation/README.md)
172
+
173
+ It's recommended to start with the [Board Camera](./src/board-camera/README.md) since the other parts are built on top of it.
174
+
175
+ Below is a diagram showing from the user input to how the camera is updated and everything in the middle.
176
+
177
+ ![data-flow](../../doc-media/entire-process.png)
178
+
179
+ ## TODO
180
+ - [x] Add a canvas position dimension publisher that can be used to get the position and dimension of the canvas.
181
+ - [ ] Add a `boardify-pixi` package that contains utility functions for the board to work with pixi.js.
182
+ - [ ] Add a `boardify-fabric` package that contains utility functions for the board to work with fabric.js.
183
+ - [ ] Add a `boardify-konva` package that contains utility functions for the board to work with konva.js.
184
+ - [ ] Add an example of the board being used with react.
185
+ - [ ] Add an example of the board being used with svelte. (I'm learning svelte right now so I can make a example for that)
186
+ - [ ] Add an example of the board being used with vue. (Currently I don't have any plans on learning vue so probably not going to make one very soon)
187
+ - [ ] A documentation site. There is a lot of util stuff that I don't think will fit in here in the readme. So stay tuned! (I am experimenting with docusaurus right now so it might be a docusaurus site)
188
+