canvas-drawing-editor 2.5.0 → 2.6.1

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 CHANGED
@@ -40,6 +40,7 @@
40
40
  - 🖋️ **富文本** - 支持部分加粗、部分改色、部分斜体
41
41
  - 🎬 **Tween 动画** - 对象属性过渡动画(位置、大小、透明度等)
42
42
  - 📱 **移动端支持** - 单指拖拽、双指缩放/旋转、长按选择、响应式布局
43
+ - 📐 **形状库** - 注册自定义形状,支持带文字的组合形状,可搜索选择
43
44
 
44
45
  ### 📦 安装
45
46
 
@@ -196,6 +197,7 @@ export class AppModule { }
196
197
  | `layers` | boolean | true | 图层管理 |
197
198
  | `group` | boolean | true | 组合/解组 |
198
199
  | `align` | boolean | true | 对齐/分布 |
200
+ | `shapePanel` | boolean | true | 形状库面板 |
199
201
 
200
202
  #### 旧版属性(向后兼容)
201
203
 
@@ -455,6 +457,94 @@ document.addEventListener('animation-update', (e) => {
455
457
  }
456
458
  ```
457
459
 
460
+ ### 📐 形状库 API
461
+
462
+ 形状库功能允许你注册自定义形状,用户可以从形状面板中选择并添加到画布。
463
+
464
+ #### 注册形状
465
+
466
+ ```javascript
467
+ const editor = document.querySelector('canvas-drawing-editor');
468
+
469
+ // 注册单个或多个形状
470
+ editor.registerShapes([
471
+ {
472
+ id: 'btn-confirm',
473
+ name: '确认按钮',
474
+ type: 'roundedRect',
475
+ category: '按钮',
476
+ fillColor: '#22c55e',
477
+ fillMode: 'fill',
478
+ cornerRadius: 8,
479
+ width: 100,
480
+ height: 40,
481
+ text: '确认',
482
+ textColor: '#ffffff',
483
+ fontSize: 14,
484
+ fontWeight: 'bold'
485
+ },
486
+ {
487
+ id: 'flow-start',
488
+ name: '开始节点',
489
+ type: 'ellipse',
490
+ category: '流程图',
491
+ fillColor: '#dbeafe',
492
+ fillMode: 'both',
493
+ strokeColor: '#3b82f6',
494
+ strokeWidth: 2,
495
+ width: 100,
496
+ height: 60,
497
+ text: '开始',
498
+ textColor: '#1e40af'
499
+ }
500
+ ]);
501
+ ```
502
+
503
+ #### 形状配置属性
504
+
505
+ | 属性 | 类型 | 说明 |
506
+ |------|------|------|
507
+ | `id` | string | 形状唯一标识 |
508
+ | `name` | string | 形状名称(显示用) |
509
+ | `type` | string | 形状类型:`rectangle`, `circle`, `triangle`, `star`, `heart`, `diamond`, `polygon`, `ellipse`, `roundedRect`, `parallelogram`, `trapezoid`, `hexagon` |
510
+ | `category` | string | 分类名称(可选) |
511
+ | `fillColor` | string | 填充颜色 |
512
+ | `fillMode` | string | 填充模式:`stroke`, `fill`, `both` |
513
+ | `strokeColor` | string | 边框颜色 |
514
+ | `strokeWidth` | number | 边框宽度 |
515
+ | `width` | number | 默认宽度 |
516
+ | `height` | number | 默认高度 |
517
+ | `cornerRadius` | number | 圆角半径(圆角矩形) |
518
+ | `text` | string | 形状中心文字 |
519
+ | `textColor` | string | 文字颜色 |
520
+ | `fontSize` | number | 文字大小 |
521
+ | `fontWeight` | string | 文字粗细:`normal`, `bold` |
522
+ | `icon` | string | 自定义 SVG 图标(面板显示用) |
523
+
524
+ #### 监听形状添加事件
525
+
526
+ ```javascript
527
+ editor.addEventListener('shape-added', (e) => {
528
+ console.log('形状已添加:', e.detail.shape.name);
529
+ console.log('创建的对象:', e.detail.object);
530
+ });
531
+ ```
532
+
533
+ #### 工具配置
534
+
535
+ 通过 `tool-config` 或 `show-shape-panel` 属性控制形状库按钮显示:
536
+
537
+ ```html
538
+ <canvas-drawing-editor
539
+ tool-config='{"shapePanel": true}'
540
+ ></canvas-drawing-editor>
541
+
542
+ <!-- 或使用单独属性 -->
543
+ <canvas-drawing-editor
544
+ show-shape-panel="true"
545
+ ></canvas-drawing-editor>
546
+ ```
547
+
458
548
  ### 🎬 Tween 动画 API
459
549
 
460
550
  通过 `tweenAnimate()` 方法可以为对象创建平滑的属性过渡动画:
@@ -486,6 +576,38 @@ editor.tweenAnimate(objectId, { x: 400 }, {
486
576
  editor.stopAllAnimations();
487
577
  ```
488
578
 
579
+ ### 🖼️ 图片导出 API
580
+
581
+ 通过 `getImageData()` 方法可以获取画布图片数据(base64 或 Blob),无需触发下载:
582
+
583
+ ```javascript
584
+ const editor = document.querySelector('canvas-drawing-editor');
585
+
586
+ // 获取 base64 格式(默认)
587
+ const dataURL = await editor.getImageData();
588
+ console.log(dataURL); // data:image/png;base64,...
589
+
590
+ // 获取 Blob 格式(适合上传到服务器)
591
+ const blob = await editor.getImageData({
592
+ type: 'blob',
593
+ format: 'png', // 'png' | 'jpeg' | 'webp'
594
+ quality: 0.92, // jpeg/webp 质量 (0-1)
595
+ background: '#ffffff' // 背景色
596
+ });
597
+
598
+ // 上传到服务器示例
599
+ const formData = new FormData();
600
+ formData.append('image', blob, 'canvas.png');
601
+ await fetch('/api/upload', { method: 'POST', body: formData });
602
+ ```
603
+
604
+ | 参数 | 类型 | 默认值 | 说明 |
605
+ |------|------|--------|------|
606
+ | `format` | string | 'png' | 图片格式:'png', 'jpeg', 'webp' |
607
+ | `quality` | number | 0.92 | 图片质量(仅 jpeg/webp 有效,0-1) |
608
+ | `type` | string | 'dataURL' | 返回类型:'dataURL' 或 'blob' |
609
+ | `background` | string | '#ffffff' | 背景颜色 |
610
+
489
611
  ### 🛠️ 开发
490
612
 
491
613
  ```bash
@@ -528,6 +650,7 @@ A powerful canvas-based drawing editor Web Component with **zero dependencies**.
528
650
  - 🖋️ **Rich Text** - Support partial bold, partial color, partial italic
529
651
  - 🎬 **Tween Animation** - Object property transition animations (position, size, opacity, etc.)
530
652
  - 📱 **Mobile Support** - Single finger drag, two-finger zoom/rotate, long press selection, responsive layout
653
+ - 📐 **Shape Library** - Register custom shapes with text, searchable panel for quick selection
531
654
 
532
655
  ### 📦 Installation
533
656
 
@@ -684,6 +807,7 @@ Recommended: Use `tool-config` attribute for unified tool configuration:
684
807
  | `layers` | boolean | true | Layer management |
685
808
  | `group` | boolean | true | Group/Ungroup |
686
809
  | `align` | boolean | true | Align/Distribute |
810
+ | `shapePanel` | boolean | true | Shape library panel |
687
811
 
688
812
  #### Legacy Attributes (Backward Compatible)
689
813
 
@@ -916,6 +1040,94 @@ Steps:
916
1040
  }
917
1041
  ```
918
1042
 
1043
+ ### 📐 Shape Library API
1044
+
1045
+ The Shape Library feature allows you to register custom shapes that users can select from a panel and add to the canvas.
1046
+
1047
+ #### Register Shapes
1048
+
1049
+ ```javascript
1050
+ const editor = document.querySelector('canvas-drawing-editor');
1051
+
1052
+ // Register one or more shapes
1053
+ editor.registerShapes([
1054
+ {
1055
+ id: 'btn-confirm',
1056
+ name: 'Confirm Button',
1057
+ type: 'roundedRect',
1058
+ category: 'Buttons',
1059
+ fillColor: '#22c55e',
1060
+ fillMode: 'fill',
1061
+ cornerRadius: 8,
1062
+ width: 100,
1063
+ height: 40,
1064
+ text: 'Confirm',
1065
+ textColor: '#ffffff',
1066
+ fontSize: 14,
1067
+ fontWeight: 'bold'
1068
+ },
1069
+ {
1070
+ id: 'flow-start',
1071
+ name: 'Start Node',
1072
+ type: 'ellipse',
1073
+ category: 'Flowchart',
1074
+ fillColor: '#dbeafe',
1075
+ fillMode: 'both',
1076
+ strokeColor: '#3b82f6',
1077
+ strokeWidth: 2,
1078
+ width: 100,
1079
+ height: 60,
1080
+ text: 'Start',
1081
+ textColor: '#1e40af'
1082
+ }
1083
+ ]);
1084
+ ```
1085
+
1086
+ #### Shape Configuration Properties
1087
+
1088
+ | Property | Type | Description |
1089
+ |----------|------|-------------|
1090
+ | `id` | string | Unique shape identifier |
1091
+ | `name` | string | Shape name (for display) |
1092
+ | `type` | string | Shape type: `rectangle`, `circle`, `triangle`, `star`, `heart`, `diamond`, `polygon`, `ellipse`, `roundedRect`, `parallelogram`, `trapezoid`, `hexagon` |
1093
+ | `category` | string | Category name (optional) |
1094
+ | `fillColor` | string | Fill color |
1095
+ | `fillMode` | string | Fill mode: `stroke`, `fill`, `both` |
1096
+ | `strokeColor` | string | Stroke color |
1097
+ | `strokeWidth` | number | Stroke width |
1098
+ | `width` | number | Default width |
1099
+ | `height` | number | Default height |
1100
+ | `cornerRadius` | number | Corner radius (for rounded rectangles) |
1101
+ | `text` | string | Center text content |
1102
+ | `textColor` | string | Text color |
1103
+ | `fontSize` | number | Font size |
1104
+ | `fontWeight` | string | Font weight: `normal`, `bold` |
1105
+ | `icon` | string | Custom SVG icon (for panel display) |
1106
+
1107
+ #### Listen for Shape Added Event
1108
+
1109
+ ```javascript
1110
+ editor.addEventListener('shape-added', (e) => {
1111
+ console.log('Shape added:', e.detail.shape.name);
1112
+ console.log('Created object:', e.detail.object);
1113
+ });
1114
+ ```
1115
+
1116
+ #### Tool Configuration
1117
+
1118
+ Control shape library button visibility via `tool-config` or `show-shape-panel` attribute:
1119
+
1120
+ ```html
1121
+ <canvas-drawing-editor
1122
+ tool-config='{"shapePanel": true}'
1123
+ ></canvas-drawing-editor>
1124
+
1125
+ <!-- Or use individual attribute -->
1126
+ <canvas-drawing-editor
1127
+ show-shape-panel="true"
1128
+ ></canvas-drawing-editor>
1129
+ ```
1130
+
919
1131
  ### 🎬 Tween Animation API
920
1132
 
921
1133
  Use `tweenAnimate()` method to create smooth property transition animations:
@@ -947,6 +1159,38 @@ editor.tweenAnimate(objectId, { x: 400 }, {
947
1159
  editor.stopAllAnimations();
948
1160
  ```
949
1161
 
1162
+ ### 🖼️ Image Export API
1163
+
1164
+ Use `getImageData()` method to get canvas image data (base64 or Blob) without triggering download:
1165
+
1166
+ ```javascript
1167
+ const editor = document.querySelector('canvas-drawing-editor');
1168
+
1169
+ // Get base64 format (default)
1170
+ const dataURL = await editor.getImageData();
1171
+ console.log(dataURL); // data:image/png;base64,...
1172
+
1173
+ // Get Blob format (suitable for server upload)
1174
+ const blob = await editor.getImageData({
1175
+ type: 'blob',
1176
+ format: 'png', // 'png' | 'jpeg' | 'webp'
1177
+ quality: 0.92, // jpeg/webp quality (0-1)
1178
+ background: '#ffffff' // Background color
1179
+ });
1180
+
1181
+ // Upload to server example
1182
+ const formData = new FormData();
1183
+ formData.append('image', blob, 'canvas.png');
1184
+ await fetch('/api/upload', { method: 'POST', body: formData });
1185
+ ```
1186
+
1187
+ | Parameter | Type | Default | Description |
1188
+ |-----------|------|---------|-------------|
1189
+ | `format` | string | 'png' | Image format: 'png', 'jpeg', 'webp' |
1190
+ | `quality` | number | 0.92 | Image quality (jpeg/webp only, 0-1) |
1191
+ | `type` | string | 'dataURL' | Return type: 'dataURL' or 'blob' |
1192
+ | `background` | string | '#ffffff' | Background color |
1193
+
950
1194
  ### 🛠️ Development
951
1195
 
952
1196
  ```bash