canvas-drawing-editor 2.0.4 → 2.0.6

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
@@ -144,21 +144,13 @@ You can pass JSON data to initialize the canvas content:
144
144
 
145
145
  #### `editor-change` Event
146
146
 
147
- Fires when canvas content changes. The event detail contains:
148
-
149
- | Property | Type | Description |
150
- |----------|------|-------------|
151
- | `objects` | Array | All drawing objects (raw data) |
152
- | `data` | Object | Serializable JSON data for storage/restore |
147
+ Fires when canvas content changes. The `e.detail.objects` array contains all drawing objects.
153
148
 
154
149
  ```javascript
155
150
  document.addEventListener('editor-change', (e) => {
156
151
  console.log('Objects:', e.detail.objects);
157
-
158
- // Use e.detail.data for saving to database
159
- const jsonData = JSON.stringify(e.detail.data);
160
- localStorage.setItem('canvas-data', jsonData);
161
- // or save to server: fetch('/api/save', { method: 'POST', body: jsonData });
152
+ // Save to server or localStorage
153
+ localStorage.setItem('canvas-data', JSON.stringify({ objects: e.detail.objects }));
162
154
  });
163
155
  ```
164
156
 
@@ -207,11 +199,10 @@ Each object in `e.detail.objects` has the following base properties:
207
199
  #### Example: Saving and Loading
208
200
 
209
201
  ```javascript
210
- // Save canvas content (use e.detail.data for clean serializable data)
202
+ // Save canvas content
211
203
  document.addEventListener('editor-change', (e) => {
212
- const jsonData = JSON.stringify(e.detail.data);
213
- localStorage.setItem('my-canvas', jsonData);
214
- // or save to database
204
+ const data = JSON.stringify({ objects: e.detail.objects });
205
+ localStorage.setItem('my-canvas', data);
215
206
  });
216
207
 
217
208
  // Load canvas content
@@ -384,21 +375,13 @@ function App() {
384
375
 
385
376
  #### `editor-change` 事件
386
377
 
387
- 当画布内容变化时触发。事件详情包含:
388
-
389
- | 属性 | 类型 | 说明 |
390
- |------|------|------|
391
- | `objects` | Array | 所有绑图对象(原始数据) |
392
- | `data` | Object | 可序列化的 JSON 数据,用于存储/回显 |
378
+ 当画布内容变化时触发。`e.detail.objects` 数组包含所有绑图对象。
393
379
 
394
380
  ```javascript
395
381
  document.addEventListener('editor-change', (e) => {
396
382
  console.log('对象列表:', e.detail.objects);
397
-
398
- // 使用 e.detail.data 保存到数据库
399
- const jsonData = JSON.stringify(e.detail.data);
400
- localStorage.setItem('canvas-data', jsonData);
401
- // 或保存到服务器: fetch('/api/save', { method: 'POST', body: jsonData });
383
+ // 保存到服务器或 localStorage
384
+ localStorage.setItem('canvas-data', JSON.stringify({ objects: e.detail.objects }));
402
385
  });
403
386
  ```
404
387
 
@@ -447,11 +430,10 @@ document.addEventListener('editor-change', (e) => {
447
430
  #### 示例:保存和加载画布
448
431
 
449
432
  ```javascript
450
- // 保存画布内容(使用 e.detail.data 获取可序列化的数据)
433
+ // 保存画布内容
451
434
  document.addEventListener('editor-change', (e) => {
452
- const jsonData = JSON.stringify(e.detail.data);
453
- localStorage.setItem('my-canvas', jsonData);
454
- // 或保存到数据库
435
+ const data = JSON.stringify({ objects: e.detail.objects });
436
+ localStorage.setItem('my-canvas', data);
455
437
  });
456
438
 
457
439
  // 加载画布内容
@@ -239,23 +239,10 @@ class b extends HTMLElement {
239
239
  }
240
240
  // 派发变化事件
