@knotx/plugins-background 0.4.12 → 0.4.14

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.
Files changed (3) hide show
  1. package/README.en.md +226 -0
  2. package/README.md +226 -0
  3. package/package.json +12 -12
package/README.en.md ADDED
@@ -0,0 +1,226 @@
1
+ # @knotx/plugins-background
2
+
3
+ Background plugin that provides visual background patterns for the KnotX canvas.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @knotx/plugins-background
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ The Background plugin provides various background patterns for the KnotX canvas, including dots, lines, and cross patterns. This plugin enhances user experience by providing grid-like visual references.
14
+
15
+ ## Implementation Principle
16
+
17
+ The Background plugin implements background effects using SVG pattern definitions and repeating tiles. The plugin renders in the Background layer, positioned below all other elements. Key principles:
18
+
19
+ 1. **SVG Pattern Generation**: Uses SVG `<defs>` and `<pattern>` to create repeatable background patterns
20
+ 2. **Dynamic Style Calculation**: Dynamically adjusts background offset and gaps based on canvas transform state (zoom, pan)
21
+ 3. **Pattern Type Support**: Supports three pattern types - dots, lines, and cross
22
+
23
+ ## Dependencies
24
+
25
+ ### Core Dependencies
26
+ - `@knotx/core`: Provides base plugin architecture
27
+ - `@knotx/decorators`: Provides decorator support
28
+ - `@knotx/jsx`: Provides JSX support
29
+
30
+ ### Plugin Dependencies
31
+ - `@knotx/plugins-canvas`: Retrieves canvas transform state and container information
32
+
33
+ ## API Documentation
34
+
35
+ ### Main Classes
36
+
37
+ #### Background
38
+
39
+ The main class of the Background plugin, extending `BasePlugin`.
40
+
41
+ ```typescript
42
+ export class Background extends BasePlugin<'background', BackgroundConfig> {
43
+ name = 'background' as const
44
+ }
45
+ ```
46
+
47
+ ### Configuration Options
48
+
49
+ #### BackgroundConfig
50
+
51
+ ```typescript
52
+ export type BackgroundConfig = Omit<BackgroundProps, 'id'>
53
+
54
+ export interface BackgroundProps {
55
+ /** Background color */
56
+ color?: string
57
+ /** Background base color */
58
+ bgColor?: string
59
+ /** Gap between pattern repetitions */
60
+ gap?: number | [number, number]
61
+ /** Size of a single pattern element */
62
+ size?: number
63
+ /** Pattern offset */
64
+ offset?: number | [number, number]
65
+ /** Line width for line patterns */
66
+ lineWidth?: number
67
+ /** Pattern variant type */
68
+ variant?: BackgroundVariant
69
+ /** Styles applied to the container */
70
+ style?: Record<string, any>
71
+ }
72
+ ```
73
+
74
+ #### BackgroundVariant
75
+
76
+ ```typescript
77
+ export enum BackgroundVariant {
78
+ Lines = 'lines',
79
+ Dots = 'dots',
80
+ Cross = 'cross',
81
+ }
82
+ ```
83
+
84
+ ## Usage Examples
85
+
86
+ ### Basic Usage
87
+
88
+ ```typescript
89
+ import { Background, BackgroundVariant } from '@knotx/plugins-background'
90
+
91
+ const engine = new Engine({
92
+ plugins: [Background],
93
+ pluginConfig: {
94
+ background: {
95
+ variant: BackgroundVariant.Dots,
96
+ color: '#ddd',
97
+ gap: 20,
98
+ },
99
+ },
100
+ })
101
+ ```
102
+
103
+ ### Dots Background
104
+
105
+ ```typescript
106
+ const engine = new Engine({
107
+ plugins: [Background],
108
+ pluginConfig: {
109
+ background: {
110
+ variant: BackgroundVariant.Dots,
111
+ color: '#999',
112
+ bgColor: '#f5f5f5',
113
+ gap: 20,
114
+ size: 2,
115
+ },
116
+ },
117
+ })
118
+ ```
119
+
120
+ ### Lines Background
121
+
122
+ ```typescript
123
+ const engine = new Engine({
124
+ plugins: [Background],
125
+ pluginConfig: {
126
+ background: {
127
+ variant: BackgroundVariant.Lines,
128
+ color: '#ddd',
129
+ lineWidth: 1,
130
+ gap: 20,
131
+ },
132
+ },
133
+ })
134
+ ```
135
+
136
+ ### Cross Background
137
+
138
+ ```typescript
139
+ const engine = new Engine({
140
+ plugins: [Background],
141
+ pluginConfig: {
142
+ background: {
143
+ variant: BackgroundVariant.Cross,
144
+ color: '#ddd',
145
+ lineWidth: 1,
146
+ gap: 20,
147
+ size: 10,
148
+ },
149
+ },
150
+ })
151
+ ```
152
+
153
+ ### Custom Styling
154
+
155
+ ```typescript
156
+ const engine = new Engine({
157
+ plugins: [Background],
158
+ pluginConfig: {
159
+ background: {
160
+ variant: BackgroundVariant.Dots,
161
+ color: '#4A90E2',
162
+ bgColor: '#F0F4F8',
163
+ gap: [30, 20], // Different horizontal and vertical gaps
164
+ offset: [10, 5], // Offset values
165
+ style: {
166
+ opacity: 0.8,
167
+ },
168
+ },
169
+ },
170
+ })
171
+ ```
172
+
173
+ ## Integration with Other Plugins
174
+
175
+ ### Integration with Canvas Plugin
176
+
177
+ The Background plugin depends on the Canvas plugin for transform state and container information:
178
+
179
+ ```typescript
180
+ import { Background, BackgroundVariant } from '@knotx/plugins-background'
181
+ import { Canvas } from '@knotx/plugins-canvas'
182
+
183
+ const engine = new Engine({
184
+ plugins: [Canvas, Background],
185
+ pluginConfig: {
186
+ canvas: {
187
+ // canvas configuration
188
+ },
189
+ background: {
190
+ variant: BackgroundVariant.Dots,
191
+ color: '#ddd',
192
+ },
193
+ },
194
+ })
195
+ ```
196
+
197
+ ## File Directory Structure
198
+
199
+ ```
200
+ packages/plugins-background/
201
+ ├── src/
202
+ │ ├── background.tsx # Main implementation file
203
+ │ └── index.ts # Export file
204
+ ├── dist/ # Build output directory
205
+ ├── package.json # Package configuration
206
+ ├── build.config.ts # Build configuration
207
+ ├── tsconfig.json # TypeScript configuration
208
+ ├── eslint.config.mjs # ESLint configuration
209
+ └── CHANGELOG.md # Changelog
210
+ ```
211
+
212
+ ### Core Files Description
213
+
214
+ - **background.tsx**: Contains the main implementation of the Background plugin, including SVG pattern generation, style calculation, and rendering logic
215
+ - **index.ts**: Exports the Background class and related type definitions
216
+
217
+ ## Notes
218
+
219
+ 1. **Performance Optimization**: The Background plugin uses SVG patterns for good performance, but performance optimization might be needed on extremely large canvases
220
+ 2. **Browser Compatibility**: Depends on SVG features, consider target browser compatibility
221
+ 3. **Style Hierarchy**: The Background plugin renders in the Background layer, ensuring it's below all other elements
222
+ 4. **Responsive Design**: The background automatically adjusts based on canvas transform state, no manual handling required
223
+
224
+ ## License
225
+
226
+ MIT
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # @knotx/plugins-background
2
+
3
+ 背景插件,为 KnotX 画布提供可视化背景模式。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @knotx/plugins-background
9
+ ```
10
+
11
+ ## 概述
12
+
13
+ Background 插件为 KnotX 画布提供了多种背景模式,包括点状、线状和交叉模式。该插件可以增强用户体验,提供网格化的视觉参考。
14
+
15
+ ## 实现原理
16
+
17
+ Background 插件通过 SVG 模式定义和重复平铺的方式实现背景效果。插件在 Background 层渲染,位于所有其他元素之下。主要原理:
18
+
19
+ 1. **SVG 模式生成**:使用 SVG `<defs>` 和 `<pattern>` 创建可重复的背景图案
20
+ 2. **动态样式计算**:根据画布变换状态(缩放、平移)动态调整背景的偏移和间隙
21
+ 3. **图案类型支持**:支持点状(dots)、线状(lines)和交叉(cross)三种图案
22
+
23
+ ## 依赖关系
24
+
25
+ ### 核心依赖
26
+ - `@knotx/core`:提供基础插件架构
27
+ - `@knotx/decorators`:提供装饰器支持
28
+ - `@knotx/jsx`:提供 JSX 支持
29
+
30
+ ### 插件依赖
31
+ - `@knotx/plugins-canvas`:获取画布变换状态和容器信息
32
+
33
+ ## API 文档
34
+
35
+ ### 主要类
36
+
37
+ #### Background
38
+
39
+ 背景插件的主要类,继承自 `BasePlugin`。
40
+
41
+ ```typescript
42
+ export class Background extends BasePlugin<'background', BackgroundConfig> {
43
+ name = 'background' as const
44
+ }
45
+ ```
46
+
47
+ ### 配置选项
48
+
49
+ #### BackgroundConfig
50
+
51
+ ```typescript
52
+ export type BackgroundConfig = Omit<BackgroundProps, 'id'>
53
+
54
+ export interface BackgroundProps {
55
+ /** 背景颜色 */
56
+ color?: string
57
+ /** 背景的底色 */
58
+ bgColor?: string
59
+ /** 图案重复之间的间隙 */
60
+ gap?: number | [number, number]
61
+ /** 单个图案元素的大小 */
62
+ size?: number
63
+ /** 图案的偏移量 */
64
+ offset?: number | [number, number]
65
+ /** 线条模式的线宽 */
66
+ lineWidth?: number
67
+ /** 图案变体类型 */
68
+ variant?: BackgroundVariant
69
+ /** 应用于容器的样式 */
70
+ style?: Record<string, any>
71
+ }
72
+ ```
73
+
74
+ #### BackgroundVariant
75
+
76
+ ```typescript
77
+ export enum BackgroundVariant {
78
+ Lines = 'lines',
79
+ Dots = 'dots',
80
+ Cross = 'cross',
81
+ }
82
+ ```
83
+
84
+ ## 使用示例
85
+
86
+ ### 基本用法
87
+
88
+ ```typescript
89
+ import { Background, BackgroundVariant } from '@knotx/plugins-background'
90
+
91
+ const engine = new Engine({
92
+ plugins: [Background],
93
+ pluginConfig: {
94
+ background: {
95
+ variant: BackgroundVariant.Dots,
96
+ color: '#ddd',
97
+ gap: 20,
98
+ },
99
+ },
100
+ })
101
+ ```
102
+
103
+ ### 点状背景
104
+
105
+ ```typescript
106
+ const engine = new Engine({
107
+ plugins: [Background],
108
+ pluginConfig: {
109
+ background: {
110
+ variant: BackgroundVariant.Dots,
111
+ color: '#999',
112
+ bgColor: '#f5f5f5',
113
+ gap: 20,
114
+ size: 2,
115
+ },
116
+ },
117
+ })
118
+ ```
119
+
120
+ ### 线条背景
121
+
122
+ ```typescript
123
+ const engine = new Engine({
124
+ plugins: [Background],
125
+ pluginConfig: {
126
+ background: {
127
+ variant: BackgroundVariant.Lines,
128
+ color: '#ddd',
129
+ lineWidth: 1,
130
+ gap: 20,
131
+ },
132
+ },
133
+ })
134
+ ```
135
+
136
+ ### 交叉背景
137
+
138
+ ```typescript
139
+ const engine = new Engine({
140
+ plugins: [Background],
141
+ pluginConfig: {
142
+ background: {
143
+ variant: BackgroundVariant.Cross,
144
+ color: '#ddd',
145
+ lineWidth: 1,
146
+ gap: 20,
147
+ size: 10,
148
+ },
149
+ },
150
+ })
151
+ ```
152
+
153
+ ### 自定义样式
154
+
155
+ ```typescript
156
+ const engine = new Engine({
157
+ plugins: [Background],
158
+ pluginConfig: {
159
+ background: {
160
+ variant: BackgroundVariant.Dots,
161
+ color: '#4A90E2',
162
+ bgColor: '#F0F4F8',
163
+ gap: [30, 20], // 不同的水平和垂直间隙
164
+ offset: [10, 5], // 偏移量
165
+ style: {
166
+ opacity: 0.8,
167
+ },
168
+ },
169
+ },
170
+ })
171
+ ```
172
+
173
+ ## 与其他插件的集成
174
+
175
+ ### 与 Canvas 插件集成
176
+
177
+ Background 插件依赖 Canvas 插件提供的变换状态和容器信息:
178
+
179
+ ```typescript
180
+ import { Background, BackgroundVariant } from '@knotx/plugins-background'
181
+ import { Canvas } from '@knotx/plugins-canvas'
182
+
183
+ const engine = new Engine({
184
+ plugins: [Canvas, Background],
185
+ pluginConfig: {
186
+ canvas: {
187
+ // canvas 配置
188
+ },
189
+ background: {
190
+ variant: BackgroundVariant.Dots,
191
+ color: '#ddd',
192
+ },
193
+ },
194
+ })
195
+ ```
196
+
197
+ ## 文件目录结构
198
+
199
+ ```
200
+ packages/plugins-background/
201
+ ├── src/
202
+ │ ├── background.tsx # 主要实现文件
203
+ │ └── index.ts # 导出文件
204
+ ├── dist/ # 构建输出目录
205
+ ├── package.json # 包配置文件
206
+ ├── build.config.ts # 构建配置
207
+ ├── tsconfig.json # TypeScript 配置
208
+ ├── eslint.config.mjs # ESLint 配置
209
+ └── CHANGELOG.md # 更新日志
210
+ ```
211
+
212
+ ### 核心文件说明
213
+
214
+ - **background.tsx**:包含 Background 插件的主要实现,包括 SVG 模式生成、样式计算和渲染逻辑
215
+ - **index.ts**:导出 Background 类和相关类型定义
216
+
217
+ ## 注意事项
218
+
219
+ 1. **性能优化**:Background 插件使用 SVG 模式,性能良好,但在极大的画布上可能需要考虑性能优化
220
+ 2. **浏览器兼容性**:依赖 SVG 特性,需要考虑目标浏览器的兼容性
221
+ 3. **样式层级**:Background 插件渲染在 Background 层,确保在所有其他元素之下
222
+ 4. **响应式设计**:背景会根据画布的变换状态自动调整,无需手动处理
223
+
224
+ ## 许可证
225
+
226
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-background",
3
- "version": "0.4.12",
3
+ "version": "0.4.14",
4
4
  "description": "Background Plugin for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -28,19 +28,19 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@knotx/core": "0.4.12",
32
- "@knotx/decorators": "0.4.12",
33
- "@knotx/jsx": "0.4.12",
34
- "@knotx/plugins-canvas": "0.4.12"
31
+ "@knotx/core": "0.4.14",
32
+ "@knotx/decorators": "0.4.14",
33
+ "@knotx/jsx": "0.4.14",
34
+ "@knotx/plugins-canvas": "0.4.14"
35
35
  },
36
36
  "devDependencies": {
37
- "@knotx/build-config": "0.4.12",
38
- "@knotx/core": "0.4.12",
39
- "@knotx/decorators": "0.4.12",
40
- "@knotx/eslint-config": "0.4.12",
41
- "@knotx/jsx": "0.4.12",
42
- "@knotx/plugins-canvas": "0.4.12",
43
- "@knotx/typescript-config": "0.4.12"
37
+ "@knotx/build-config": "0.4.14",
38
+ "@knotx/core": "0.4.14",
39
+ "@knotx/decorators": "0.4.14",
40
+ "@knotx/eslint-config": "0.4.14",
41
+ "@knotx/jsx": "0.4.14",
42
+ "@knotx/plugins-canvas": "0.4.14",
43
+ "@knotx/typescript-config": "0.4.14"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "unbuild",