-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (287 loc) · 10.2 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>May's Canvas</title>
<link rel="stylesheet" href="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
<link rel="stylesheet" href="assets/css/canvas.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script><script src= "assets/js/touch_punch.js"></script>
<script>
//set all variables
var canvasWidth = 600;
var canvasHeight = 400;
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var paint;
var colorPurple = '#660099';
var colorBlue = '#3333CC';
var colorGreen = '#336633';
var colorYellow = '#FFFF33';
var colorPink = '#CC0099';
var clickColor = new Array();
var curColor = null;
var clickSize = new Array();
var curSize = null;
var clickTool = new Array();
var curTool = null;
var clickAlpha = new Array();
var curAlpha = null;
$(document).ready(function() {
function initialize() {
curColor = colorPurple;
curSize = 10;
curTool = 'marker';
curAlpha = 1;
//remove and set borders on default options on page load or clear
$('.choice').removeClass('highlight');
$('#purple, #normal, #marker').addClass('highlight');
//load images in stamp picker
$('#img1').append("<img class='draggable new_stamp' src='assets/images/butterfly.png' alt='butterfly'>");
$('#img2').append("<img class='draggable new_stamp' src='assets/images/apple.png' alt='apple'>");
$('#img3').append("<img class='draggable new_stamp' src='assets/images/goldfish.png' alt='goldfish'>");
$('#img4').append("<img class='draggable new_stamp' src='assets/images/sun.png' alt='sun'>");
$('#img5').append("<img class='draggable new_stamp' src='assets/images/tree.png' alt='tree'>");
$('#img6').append("<img class='draggable new_stamp' src='assets/images/earth.png' alt='earth'>");
$('#img7').append("<img class='draggable new_stamp' src='assets/images/octopus.png' alt='octopus'>");
$('#img8').append("<img class='draggable new_stamp' src='assets/images/teddybear.png' alt='teddybear'>");
$('#img9').append("<img class='draggable new_stamp' src='assets/images/ninja.png' alt='ninja'>");
$('#img10').append("<img class='draggable new_stamp' src='assets/images/whale.png' alt='whale'>");
$(".draggable").draggable();
}
//set 2d context
context = document.getElementById('canvas').getContext('2d');
//set height and width of canvas
$('#canvas').attr({
width: canvasWidth,
height: canvasHeight
});
initialize();
//drawing - on mousedown
$(document).on('vmousedown', '#canvas', function(e) {
e.preventDefault();
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(mouseX, mouseY);
redraw();
});
//drawing - on mousemove
$(document).on('vmousemove', '#canvas', function(e) {
e.preventDefault();
if(paint) {
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
addClick(mouseX, mouseY, true);
redraw();
}
});
//drawing - on mouseup
$(document).on('vmouseup', '#canvas', function(e) {
e.preventDefault();
paint = false;
});
//drawing - on mouse leaving the canvas
$(document).on('mouseleave', '#canvas', function(e) {
paint = false;
});
$(document).on('vclick', '#canvas', function(e) {
e.preventDefault();
});
//drawing - on mouse re-entering the canvas - WORK ON THIS!!!?
// $(document).on('mouseenter', '#canvas', function(e) {
// paint = true;
// });
//add click function
function addClick(x, y, dragging)
{
if(curTool == "eraser") {
curColor = "white";
}
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
clickColor.push(curColor);
clickSize.push(curSize);
// clickTool.push(curTool);
clickAlpha.push(curAlpha);
}
//redraw function
function redraw() {
context.clearRect(0, 0, canvasWidth, canvasHeight);
context.lineJoin = "round";
for(var i=0; i<clickX.length; i++) {
context.beginPath();
if(clickDrag[i] && i) {
context.moveTo(clickX[i-1], clickY[i-1]);
} else {
context.moveTo(clickX[i]-1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.strokeStyle = clickColor[i];
context.lineWidth = clickSize[i];
context.globalAlpha = clickAlpha[i];
context.stroke();
}
}
//add highlight class for border
function highlight(choice) {
var choice_class = choice.attr('class').substr(7,10);
$('.' + choice_class).removeClass('highlight');
$(choice).addClass('highlight');
}
$(document).on('vclick', '.choice_col', function() {
if(curTool == 'eraser') {
curTool = 'marker';
$('#eraser').removeClass('highlight');
$('#marker').addClass('highlight');
}
});
//change current color when click on colors in color picker
$(document).on('vclick', '#purple', function() {
curColor = colorPurple;
highlight($(this));
});
$(document).on('vclick', '#blue', function() {
curColor = colorBlue;
highlight($(this));
});
$(document).on('vclick', '#green', function() {
curColor = colorGreen;
highlight($(this));
});
$(document).on('vclick', '#yellow', function() {
curColor = colorYellow;
highlight($(this));
});
$(document).on('vclick', '#pink', function() {
curColor = colorPink;
highlight($(this));
});
//change current font weight when click on options in font weight picker
$(document).on('vclick', '#small', function() {
curSize = 4;
highlight($(this));
});
$(document).on('vclick', '#normal', function() {
curSize = 10;
highlight($(this));
});
$(document).on('vclick', '#large', function() {
curSize = 17;
highlight($(this));
});
$(document).on('vclick', '#huge', function() {
curSize = 23;
highlight($(this));
});
//change current tool when click on options in tool picker
$(document).on('vclick', '#marker', function() {
curTool = 'marker';
curAlpha = 1;
if(curColor == 'white') {
curColor = colorPurple;
}
highlight($(this));
});
$(document).on('vclick', '#crayon', function() {
curTool = 'crayon';
curAlpha = 0.4;
if(curColor == 'white') {
curColor = colorPurple;
}
highlight($(this));
});
$(document).on('vclick', '#eraser', function() {
curTool = 'eraser';
curAlpha = 1;
highlight($(this));
$('.choice_col').removeClass('highlight');
});
// //change color of font weight options depending on current color - use this or not???
// $(document).on('vclick', '.choice_col', function() {
// if(curColor==colorPurple) {
// $('.line').css('background-color', colorPurple);
// } else if(curColor==colorBlue) {
// $('.line').css('background-color', colorBlue);
// } else if(curColor==colorGreen) {
// $('.line').css('background-color', colorGreen);
// } else if(curColor==colorYellow) {
// $('.line').css('background-color', colorYellow);
// } else if(curColor==colorPink) {
// $('.line').css('background-color', colorPink);
// }
// });
//draggable - make stamps draggable and when one is dragged replace with new one
$(document).on('vmousedown', '.new_stamp', function() {
$(this).removeClass('new_stamp');
var img_path = $(this).attr('src');
var stamp_id = $(this).parent().attr('id');
$('#' + stamp_id).append("<img class='draggable new_stamp' src='" + img_path + "'>");
$(".draggable").draggable();
});
//clear canvas when click clear button (confirm first)
$(document).on('click', '#clear', function() {
if(confirm('Are you sure you want to clear the canvas?')) {
clearCanvas();
}
});
//clear canvas function - clears canvas and resets all arrays
function clearCanvas()
{
context.clearRect(0, 0, canvasWidth, canvasHeight);
clickX.length = 0;
clickY.length = 0;
clickDrag.length = 0;
clickColor.length = 0;
clickSize.length = 0;
clickAlpha.length = 0;
$('.draggable').remove();
initialize();
}
});
</script>
</head>
<body>
<!-- canvas -->
<canvas id="canvas"></canvas>
<!-- color picker -->
<div class="picker">
<div class="choice choice_col" id="purple"></div>
<div class="choice choice_col" id="blue"></div>
<div class="choice choice_col" id="green"></div>
<div class="choice choice_col" id="yellow"></div>
<div class="choice choice_col" id="pink"></div>
<button id="clear">Clear</button>
</div>
<!-- font weight picker -->
<div class="picker">
<div class="choice choice_wgt" id="small"><div class="line" id="line_small"></div></div>
<div class="choice choice_wgt" id="normal"><div class="line" id="line_normal"></div></div>
<div class="choice choice_wgt" id="large"><div class="line" id="line_large"></div></div>
<div class="choice choice_wgt" id="huge"><div class="line" id="line_huge"></div></div>
</div>
<!-- tool picker -->
<div class="picker">
<div class="choice choice_tol" id="marker"><img src="assets/images/marker.png" alt="marker"></div>
<div class="choice choice_tol" id="crayon"><img src="assets/images/crayon.png" alt="crayon"></div>
<div class="choice choice_tol" id="eraser"><img src="assets/images/eraser.png" alt="eraser"></div>
</div>
<!-- stamps -->
<div id="drag_picker">
<div class="img" id="img1"></div>
<div class="img" id="img2"></div>
<div class="img" id="img3"></div>
<div class="img" id="img4"></div>
<div class="img" id="img5"></div>
<div class="img" id="img6"></div>
<div class="img" id="img7"></div>
<div class="img" id="img8"></div>
<div class="img" id="img9"></div>
<div class="img" id="img10"></div>
</div>
</body>
</html>