241
241
  dispatchChangeEvent() {
242
- const t = {
243
- objects: this.objects.map((i) => {
244
- if (i.type === "IMAGE") {
245
- const { imageElement: s, ...e } = i;
246
- return e;
247
- }
248
- return { ...i };
249
- })
250
- };
251
242
  this.dispatchEvent(new CustomEvent("editor-change", {
252
243
  bubbles: !0,
253
244
  composed: !0,
254
- detail: {
255
- objects: this.objects,
256
- data: t
257
- // 可直接用于存储/回显的 JSON 数据
258
- }
245
+ detail: { objects: this.objects }
259
246
  }));
260
247
  }
261
248
  // 键盘事件处理
@@ -547,7 +534,7 @@ class b extends HTMLElement {
547
534
  const t = this.minimapCanvas, i = this.canvas;
548
535
  this.minimapCtx.clearRect(0, 0, t.width, t.height);
549
536
  const s = t.width / i.width, e = t.height / i.height, n = Math.min(s, e) * 0.92, o = i.width * n, a = i.height * n, l = (t.width - o) / 2, r = (t.height - a) / 2;
550
- this.minimapCtx.fillStyle = "#ffffff", this.minimapCtx.fillRect(l, r, o, a), this.minimapCtx.save(), this.minimapCtx.translate(l, r), this.minimapCtx.scale(n, n), this.minimapCtx.translate(this.panOffset.x, this.panOffset.y), this.minimapCtx.scale(this.scale, this.scale), (this.currentObject ? [...this.objects, this.currentObject] : this.objects).forEach((d) => {
537
+ this.minimapCtx.fillStyle = "#ffffff", this.minimapCtx.fillRect(l, r, o, a), this.minimapCtx.save(), this.minimapCtx.translate(l, r), this.minimapCtx.scale(n, n), (this.currentObject ? [...this.objects, this.currentObject] : this.objects).forEach((d) => {
551
538
  switch (this.minimapCtx.fillStyle = d.color, this.minimapCtx.strokeStyle = d.color, this.minimapCtx.lineWidth = Math.max(d.lineWidth, 1), this.minimapCtx.setLineDash([]), d.type) {
552
539
  case "RECTANGLE": {
553
540
  const c = d;
@@ -579,10 +566,10 @@ class b extends HTMLElement {
579
566
  }
580
567
  // 小地图点击定位
581
568
  handleMinimapClick(t) {
582
- const i = this.minimapCanvas.getBoundingClientRect(), s = t.clientX - i.left, e = t.clientY - i.top, n = this.minimapCanvas.width / this.canvas.width, o = this.minimapCanvas.height / this.canvas.height, a = Math.min(n, o) * 0.92, l = this.canvas.width * a, r = this.canvas.height * a, h = (this.minimapCanvas.width - l) / 2, d = (this.minimapCanvas.height - r) / 2, c = s - h, p = e - d, u = (c / a - this.panOffset.x) / this.scale, m = (p / a - this.panOffset.y) / this.scale, g = this.canvas.width / 2, x = this.canvas.height / 2;
569
+ const i = this.minimapCanvas.getBoundingClientRect(), s = t.clientX - i.left, e = t.clientY - i.top, n = this.minimapCanvas.width / this.canvas.width, o = this.minimapCanvas.height / this.canvas.height, a = Math.min(n, o) * 0.92, l = this.canvas.width * a, r = this.canvas.height * a, h = (this.minimapCanvas.width - l) / 2, d = (this.minimapCanvas.height - r) / 2, c = s - h, p = e - d, u = (c / a - this.panOffset.x) / this.scale, g = (p / a - this.panOffset.y) / this.scale, m = this.canvas.width / 2, x = this.canvas.height / 2;
583
570
  this.panOffset = {
584
- x: g / this.scale - u,
585
- y: x / this.scale - m
571
+ x: m / this.scale - u,
572
+ y: x / this.scale - g
586
573
  }, this.renderCanvas(), this.renderMinimap();
587
574
  }
588
575
  // 图片上传处理
@@ -805,7 +792,7 @@ class b extends HTMLElement {
805
792
  </div>
806
793
 
807
794
  <!-- 空画布提示 -->
808
- <div class="empty-hint">
795
+ <div class="empty-hint" style="display: ${this.getAttribute("initial-data") ? "none" : "flex"};">
809
796
  <h3>开始创作</h3>
810
797
  <p>选择左侧的工具开始绘制</p>
811
798
  </div>