ding-image-editor 3.15.7 → 3.15.9

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Type definitions for TOAST UI Image Editor v3.15.6
1
+ // Type definitions for TOAST UI Image Editor v3.15.8
2
2
  // TypeScript Version: 3.2.2
3
3
 
4
4
  declare namespace tuiImageEditor {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ding-image-editor",
3
- "version": "3.15.7",
3
+ "version": "3.15.9",
4
4
  "description": "TOAST UI ImageEditor",
5
5
  "keywords": [
6
6
  "nhn",
@@ -103,8 +103,20 @@ class Cropper extends Component {
103
103
  canvas.add(this._cropzone);
104
104
  canvas.on('mouse:down', this._listeners.mousedown);
105
105
  canvas.selection = false;
106
- canvas.defaultCursor = 'crosshair';
107
-
106
+
107
+ const svgString = `<?xml version="1.0" encoding="UTF-8"?>
108
+ <svg width="22px" height="22px" viewBox="0 0 22 22" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
109
+ <title>ddd</title>
110
+ <g id="1130" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
111
+ <g id="dd" transform="translate(-775, -430)" fill="#2F2F46" fill-rule="nonzero" stroke="#FFFFFF" stroke-width="0.5">
112
+ <path d="M786.750006,430.75 L786.749908,434.79455 C788.176052,434.965136 789.454697,435.615862 790.419417,436.580583 C791.384137,437.545303 792.034864,438.823948 792.20545,440.250092 L796.25,440.249994 L796.25,441.749944 L792.205337,441.750853 C792.034555,443.1768 791.383757,444.45524 790.419045,445.41979 C789.454368,446.384305 788.175868,447.034886 786.749908,447.20545 L786.750006,451.25 L785.250056,451.25 L785.249147,447.205337 C783.823384,447.034577 782.54509,446.383924 781.580583,445.419417 C780.616076,444.45491 779.965423,443.176616 779.794663,441.750853 L775.75,441.749944 L775.75,440.249994 L779.79455,440.250092 C779.965114,438.824132 780.615695,437.545632 781.58021,436.580955 C782.54476,435.616243 783.8232,434.965445 785.249147,434.794663 L785.250056,430.75 L786.750006,430.75 Z M785.249017,441.750039 L781.308947,441.750657 C781.469428,442.761766 781.949058,443.666572 782.641243,444.358757 C783.333413,445.050927 784.238196,445.530552 785.249279,445.691043 L785.249017,441.750039 Z M790.691053,441.750657 L786.749039,441.750039 L786.749655,445.691212 C787.761002,445.530915 788.666047,445.051328 789.358416,444.359098 C790.050784,443.66687 790.530546,442.761932 790.691053,441.750657 Z M785.249279,436.308957 C784.23803,436.469474 783.333116,436.94923 782.640902,437.641584 C781.948686,438.333939 781.469105,439.23896 781.308798,440.250281 L785.249017,440.250017 Z M786.749655,436.308788 L786.749039,440.250017 L790.691202,440.250281 C790.530869,439.238794 790.051156,438.333642 789.358757,437.641243 C788.666344,436.948829 787.761168,436.469111 786.749655,436.308788 Z" id="ss"></path>
113
+ </g>
114
+ </g>
115
+ </svg>
116
+ `
117
+ // 将 SVG 源码转换为 Base64 格式
118
+ const base64 = btoa(svgString);
119
+ canvas.defaultCursor = `url("data:image/svg+xml;base64,${base64}"),auto`;
108
120
  fabric.util.addListener(document, 'keydown', this._listeners.keydown);
109
121
  fabric.util.addListener(document, 'keyup', this._listeners.keyup);
110
122
  }
@@ -0,0 +1,178 @@
1
+ import { fabric } from 'fabric';
2
+ import Component from '@/interface/component';
3
+ import { componentNames } from '@/consts';
4
+
5
+ /**
6
+ * FreeDrawing
7
+ * @class FreeDrawing
8
+ * @param {Graphics} graphics - Graphics instance
9
+ * @extends {Component}
10
+ * @ignore
11
+ */
12
+ class FreeDrawing extends Component {
13
+ constructor(graphics) {
14
+ super(componentNames.FREE_DRAWING, graphics);
15
+
16
+ /**
17
+ * Brush width
18
+ * @type {number}
19
+ */
20
+ this.width = 12;
21
+
22
+ /**
23
+ * fabric.Color instance for brush color
24
+ * @type {fabric.Color}
25
+ */
26
+ this.oColor = new fabric.Color('rgba(0, 0, 0, 0.5)');
27
+ this._handlers = {
28
+ mousedown: this._onFabricMouseDown.bind(this),
29
+ mousemove: this._onFabricMouseMove.bind(this),
30
+ mouseup: this._onMasikMouseUp.bind(this),
31
+ };
32
+
33
+ /**
34
+ * imageEditor instance
35
+ */
36
+ this.imageEditor = null;
37
+ }
38
+
39
+ /**
40
+ * Start free drawing mode
41
+ * @param {{width: ?number, color: ?string}} [setting] - Brush width & color
42
+ */
43
+ start(setting) {
44
+ console.log(setting);
45
+ if (setting?.mosaic) {
46
+ this.setMosaic(setting);
47
+ } else {
48
+ const canvas = this.getCanvas();
49
+ console.log(canvas);
50
+ canvas.isDrawingMode = true;
51
+ this.setBrush(setting);
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Set brush
57
+ * @param {{width: ?number, color: ?string}} [setting] - Brush width & color
58
+ */
59
+ setBrush(setting) {
60
+ const brush = this.getCanvas().freeDrawingBrush;
61
+
62
+ setting = setting || {};
63
+ this.width = setting.width || this.width;
64
+ if (setting.color) {
65
+ this.oColor = new fabric.Color(setting.color);
66
+ }
67
+ brush.width = this.width;
68
+ brush.color = this.oColor.toRgba();
69
+ }
70
+
71
+ /**
72
+ * End free drawing mode
73
+ */
74
+ end() {
75
+ const canvas = this.getCanvas();
76
+ canvas.isDrawingMode = false;
77
+ canvas.selection = true;
78
+ canvas.off('mouse:down', this._handlers.mousedown);
79
+ }
80
+
81
+
82
+
83
+ /**
84
+ * Set mosaic
85
+ */
86
+ setMosaic(setting) {
87
+
88
+ this.imageEditor = setting.imageEditor;
89
+ this.width = setting.width;
90
+ const canvas = this.getCanvas();
91
+ canvas.selection = false;
92
+ canvas.isDrawingMode = true;
93
+
94
+ const url = localStorage.getItem('mosaicImge')
95
+ console.log('mosic image',url)
96
+ var img222 = new Image();
97
+ img222.src = url;
98
+ var texturePatternBrush = new fabric.PatternBrush(canvas);
99
+ texturePatternBrush.source = img222;
100
+ canvas.freeDrawingBrush = texturePatternBrush;
101
+ canvas.freeDrawingBrush.width=30
102
+ //canvas.on('mouse:down', this._handlers.mousedown);
103
+ }
104
+
105
+ /**
106
+ * MouseDown event handler on canvas
107
+ * @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event object
108
+ * @private
109
+ */
110
+ _onFabricMouseDown() {
111
+ const canvas = this.getCanvas();
112
+ canvas.on({
113
+ 'mouse:move': this._handlers.mousemove,
114
+ 'mouse:up': this._handlers.mouseup,
115
+ });
116
+ }
117
+
118
+ /**
119
+ * MouseDown event handler on canvas
120
+ * @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event object
121
+ * @private
122
+ */
123
+ _onFabricMouseMove(fEvent) {
124
+ const canvas = this.getCanvas();
125
+ const blockSize = this.width;
126
+ const halfSize = this.width / 2;
127
+ // 直接获取e中的x,y有问题
128
+ const startPoints = canvas.getPointer(fEvent.e);
129
+ console.log(startPoints);
130
+ const startX = startPoints.x - halfSize;
131
+ console.log(startX);
132
+ const startY = startPoints.y - halfSize;
133
+ const ctx = canvas.contextContainer;
134
+ const imageData = ctx.getImageData(startX, startY, blockSize, blockSize);
135
+ const { data } = imageData;
136
+ let r = 0;
137
+ let g = 0;
138
+ let b = 0;
139
+ for (let i = 0; i < data.length; i += 4) {
140
+ r += data[i];
141
+ g += data[i + 1];
142
+ b += data[i + 2];
143
+ }
144
+ r = Math.floor(r / (blockSize * blockSize));
145
+ g = Math.floor(g / (blockSize * blockSize));
146
+ b = Math.floor(b / (blockSize * blockSize));
147
+ for (let i = 0; i < data.length; i += 4) {
148
+ data[i] = r;
149
+ data[i + 1] = g;
150
+ data[i + 2] = b;
151
+ data[i + 3] = 255; // Alpha值
152
+ }
153
+ ctx.putImageData(imageData, startX, startY);
154
+ }
155
+
156
+ /**
157
+ * MouseUp event handler on canvas
158
+ * @private
159
+ */
160
+ async _onMasikMouseUp() {
161
+ const doms = document.getElementsByClassName('lower-canvas');
162
+ const [domCanvas] = doms;
163
+ const dataURL = domCanvas.toDataURL();
164
+ const response = await fetch(dataURL);
165
+ const blob = await response.blob();
166
+ const imgUrl = URL.createObjectURL(blob);
167
+ //this.imageEditor.invoke('addImageObject', imgUrl);
168
+ this.imageEditor.addImageObject(imgUrl)
169
+ console.log(this.imageEditor)
170
+ const canvas = this.getCanvas();
171
+ canvas.off({
172
+ 'mouse:move': this._handlers.mousemove,
173
+ 'mouse:up': this._handlers.mouseup,
174
+ });
175
+ }
176
+ }
177
+
178
+ export default FreeDrawing;
@@ -41,7 +41,7 @@ class FreeDrawing extends Component {
41
41
  * @param {{width: ?number, color: ?string}} [setting] - Brush width & color
42
42
  */
43
43
  start(setting) {
44
- console.log(setting);
44
+ console.log('drawingsetting',setting);
45
45
  if (setting?.mosaic) {
46
46
  this.setMosaic(setting);
47
47
  } else {
@@ -57,7 +57,10 @@ class FreeDrawing extends Component {
57
57
  * @param {{width: ?number, color: ?string}} [setting] - Brush width & color
58
58
  */
59
59
  setBrush(setting) {
60
- const brush = this.getCanvas().freeDrawingBrush;
60
+ const canvas = this.getCanvas()
61
+ let pencilBrush = new fabric.PencilBrush(canvas)
62
+ canvas.freeDrawingBrush = pencilBrush;
63
+ const brush = canvas.freeDrawingBrush;
61
64
 
62
65
  setting = setting || {};
63
66
  this.width = setting.width || this.width;
@@ -78,17 +81,81 @@ class FreeDrawing extends Component {
78
81
  canvas.off('mouse:down', this._handlers.mousedown);
79
82
  }
80
83
 
84
+
85
+
81
86
  /**
82
87
  * Set mosaic
83
88
  */
84
89
  setMosaic(setting) {
85
- this.imageEditor = setting.imageEditor;
86
- this.width = setting.width;
90
+
87
91
  const canvas = this.getCanvas();
88
92
  canvas.selection = false;
89
- canvas.on('mouse:down', this._handlers.mousedown);
93
+ canvas.isDrawingMode = true;
94
+ const ctx = canvas.contextContainer;
95
+ const originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
96
+ console.log(originalImageData)
97
+
98
+ let mosaicImageData = this.toMosaicImageData(originalImageData);
99
+
100
+
101
+ const patternCanvas = fabric.document.createElement('canvas');
102
+ const patternCtx = patternCanvas.getContext('2d');
103
+ patternCanvas.width = canvas.width
104
+ patternCanvas.height = canvas.height
105
+ patternCtx.putImageData(mosaicImageData,0,0)
106
+
107
+ var texturePatternBrush = new fabric.PatternBrush(canvas);
108
+ texturePatternBrush.source = patternCanvas;
109
+ canvas.freeDrawingBrush = texturePatternBrush;
110
+ canvas.freeDrawingBrush.width=setting.width
90
111
  }
91
112
 
113
+ toMosaicImageData(imageData) {
114
+ // 定义马赛克方格大小(越大越模糊)
115
+ const suquareSize = 50;
116
+ let data = imageData.data;
117
+ const canvas ={ width:imageData.width,height:imageData.height}
118
+ //首先根据宽高遍历整个图片获取到对应的方格
119
+ for (let i = 0; i < canvas.height; i += suquareSize) {
120
+ for (let j = 0; j < canvas.width; j += suquareSize) {
121
+ let totalR = 0;
122
+ let totalG = 0;
123
+ let totalB = 0;
124
+ let totalA = 0;
125
+ let count = 0;
126
+ //遍历当前方格的每个像素将其RGBA值累加起来
127
+ for (let y = i; y < i + suquareSize && y < canvas.height; y++) {
128
+ for (let x = j; x < j + suquareSize && x < canvas.width; x++) {
129
+ //y * canvas.width + x就能计算出当前像素在整个图片中的索引
130
+ //再乘以4是因为imageData.data每个像素用4个值表示
131
+ //pixelIndex就是当前像素在imageData.data的起始索引也就是它的R值
132
+ let pixelIndex = (y * canvas.width + x) * 4;
133
+ totalR += data[pixelIndex];
134
+ totalG += data[pixelIndex + 1];
135
+ totalB += data[pixelIndex + 2];
136
+ totalA += data[pixelIndex + 3];
137
+ count++;
138
+ }
139
+ }
140
+ let avgR = totalR / count;
141
+ let avgG = totalG / count;
142
+ let avgB = totalB / count;
143
+ let avgA = totalA / count;
144
+ // 遍历的逻辑与上面一模一样,这一步是将方格内的每个像素的RGBA值替换为平均值
145
+ for (let y = i; y < i + suquareSize && y < canvas.height; y++) {
146
+ for (let x = j; x < j + suquareSize && x < canvas.width; x++) {
147
+ let pixelIndex = (y * canvas.width + x) * 4;
148
+ data[pixelIndex] = avgR;
149
+ data[pixelIndex + 1] = avgG;
150
+ data[pixelIndex + 2] = avgB;
151
+ data[pixelIndex + 3] = avgA;
152
+ }
153
+ }
154
+ }
155
+ }
156
+ return imageData;
157
+ }
158
+
92
159
  /**
93
160
  * MouseDown event handler on canvas
94
161
  * @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event object
@@ -151,7 +218,9 @@ class FreeDrawing extends Component {
151
218
  const response = await fetch(dataURL);
152
219
  const blob = await response.blob();
153
220
  const imgUrl = URL.createObjectURL(blob);
154
- this.imageEditor.invoke('addImageObject', imgUrl);
221
+ //this.imageEditor.invoke('addImageObject', imgUrl);
222
+ this.imageEditor.addImageObject(imgUrl)
223
+ console.log(this.imageEditor)
155
224
  const canvas = this.getCanvas();
156
225
  canvas.off({
157
226
  'mouse:move': this._handlers.mousemove,
@@ -488,7 +488,7 @@ class Text extends Component {
488
488
  * @private
489
489
  */
490
490
  _onFabricSelect(fEvent) {
491
- console.log('sssssssddddddds')
491
+ console.log('ssssssss')
492
492
  this.isPrevEditing = true;
493
493
 
494
494
  this.setSelectedInfo(fEvent.target, true);
@@ -525,8 +525,6 @@ class Text extends Component {
525
525
  const e = fEvent.e || {};
526
526
  const originPointer = this.getCanvas().getPointer(e);
527
527
 
528
- console.log('_fireAddText')
529
-
530
528
  if (!obj) {
531
529
  this.fire(events.ADD_TEXT, {
532
530
  originPosition: {
@@ -112,7 +112,7 @@ const Cropzone = fabric.util.createClass(
112
112
  ctx.scale(originalScaleX, originalScaleY);
113
113
 
114
114
  // Render outer rect
115
- this._fillOuterRect(ctx, 'rgba(0, 0, 0, 0.5)');
115
+ this._fillOuterRect(ctx, 'rgba(0, 0, 0, 0.3)');
116
116
 
117
117
  if (this.options.lineWidth) {
118
118
  this._fillInnerRect(ctx);