-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapes7.html
353 lines (292 loc) · 8.42 KB
/
shapes7.html
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
<!DOCTYPE html>
<html>
<head>
<title>Drawing Shapes</title>
<link rel="stylesheet" type="text/css" href="mystylesheet.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script src="http://www.html5canvastutorials.com/libraries/kinetic2d-v1.0.0.js"></script>
<script type="text/javascript" src="http://meta100.github.com/mColorPicker/javascripts/mColorPicker_min.js"></script>
<script type="text/javascript">
$.fn.mColorPicker.init.replace = false;//initialise color picker with no color
$(document).ready(function(){
//reading client window height and width
height=$(window).height()-50;
width=$(window).width()-120;
//initialise the canvas
init();
//to select a tool from the toolbar
$(".tools").click(function(){
$(".selected").removeClass("selected");
$(this).addClass("selected");
selectedTool=this.id;
context.lineWidth=1;
context.strokeStyle=linecol;
if(selectedTool=="clearall")
{
clearCanvas();
}
});
//to select background,line and foreground color
$('#bgcolor').mColorPicker();
$('#bgcolor').bind('colorpicked', function(){
bgcol=($(this).val());
$('#board').css("background-color",bgcol);
});
$('#fgcolor').mColorPicker();
$('#fgcolor').bind('colorpicked', function(){
fgcol=($(this).val());
context.fillStyle=fgcol;
});
$('#linecolor').mColorPicker();
$('#linecolor').bind('colorpicked', function(){
linecol=($(this).val());
context.strokeStyle=linecol;
});
});
</script>
</head>
<body>
<div id="container">
<div id="cnvs">
<canvas id="board">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
</div>
<div id="toolbar">
Toolbar
<div id="pencil" class="tools"> </div>
<div id="rect" class="tools"> </div>
<div id="line" class="tools"> </div>
<div id="circle" class="tools"> </div>
<div id="brush" class="tools"> </div>
<div id="cursor" class="tools"> </div>
<div id="eraser" class="tools"> </div>
<div id="clearall" class="tools"> </div>
</div>
</div>
<div id="palette">
<input id="bgcolor" type="color" placeholder="Background color"/>
<input id="linecolor" type="color" placeholder="Line color"/>
<input id="fgcolor" type="color" placeholder="Foreground color"/>
</div>
</body>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v1.2.0.js"></script>
<script type="text/javascript">
//variables for original and temporary canvas
var canvas,context;
var canvaso,contexto;
//starting coordinates
var startX=0;
var startY=0;
//ending coordinates
var endX=0;
var endY=0;
var diffX=0;
var diffY=0;
//height and width of canvas
var height=0;
var width=0;
var selectedTool;
//to store the status of mouse button up or down
var started;
//x and y will have the values of the coordinates of the mouse
var x,y;
//variables to store the selected color
var bgcol="white";
var linecol="black";
var fgcol;
//to create parent node for the canvas
var container;
//to show separate outline for the selected shape
var mySel;
var mySelColor = '#CC0000';
var mySelWidth = 1;
//to store the status of a rectangle being moved
var moveRectValid;
// holds all drawn rectangles
var boxes = [];
var kin;
// drag and drop globals
var offsetX = 0;
var offsetY = 0;
function init()
{
/*canvaso = document.getElementById('board');
contexto = canvaso.getContext('2d');
canvaso.height = height;
canvaso.width = width;*/
kin = new Kinetic("board", "2d");
canvaso = kin.getCanvas();
contexto = kin.getContext();
canvaso.height = height;
canvaso.width = width;
//creating a temporary canvas from which images will be copied onto the original canvas
container = canvaso.parentNode;
canvas = document.createElement('canvas');
canvas.id = 'tempImg';
canvas.width = width;
canvas.height = height;
container.appendChild(canvas);
context = canvas.getContext('2d');
started=false;
moveRectValid=false; //this is set to true only if the selected tool is cursor and a rectangle is selected to move
canvas.onselectstart = function () { return false; }; //initially nothing on the canvas can be selected
//adding event listeners
canvas.addEventListener('mousedown',mouseDown,false);
canvas.addEventListener('mouseup',mouseUp,false);
canvas.addEventListener('mousemove',mouseMove,false);
canvas.addEventListener('mouseout',mouseOut,false);
}
//if the mouse is clicked and dragged out of the canvas, nothing should be drawn
function mouseOut(event)
{
started=false;
}
//this defines what should be done when the mouse is moved
function mouseMove(event)
{
if (event.clientX || event.clientY == 0)
{
x=event.clientX;
y=event.clientY;
if(selectedTool=="pencil"||selectedTool=="brush"||selectedTool=="eraser")
{
if(selectedTool!="pencil")
{
context.lineWidth=5;
}
if(selectedTool=="eraser")
{
context.strokeStyle=bgcol;
}
if (started)
{
context.lineTo(x, y);
context.stroke();
}
else
{
context.beginPath();
context.moveTo(x, y);
}
}
else if(selectedTool=="clearall")
{
}
else if(selectedTool=="cursor")
{
//var parent=document.getElementById('board');
var removed=document.getElementById('tempImg');
//var oldChild=parent.removeChild(removed);*/
removed.parentNode.removeChild(removed);
}
else
{
if(started)
{
context.clearRect(0, 0,width,height);
drawShape(selectedTool,startX,startY,x,y);
}
}
}
}
/*only when the mouse button is pressed down, should anything be drawn.so before drawing an image,the value of started is checked and is drawn only if the value is true*/
function mouseDown(event)
{
startX=event.clientX;
startY=event.clientY;
started=true;
}
//only after the mouse button is left i.e., mouseup,the object is actually drawn onto the canvas depending on the tool
function mouseUp(event)
{
endX=event.clientX;
endY=event.clientY;
started=false;
drawShape(selectedTool,startX,startY,endX,endY);
if(selectedTool=="rect")
{
addRectangle(startX,startY,endX,endY);
console.log(boxes.length);
}
img_update();
}
//after every mouse up,the image drawn on the temporary canvas is redrawn onto the original canvas
function img_update()
{
kin.beginRegion();
contexto.beginPath();
contexto.drawImage(canvas, 0, 0);
contexto.closePath();
kin.addRegionEventListener("onmousedown",function(){
if(selectedTool="cursor")
alert("u selected this"));
});
kin.addRegionEventListener("onmouseup",alert("done"));
kin.closeRegion();
context.clearRect(0, 0, canvas.width, canvas.height);
}
function drawShape(tool,cornerX,cornerY,corner2X,corner2Y)
{
switch(tool)
{
//to draw a line
case "line":
{
context.beginPath();
context.moveTo(cornerX,cornerY);
context.lineTo(corner2X,corner2Y);
context.stroke();
context.closePath();
}
break;
//to draw a rectangle
case "rect":
{
diffX=-corner2X+cornerX;
diffY=-corner2Y+cornerY;
context.strokeRect(cornerX,cornerY,-diffX,-diffY);
}
break;
//to draw a circle
case "circle":
{
x=(cornerX+corner2X)/2;
y=(cornerY+corner2Y)/2;
diffX=(cornerX-corner2X)*(cornerX-corner2X);
diffY=(cornerY-corner2Y)*(cornerY-corner2Y);
var radius=Math.sqrt(diffX+diffY)/2;
context.beginPath();
context.arc(x,y,radius,0,2*Math.PI,false);
context.stroke();
}
break;
}
}
//to clear canvas
function clearCanvas()
{
contexto.clearRect(0,0,width,height);
}
//Box object to hold data for all drawn rects
function Box()
{
this.x = 0;
this.y = 0;
this.w = 1; // default width and height
this.h = 1;
}
//Initialize a new Box, add it, and invalidate the canvas
function addRectangle(cornerX,cornerY,corner2X,corner2Y)
{
var rect = new Box;
diffX=-corner2X+cornerX;
diffY=-corner2Y+cornerY;
rect.x = x;
rect.y = y;
rect.w = -diffX;
rect.h = -diffY;
boxes.push(rect);
}
</script>
</html>