-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawing.js
More file actions
261 lines (235 loc) · 8.5 KB
/
drawing.js
File metadata and controls
261 lines (235 loc) · 8.5 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
//default canvas
var myCanvas;
/*
mediante canvas lavoro su un contesto, ovvero un oggetto che rappresenta l'intera bitmap del canvas, mediantel'utilizzo
di fabricjs vado a manipolare invece degli oggetti direttamente, potendone modificare le proprietà in modo indipendente
*/
//layer between myCanvas and the drawed objects
var canvF;
//supporting structures
var gomma;
var rettangoli;
var cerchi;
var tempCanvF;
//flag
var gommaIn = false;
var elemento;
var posX;
var posY;
//generatore casuale di colore
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var rand_color = '#';
for (var i = 0; i < 6; i++) {
rand_color += letters[Math.floor(Math.random() * 16)];
}
return rand_color;
}
//initializing container, canvas and fabric canvas
function initF() {
gommaIn = false;
canvasContainer = document.createElement('div');
document.body.appendChild(canvasContainer);
canvasContainer.style.position = "absolute";
canvasContainer.style.left = "0px";
canvasContainer.style.top = "0px";
canvasContainer.style.width = "100%";
canvasContainer.style.height = "100%";
canvasContainer.style.zIndex = "1000";
superContainer = document.body;
myCanvas = document.createElement('canvas');
myCanvas.style.width = superContainer.scrollWidth + "px";
myCanvas.style.height = superContainer.scrollHeight + "px";
//scrollwidth include anche il padding, ma non il margin, il border e scrollbar, da il valore in pixel
//la larghezza totale del contenitore
//width indica la larghezza dell'area visibile di un oggetto
myCanvas.width = superContainer.scrollWidth;
myCanvas.height = superContainer.scrollHeight;
myCanvas.style.position = 'absolute';
canvasContainer.appendChild(myCanvas);
canvF = new fabric.Canvas(myCanvas);
tempCanvF = new fabric.Canvas(myCanvas);
rettangoli = new fabric.Canvas(myCanvas);
//options object has 2 property: e, the original event, and target, the(eventually) selected object
//selected object always on top
canvF.on('object:selected', function (options) {
//remove target from the array, then put it on top and redraw
canvF.bringToFront(options.target);
});
//need it for the eraser
canvF.on('object:moving', onChange);
canvF.selection = false;
}
//custom colors
function defBackColor() {
elemento = document.getElementById('rect');
elemento.style.backgroundColor = "rgb(112, 210, 177)";
elemento = document.getElementById('pencil');
elemento.style.backgroundColor = "rgb(13, 155, 229)";
elemento = document.getElementById('cerchio');
elemento.style.backgroundColor = "rgb(3, 176, 176)";
elemento = document.getElementById('gomma');
elemento.style.backgroundColor = "blue";
elemento = document.getElementById('anima');
elemento.style.backgroundColor = "rgb(255, 117, 0)";
elemento = document.getElementById('lnkDownload');
elemento.style.backgroundColor = "red";
}
function drawRectF() {
//flag di disegno a mano libera disattivato
canvF.isDrawingMode = false;
defBackColor();
elemento = document.getElementById('rect');
elemento.style.backgroundColor = "black";
gommaIn = false;
canvF.remove(gomma);
var rect = new fabric.Rect();
rect.left = 100;
rect.top = 100;
var coloreRect = getRandomColor();
rect.fill = coloreRect;
rect.width = 80;
rect.height = 80;
rect.borderColor = 'red';
rect.cornerColor = 'green';
rect.cornerSize = 20;
rect.eraser = false;
//controls invisible
transparentCorners = false;
rettangoli.add(rect);
canvF.add(rect);
}
//gravity for rects, easeOutBounce as easeIn function
function animaF() {
defBackColor();
elemento = document.getElementById('anima');
elemento.style.backgroundColor = "black";
//for each rects
rettangoli.forEachObject(function (obj) {
//animate:proprietà da animare, intervallo,parametri:render the canvas onChange, durata animazione, funzione easin
obj.animate('top', myCanvas.height - obj.height, {
onChange: canvF.renderAll.bind(canvF),
duration: 2000,
easing: fabric.util.ease.easeOutBounce
});
});
}
function drawCircleF() {
canvF.isDrawingMode = false;
gommaIn = false;
defBackColor();
elemento = document.getElementById('cerchio');
elemento.style.backgroundColor = "black";
canvF.remove(gomma);
var circle = new fabric.Circle({ radius: 30, fill: getRandomColor(), top: myCanvas.height / 2, left: myCanvas.width / 2 });
circle.borderColor = 'red';
circle.cornerColor = 'green';
circle.cornerSize = 20;
circle.eraser = false;
transparentCorners = false;
canvF.add(circle);
}
function eraseF() {
if (!gommaIn) {
defBackColor();
elemento = document.getElementById('gomma');
elemento.style.backgroundColor = "black";
//eraser svg path
fabric.loadSVGFromURL('images/eraser9.svg', function (objects, options) {
var obj = fabric.util.groupSVGElements(objects, options);
obj.scale(0.2);
//la posizione in alto a destra
obj.left = myCanvas.width - obj.width * 0.2;
obj.top = 50;
gomma = obj;
gomma.eraser = true;
transparentCorners = false;
gommaIn = true;
canvF.isDrawingMode = false;
gomma.hasBorders = false;
gomma.hasControls = false;
//settandolo a true, l'hit system è valutato sul perimetro esatto della figura
perPixelTargetFind = true;
canvF.add(gomma).renderAll();
});
}
else {
gommaIn = false;
canvF.remove(gomma);
elemento = document.getElementById('gomma');
elemento.style.backgroundColor = "blue";
}
}
function drawLineF() {
canvF.isDrawingMode = !canvF.isDrawingMode;
if (canvF.isDrawingMode) {
defBackColor();
elemento = document.getElementById('pencil');
elemento.style.backgroundColor = "black";
gommaIn = false;
canvF.remove(gomma);
freeDrawingBrush.color = getRandomColor();
freeDrawingBrush.width = 10;
perPixelTargetFind = true;
}
else {
elemento = document.getElementById('pencil');
elemento.style.backgroundColor = "rgb(13, 155, 229)";
}
}
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
function handleImage(e) {
var reader = new FileReader();
//al termine del caricamento del file, viene generato l'evento load
reader.onload = function (event) {
var img = new Image();
img.onload = function () {
canvF.isDrawingMode = false;
var imgInstance = new fabric.Image(img, {
selectable: 1
})
if (imgInstance.width > myCanvas.width || imgInstance.height > myCanvas.height) {
imgInstance.scale(0.4);
}
canvF.add(imgInstance);
canvF.deactivateAll().renderAll();
}
//src specifica l'url dell'immagine
img.src = event.target.result;
}
//leggo il file immagine come file binario
reader.readAsDataURL(e.target.files[0]);
if (gommaIn) {
gommaIn = false;
canvF.remove(gomma);
}
elemento.style.backgroundColor = "rgb(3, 176, 176)";
elemento = document.getElementById('gomma');
}
var imageSaver = document.getElementById('lnkDownload');
imageSaver.addEventListener('click', saveImage, false);
function saveImage(e) {
defBackColor();
elemento = document.getElementById('lnkDownload');
elemento.style.backgroundColor = "black";
//serializzazione di canvF
this.href = canvF.toDataURL({
format: 'png',
quality: 1.0
});
this.download = 'canvas.png'
}
//only if i'm the eraser, when i pass over another object i remove it
var onChange = function (options) {
if (!options.target.eraser) {
return;
}
//per aggiornare la control area dell'oggetto
options.target.setCoords();
canvF.forEachObject(function (obj) {
if (!obj.eraser && options.target.intersectsWithObject(obj)) {
canvF.remove(obj);
}
});
}