-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapes2.html
360 lines (303 loc) · 7.9 KB
/
shapes2.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
354
355
356
357
358
359
360
<!DOCTYPE html>
<html>
<head>
<title>Drawing Shapes</title>
<style type="text/css">
#cnvs
{
float:left;
}
#board
{
border:1px solid black;
}
#tempImg
{
position:absolute;
left:7.5px;
border:1px solid black;
}
#toolbar
{
float:left;
margin-left:10px;
}
.tools
{
height:38px;
width:38px;
outline:gray solid thin;
background-repeat:no-repeat;
background-position:center;
}
.selected
{
height:38px;
width:38px;
outline:red solid thin;
}
#pencil
{
background-image:url(pencil.png);
}
#circle
{
background-image:url(circle.png);
}
#rect
{
background-image:url(rectangle.png);
}
#cursor
{
background-image:url(cursor.png);
}
#eraser
{
background-image:url(eraser.png);
}
#line
{
background-image:url(line.png);
}
#brush
{
background-image:url(brush.png);
}
#clearall
{
background-image:url(clear.png);
}
</style>
<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" charset="UTF-8"></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);
//context.fillStyle=bgcol;
//context.fillRect(0,0,width,height);
});
$('#fgcolor').mColorPicker();
$('#fgcolor').bind('colorpicked', function(){
fgcol=($(this).val());
});
$('#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">
var canvas,canvaso;
var context,contexto;
var startX=0;
var startY=0;
var endX=0;
var endY=0;
var diffX=0;
var diffY=0;
var height=0;
var width=0;
var selectedTool="cursor";
var started;
var x,y;
var bgcol="white";
var linecol="black";
var fgcol;
var diffx,diffy;
var container;
// holds all drawn rectangles
var boxes = [];
//to initialise canvases and event listeners
function init()
{
canvaso = document.getElementById('board');
contexto = canvaso.getContext('2d');
canvaso.height = height;
canvaso.width = width;
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;
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);
}
}
if(started)
{
if(selectedTool=="rect")
{
context.clearRect(0, 0,width,height);
drawRectangle(startX,startY,x,y);
}
else if(selectedTool=="circle")
{
context.clearRect(0, 0,width,height);
drawCircle(startX,startY,x,y);
}
else if(selectedTool=="line")
{
context.clearRect(0, 0,width,height);
drawLine(startX,startY,x,y);
}
else if(selectedTool=="cursor")
{
}
}
}
}
/*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;
if(selectedTool=="rect")
{
drawRectangle(startX,startY,endX,endY);
}
if(selectedTool=="line")
{
drawLine(startX,startY,endX,endY);
}
if(selectedTool=="circle")
{
drawCircle(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.drawImage(canvas, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);
}
//to draw a line
function drawLine(cornerX,cornerY,corner2X,corner2Y)
{
context.beginPath();
context.moveTo(cornerX,cornerY);
context.lineTo(corner2X,corner2Y);
context.stroke();
context.closePath();
}
//to draw a rectangle
function drawRectangle(cornerX,cornerY,corner2X,corner2Y)
{
diffX=-corner2X+cornerX;
diffY=-corner2Y+cornerY;
context.strokeRect(cornerX,cornerY,-diffX,-diffY);
}
//to draw a circle
function drawCircle(cornerX,cornerY,corner2X,corner2Y)
{
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();
}
//to clear canvas
function clearCanvas()
{
contexto.clearRect(0,0,width,height);
}
</script>
</html>