-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapes13.html
337 lines (286 loc) · 7.83 KB
/
shapes13.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
<!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 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!="cursor")
{
if(document.getElementById('board').childNodes.length==1)
{
if((selectedTool=="rect")||(selectedTool=="circle")||(selectedTool=="line"))
{
document.getElementById('cnvs').style.cursor="crosshair";
}
else if(selectedTool=="pencil")
{
$('#cnvs').css('cursor',"url('Pencil.cur'),auto");
}
else if(selectedTool=="brush")
{
$('#cnvs').css('cursor',"url('Brush.cur'),auto");
}
else if(selectedTool=="eraser")
{
$('#cnvs').css('cursor',"url('eraser.cur'),auto");
}
else
{
document.getElementById('cnvs').style.cursor="default";
}
addTempCanvas();
}
if(selectedTool=="clearall")
{
clearCanvas();
}
}
else
{
$('#tempImg').remove();
}
});
//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 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; //not used
//to create parent node for the canvas
var container;
var parent=document.getElementById('board');
// 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;
addTempCanvas();
started=false;
}
//creating a temporary canvas from which images will be copied onto the original canvas
function addTempCanvas()
{
container = canvaso.parentNode;
canvas = document.createElement('canvas');
canvas.id = 'tempImg';
canvas.width = width;
canvas.height = height;
container.appendChild(canvas);
context = canvas.getContext('2d');
//adding event listeners
canvas.addEventListener('mousedown',mouseDown,false);
canvas.addEventListener('mouseup',mouseUp,false);
canvas.addEventListener('mousemove',mouseMove,false);
canvas.addEventListener('mouseout',mouseOut,false);
canvaso.addEventListener('mouseup', canvMouse, false);
}
function canvMouse(event)
{
if (selectedTool == "cursor")
{
startX=event.screenX;
startY=event.screenY;
}
}
//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)
{
context.strokeStyle=linecol;
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")
{
}*/
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);
img_update();
}
//after every mouse up,the image drawn on the temporary canvas is redrawn onto the original canvas
function img_update()
{
contexto.beginPath();
contexto.drawImage(canvas, 0, 0);
contexto.closePath();
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);
}
</script>
</html>