-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathindex_jQuery.html
263 lines (249 loc) · 5.69 KB
/
index_jQuery.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
<!doctype html>
<html>
<head>
<title>Sketch Pad</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<script src="lib/jquery/jquery-1.8.2.min.js"></script>
<style type="text/css">
body {
margin:0px;
width:100%;
height:100%;
overflow:hidden;
font-family:Arial;
/* prevent text selection on ui */
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
/* prevent scrolling in windows phone */
-ms-touch-action: none;
/* prevent selection highlight */
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.header, .footer{
position: absolute;
background-color: #222;
text-align: center;
}
.header {
top: 0px;
left: 0px;
right: 0px;
height: 32px;
padding:6px;
}
.footer {
bottom: 0px;
left: 0px;
right: 0px;
height: 42px;
padding:2px;
}
.title {
width: auto;
line-height: 32px;
font-size: 20px;
font-weight: bold;
color: #eee;
text-shadow: 0px -1px #000;
padding:0 60px;
}
.navbtn {
cursor: pointer;
float:left;
padding: 6px 10px;
font-weight: bold;
line-height: 18px;
font-size: 14px;
color: #eee;
text-shadow: 0px -1px #000;
border: solid 1px #111;
border-radius: 4px;
background-color: #404040;
box-shadow: 0 0 1px 1px #555,inset 0 1px 0 0 #666;
}
.navbtn-hover, .navbtn:active {
color: #222;
text-shadow: 0px 1px #aaa;
background-color: #aaa;
box-shadow: 0 0 1px 1px #444,inset 0 1px 0 0 #ccc;
}
#content{
position: absolute;
top: 44px;
left: 0px;
right: 0px;
bottom: 46px;
overflow:hidden;
background-color:#ddd;
}
#canvas{
cursor:crosshair ;
background-color:#fff;
}
.palette-case {
width:260px;
margin:auto;
text-align:center;
}
.palette-box {
float:left;
padding:2px 6px 2px 6px;
}
.palette {
border:2px solid #777;
height:36px;
width:36px;
}
.red{
background-color:#c22;
}
.blue{
background-color:#22c;
}
.green{
background-color:#2c2;
}
.white{
background-color:#fff;
}
.black{
background-color:#000;
border:2px dashed #fff;
}
</style>
<script type="text/javascript">
var ctx, color = "#000";
$(document).ready(function () {
// setup a new canvas for drawing wait for device init
setTimeout(function(){
newCanvas();
}, 1000);
// reset palette selection (css) and select the clicked color for canvas strokeStyle
$(".palette").click(function(){
$(".palette").css("border-color", "#777");
$(".palette").css("border-style", "solid");
$(this).css("border-color", "#fff");
$(this).css("border-style", "dashed");
color = $(this).css("background-color");
ctx.beginPath();
ctx.strokeStyle = color;
});
// link the new button with newCanvas() function
$("#new").click(function() {
newCanvas();
});
});
// function to setup a new canvas for drawing
function newCanvas(){
//define and resize canvas
$("#content").height($(window).height()-90);
var canvas = '<canvas id="canvas" width="'+$(window).width()+'" height="'+($(window).height()-90)+'"></canvas>';
$("#content").html(canvas);
// setup canvas
ctx=document.getElementById("canvas").getContext("2d");
ctx.strokeStyle = color;
ctx.lineWidth = 5;
// setup to trigger drawing on mouse or touch
$("#canvas").drawTouch();
$("#canvas").drawPointer();
$("#canvas").drawMouse();
}
// prototype to start drawing on touch using canvas moveTo and lineTo
$.fn.drawTouch = function() {
var start = function(e) {
e = e.originalEvent;
ctx.beginPath();
x = e.changedTouches[0].pageX;
y = e.changedTouches[0].pageY-44;
ctx.moveTo(x,y);
};
var move = function(e) {
e.preventDefault();
e = e.originalEvent;
x = e.changedTouches[0].pageX;
y = e.changedTouches[0].pageY-44;
ctx.lineTo(x,y);
ctx.stroke();
};
$(this).on("touchstart", start);
$(this).on("touchmove", move);
};
// prototype to start drawing on pointer(microsoft ie) using canvas moveTo and lineTo
$.fn.drawPointer = function() {
var start = function(e) {
e = e.originalEvent;
ctx.beginPath();
x = e.pageX;
y = e.pageY-44;
ctx.moveTo(x,y);
};
var move = function(e) {
e.preventDefault();
e = e.originalEvent;
x = e.pageX;
y = e.pageY-44;
ctx.lineTo(x,y);
ctx.stroke();
};
$(this).on("MSPointerDown", start);
$(this).on("MSPointerMove", move);
};
// prototype to start drawing on mouse using canvas moveTo and lineTo
$.fn.drawMouse = function() {
var clicked = 0;
var start = function(e) {
clicked = 1;
ctx.beginPath();
x = e.pageX;
y = e.pageY-44;
ctx.moveTo(x,y);
};
var move = function(e) {
if(clicked){
x = e.pageX;
y = e.pageY-44;
ctx.lineTo(x,y);
ctx.stroke();
}
};
var stop = function(e) {
clicked = 0;
};
$(this).on("mousedown", start);
$(this).on("mousemove", move);
$(window).on("mouseup", stop);
};
</script>
</head>
<body>
<div id="page">
<div class="header">
<a id="new" class="navbtn">New</a>
<div class="title">Sketch Pad</div>
</div>
<div id="content"><p style="text-align:center">Loading Canvas...</p></div>
<div class="footer">
<div class="palette-case">
<div class="palette-box">
<div class="palette white"></div>
</div>
<div class="palette-box">
<div class="palette red"></div>
</div>
<div class="palette-box">
<div class="palette blue"></div>
</div>
<div class="palette-box">
<div class="palette green"></div>
</div>
<div class="palette-box">
<div class="palette black"></div>
</div>
<div style="clear:both"></div>
</div>
</div>
</div>
</body>
</html>