ding-image-editor 3.15.7 → 3.15.8
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/dist/tui-image-editor.css +1 -1
- package/dist/tui-image-editor.js +85 -14
- package/dist/tui-image-editor.min.css +1 -1
- package/dist/tui-image-editor.min.js +4 -4
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/js/component/cropper.js +14 -2
- package/src/js/component/freeDrawing copy.js +178 -0
- package/src/js/component/freeDrawing.js +73 -5
- package/src/js/component/text.js +1 -3
- package/src/js/extension/cropzone.js +1 -1
package/index.d.ts
CHANGED
package/package.json
CHANGED
@@ -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
|
-
|
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 {
|
@@ -78,17 +78,83 @@ class FreeDrawing extends Component {
|
|
78
78
|
canvas.off('mouse:down', this._handlers.mousedown);
|
79
79
|
}
|
80
80
|
|
81
|
+
|
82
|
+
|
81
83
|
/**
|
82
84
|
* Set mosaic
|
83
85
|
*/
|
84
86
|
setMosaic(setting) {
|
85
|
-
|
86
|
-
this.
|
87
|
+
|
88
|
+
// this.imageEditor = setting.imageEditor;
|
89
|
+
// this.width = setting.width;
|
87
90
|
const canvas = this.getCanvas();
|
88
91
|
canvas.selection = false;
|
89
|
-
canvas.
|
92
|
+
canvas.isDrawingMode = true;
|
93
|
+
const ctx = canvas.contextContainer;
|
94
|
+
const originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
95
|
+
console.log(originalImageData)
|
96
|
+
|
97
|
+
let mosaicImageData = this.toMosaicImageData(originalImageData);
|
98
|
+
|
99
|
+
|
100
|
+
const patternCanvas = fabric.document.createElement('canvas');
|
101
|
+
const patternCtx = patternCanvas.getContext('2d');
|
102
|
+
patternCanvas.width = canvas.width
|
103
|
+
patternCanvas.height = canvas.height
|
104
|
+
patternCtx.putImageData(mosaicImageData,0,0)
|
105
|
+
|
106
|
+
var texturePatternBrush = new fabric.PatternBrush(canvas);
|
107
|
+
texturePatternBrush.source = patternCanvas;
|
108
|
+
canvas.freeDrawingBrush = texturePatternBrush;
|
109
|
+
canvas.freeDrawingBrush.width=setting.width
|
90
110
|
}
|
91
111
|
|
112
|
+
toMosaicImageData(imageData) {
|
113
|
+
// 定义马赛克方格大小(越大越模糊)
|
114
|
+
const suquareSize = 50;
|
115
|
+
let data = imageData.data;
|
116
|
+
const canvas ={ width:imageData.width,height:imageData.height}
|
117
|
+
//首先根据宽高遍历整个图片获取到对应的方格
|
118
|
+
for (let i = 0; i < canvas.height; i += suquareSize) {
|
119
|
+
for (let j = 0; j < canvas.width; j += suquareSize) {
|
120
|
+
let totalR = 0;
|
121
|
+
let totalG = 0;
|
122
|
+
let totalB = 0;
|
123
|
+
let totalA = 0;
|
124
|
+
let count = 0;
|
125
|
+
//遍历当前方格的每个像素将其RGBA值累加起来
|
126
|
+
for (let y = i; y < i + suquareSize && y < canvas.height; y++) {
|
127
|
+
for (let x = j; x < j + suquareSize && x < canvas.width; x++) {
|
128
|
+
//y * canvas.width + x就能计算出当前像素在整个图片中的索引
|
129
|
+
//再乘以4是因为imageData.data每个像素用4个值表示
|
130
|
+
//pixelIndex就是当前像素在imageData.data的起始索引也就是它的R值
|
131
|
+
let pixelIndex = (y * canvas.width + x) * 4;
|
132
|
+
totalR += data[pixelIndex];
|
133
|
+
totalG += data[pixelIndex + 1];
|
134
|
+
totalB += data[pixelIndex + 2];
|
135
|
+
totalA += data[pixelIndex + 3];
|
136
|
+
count++;
|
137
|
+
}
|
138
|
+
}
|
139
|
+
let avgR = totalR / count;
|
140
|
+
let avgG = totalG / count;
|
141
|
+
let avgB = totalB / count;
|
142
|
+
let avgA = totalA / count;
|
143
|
+
// 遍历的逻辑与上面一模一样,这一步是将方格内的每个像素的RGBA值替换为平均值
|
144
|
+
for (let y = i; y < i + suquareSize && y < canvas.height; y++) {
|
145
|
+
for (let x = j; x < j + suquareSize && x < canvas.width; x++) {
|
146
|
+
let pixelIndex = (y * canvas.width + x) * 4;
|
147
|
+
data[pixelIndex] = avgR;
|
148
|
+
data[pixelIndex + 1] = avgG;
|
149
|
+
data[pixelIndex + 2] = avgB;
|
150
|
+
data[pixelIndex + 3] = avgA;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
return imageData;
|
156
|
+
}
|
157
|
+
|
92
158
|
/**
|
93
159
|
* MouseDown event handler on canvas
|
94
160
|
* @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event object
|
@@ -151,7 +217,9 @@ class FreeDrawing extends Component {
|
|
151
217
|
const response = await fetch(dataURL);
|
152
218
|
const blob = await response.blob();
|
153
219
|
const imgUrl = URL.createObjectURL(blob);
|
154
|
-
this.imageEditor.invoke('addImageObject', imgUrl);
|
220
|
+
//this.imageEditor.invoke('addImageObject', imgUrl);
|
221
|
+
this.imageEditor.addImageObject(imgUrl)
|
222
|
+
console.log(this.imageEditor)
|
155
223
|
const canvas = this.getCanvas();
|
156
224
|
canvas.off({
|
157
225
|
'mouse:move': this._handlers.mousemove,
|
package/src/js/component/text.js
CHANGED
@@ -488,7 +488,7 @@ class Text extends Component {
|
|
488
488
|
* @private
|
489
489
|
*/
|
490
490
|
_onFabricSelect(fEvent) {
|
491
|
-
console.log('
|
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.
|
115
|
+
this._fillOuterRect(ctx, 'rgba(0, 0, 0, 0.3)');
|
116
116
|
|
117
117
|
if (this.options.lineWidth) {
|
118
118
|
this._fillInnerRect(ctx);
|