-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathindex_jQueryMobile.html
210 lines (197 loc) · 4.96 KB
/
index_jQueryMobile.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
<!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" />
<link rel="stylesheet" href="lib/jquery.mobile/jquery.mobile-1.2.0.min.css" />
<script src="lib/jquery/jquery-1.8.2.min.js"></script>
<script src="lib/jquery.mobile/jquery.mobile-1.2.0.min.js"></script>
<style type="text/css">
body {
margin:0px;
width:100%;
height:100%;
overflow:hidden;
/* 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;
}
#content {
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);
// prevent footer to toggle on touch
$("[data-role=footer]").fixedtoolbar({ tapToggle: false });
// 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 data-role="page" id="page1">
<div data-theme="a" data-role="header">
<h3>Sketch Pad</h3>
<a id="new" data-role="button" data-theme="b" class="ui-btn-left">New</a>
</div>
<div id="content"><p style="text-align:center">Loading Canvas...</p></div>
<div data-theme="a" data-role="footer" data-position="fixed">
<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>