-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.js
More file actions
353 lines (303 loc) · 8.7 KB
/
Copy pathshape.js
File metadata and controls
353 lines (303 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
var canvas = this.__canvas = new fabric.Canvas('mainCanvas');
const myCanvas = canvas.getElement();
const ctx = myCanvas.getContext('2d');
canvas.setWidth(window.innerWidth);
canvas.setHeight(window.innerHeight);
window.addEventListener('resize',function(){
canvas.setWidth(window.innerWidth);
canvas.setHeight(window.innerHeight);
})
let fillColor = 'white';
let strokeColor = 'black'
let strokeWidth = 3;
let bgColor = 'white';
let colorLayerData=ctx.getImageData(0, 0, canvas.getWidth(), canvas.getHeight());
let isPaintBucket = false;
canvas.isDrawingMode = false;
canvas.selectionColor = 'rgba(255, 255, 255, 0.3)';
canvas.selectionBorderColor = 'blue';
canvas.selectionLineWidth = 1;
fabric.Object.prototype.transparentCorners = false;
fabric.Object.prototype.cornerColor = 'blue';
fabric.Object.prototype.cornerStyle = 'square';
fabric.Object.prototype.noScaleCache = false;
fabric.Object.prototype.cornerSize = 8;
document.getElementById('strokeColor').addEventListener('input',function(e){
strokeColor = e.target.value;
var activeObjects = canvas.getActiveObjects();
if (activeObjects) {
activeObjects.forEach(function(obj) {
obj.set('stroke',strokeColor);
obj.set()
})
canvas.renderAll();
}
if (canvas.isDrawingMode) {
canvas.freeDrawingBrush.color = strokeColor
}
})
document.getElementById('fillColor').addEventListener('input',function(e){
fillColor = e.target.value;
var activeObjects = canvas.getActiveObjects();
if (activeObjects) {
activeObjects.forEach(function(obj) {
obj.set('fill',fillColor);
})
canvas.renderAll();
}
})
document.getElementById('strokeWidth').addEventListener('input',function(e){
strokeWidth = e.target.value;
var activeObjects = canvas.getActiveObjects();
if (activeObjects) {
activeObjects.forEach(function(obj) {
obj.set('strokeWidth',strokeWidth);
obj.set()
})
canvas.renderAll();
}
if (canvas.isDrawingMode) {
canvas.freeDrawingBrush.width = strokeWidth
}
})
function paintBucketOn() {
Select();
AllBrushOff();
isPaintBucket = true;
}
function Select() {
AllBrushOff()
canvas.isDrawingMode = false;
isPaintBucket = false;
myCanvas.style.cursor = 'default'
}
function Erase() {
canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.color = bgColor;
canvas.freeDrawingBrush.width = strokeWidth;
canvas.isDrawingMode = true;
}
function Copy() {
canvas.getActiveObject().clone(function(cloned) {
_clipboard = cloned;
});
}
function Paste() {
_clipboard.clone(function(clonedObj) {
canvas.discardActiveObject();
clonedObj.set({
left: clonedObj.left + 10,
top: clonedObj.top + 10,
evented: true,
});
if (clonedObj.type === 'activeSelection') {
// active selection needs a reference to the canvas.
clonedObj.canvas = canvas;
clonedObj.forEachObject(function(obj) {
canvas.add(obj);
});
// this should solve the unselectability
clonedObj.setCoords();
} else {
canvas.add(clonedObj);
}
_clipboard.top += 10;
_clipboard.left += 10;
canvas.setActiveObject(clonedObj);
canvas.requestRenderAll();
});
}
function Cut() {
Copy();
var activeObjects = canvas.getActiveObjects();
// Check if an object is selected
if (activeObjects) {
// Remove the selected object from the canvas
activeObjects.forEach(function (object) {
canvas.remove(object);
})
}
canvas.setActiveObject(null);
}
document.addEventListener('copy',function(e){
Copy();
navigator.clipboard.writeText("fabricObject");
e.preventDefault();
})
document.addEventListener('paste',function(e){
if (e.clipboardData && e.clipboardData.items) {
if (e.clipboardData.items[e.clipboardData.items.length-1].type.indexOf('image') !== -1 ) {
// create a file object from the clipboard data
var file = e.clipboardData.items[e.clipboardData.items.length-1].getAsFile();
// create a URL object from the file object
var url = URL.createObjectURL(file);
// create an image object from the URL
fabric.Image.fromURL(url, function(img) {
// set the position and size of the image
img.set({
left: 100,
top: 50,
width: img.width,
height: img.height
});
// add the image to the canvas
canvas.add(img);
// render the canvas to update the changes
canvas.renderAll();
});
// prevent the default paste action
e.preventDefault();
}
e.clipboardData.items[e.clipboardData.items.length-1].getAsString(function(data){
if (data==='fabricObject') {
Paste();
}
})
}
})
document.addEventListener('cut',function(){
Cut();
})
document.addEventListener('keydown', function(event) {
if (event.keyCode === 46) { // 46 is the keycode for 'delete' key
// Get the currently selected object
var activeObjects = canvas.getActiveObjects();
// Check if an object is selected
if (activeObjects) {
// Remove the selected object from the canvas
activeObjects.forEach(function (object) {
canvas.remove(object);
})
canvas.setActiveObject(null)
}
}
});
// functions to add shapes
function AddRectangle() {
var rect = new fabric.Rect({
left: 100,
top: 50,
fill: fillColor,
width: 200,
height: 100,
objectCaching: false,
stroke: strokeColor,
strokeWidth: strokeWidth,
strokeUniform: true
});
canvas.add(rect);
canvas.setActiveObject(rect);
Select();
}
function AddCircle() {
var circle = new fabric.Circle({
top:50,
left: 100,
fill: fillColor,
radius: 100,
objectCaching: false,
stroke: strokeColor,
strokeWidth: strokeWidth,
strokeUniform: true
});
canvas.add(circle);
canvas.setActiveObject(circle);
Select();
}
function AddEllipse(){
var ellipse = new fabric.Ellipse({
top:50,
left: 100,
fill: fillColor,
rx:150,
ry:75,
objectCaching: false,
stroke: strokeColor,
strokeWidth: strokeWidth,
strokeUniform: true
});
canvas.add(ellipse);
canvas.setActiveObject(ellipse);
Select();
}
function AddTriangle(){
var triangle = new fabric.Triangle({
top:50,
left: 100,
fill: fillColor,
width:200,
height:100,
objectCaching: false,
stroke: strokeColor,
strokeWidth: strokeWidth,
strokeUniform: true
});
canvas.add(triangle);
canvas.setActiveObject(triangle);
Select();
}
function AddLine() {
var line = new fabric.Line([100, 100, 200, 200], {
stroke: 'black',
strokeWidth: 2,
});
canvas.add(line);
canvas.setActiveObject(line);
Select();
}
function AddSquare() {
var rect = new fabric.Rect({
left: 100,
top: 50,
fill: fillColor,
width: 100,
height: 100,
objectCaching: false,
stroke: strokeColor,
strokeWidth: strokeWidth,
strokeUniform: true
});
canvas.add(rect);
canvas.setActiveObject(rect);
Select();
}
canvas.on('mouse:down',function(ev) {
if (isPaintBucket) {
paintBucket(ev)
}
})
function paintBucket(ev) {
// Get the fill color from the foreground element
// Get the current mouse position
let pointer = canvas.getPointer(ev.e);
// Find the object at the current mouse position
let target = canvas.findTarget(pointer);
if (target) {
// If an object is found, fill it with the selected color
target.set({ fill: fillColor });
canvas.renderAll();
} else {
// If no object is found, fill the entire canvas with the selected color
canvas.setBackgroundColor(fillColor, canvas.renderAll.bind(canvas));
}
}
function DownloadPng() {
let img = canvas.toDataURL('image/png');
var link = document.createElement('a');
link.href = img;
link.download = 'my-image.png';
link.click();
document.body.removeChild(link);
}
function shapeAutocomplete() {
canvas.isDrawingMode = true;
canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.width = 3;
canvas.freeDrawingBrush.
canvas.on('path:created', function(options) {
canvas.setActiveObject(options.path);
var selectedPath = canvas.getActiveObject();
var img = new Image();
img.src = selectedPath.toDataURL();
})
}