-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
325 lines (274 loc) · 12.1 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
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>
<html>
<head>
<title>Drawpad - draw with your Leap</title>
<script src="blob.js"></script>
<script src="canvas-toBlob.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="filesaver.js"></script>
<script>
/* mousetrap v1.2.2 craig.is/killing/mice */
(function(){function q(a,c,b){a.addEventListener?a.addEventListener(c,b,!1):a.attachEvent("on"+c,b)}function x(a){return"keypress"==a.type?String.fromCharCode(a.which):h[a.which]?h[a.which]:y[a.which]?y[a.which]:String.fromCharCode(a.which).toLowerCase()}function r(a,c){var a=a||{},b=!1,d;for(d in l)a[d]&&l[d]>c?b=!0:l[d]=0;b||(n=!1)}function z(a,c,b,d,F){var g,e,f=[],j=b.type;if(!k[a])return[];"keyup"==j&&s(a)&&(c=[a]);for(g=0;g<k[a].length;++g)if(e=k[a][g],!(e.seq&&l[e.seq]!=e.level)&&j==e.action&&
("keypress"==j&&!b.metaKey&&!b.ctrlKey||c.sort().join(",")===e.modifiers.sort().join(",")))d&&e.combo==F&&k[a].splice(g,1),f.push(e);return f}function t(a,c,b){if(!u.stopCallback(c,c.target||c.srcElement,b)&&!1===a(c,b))c.preventDefault&&c.preventDefault(),c.stopPropagation&&c.stopPropagation(),c.returnValue=!1,c.cancelBubble=!0}function v(a){"number"!==typeof a.which&&(a.which=a.keyCode);var c=x(a);if(c)if("keyup"==a.type&&w==c)w=!1;else{var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");
a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");var b=z(c,b,a),d,f={},g=0,e=!1;for(d=0;d<b.length;++d)b[d].seq?(e=!0,g=Math.max(g,b[d].level),f[b[d].seq]=1,t(b[d].callback,a,b[d].combo)):!e&&!n&&t(b[d].callback,a,b[d].combo);a.type==n&&!s(c)&&r(f,g)}}function s(a){return"shift"==a||"ctrl"==a||"alt"==a||"meta"==a}function A(a,c,b){if(!b){if(!p){p={};for(var d in h)95<d&&112>d||h.hasOwnProperty(d)&&(p[h[d]]=d)}b=p[a]?"keydown":"keypress"}"keypress"==b&&c.length&&(b="keydown");return b}function B(a,
c,b,d,f){var a=a.replace(/\s+/g," "),g=a.split(" "),e,h,j=[];if(1<g.length){var i=a,m=b;l[i]=0;m||(m=A(g[0],[]));a=function(){n=m;++l[i];clearTimeout(C);C=setTimeout(r,1E3)};b=function(a){t(c,a,i);"keyup"!==m&&(w=x(a));setTimeout(r,10)};for(d=0;d<g.length;++d)B(g[d],d<g.length-1?a:b,m,i,d)}else{h="+"===a?["+"]:a.split("+");for(g=0;g<h.length;++g)e=h[g],D[e]&&(e=D[e]),b&&("keypress"!=b&&E[e])&&(e=E[e],j.push("shift")),s(e)&&j.push(e);b=A(e,j,b);k[e]||(k[e]=[]);z(e,j,{type:b},!d,a);k[e][d?"unshift":
"push"]({callback:c,modifiers:j,action:b,seq:d,level:f,combo:a})}}for(var h={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"ins",46:"del",91:"meta",93:"meta",224:"meta"},y={106:"*",107:"+",109:"-",110:".",111:"/",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'"},E={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7",
"*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},D={option:"alt",command:"meta","return":"enter",escape:"esc"},p,k={},i={},l={},C,w=!1,n=!1,f=1;20>f;++f)h[111+f]="f"+f;for(f=0;9>=f;++f)h[f+96]=f;q(document,"keypress",v);q(document,"keydown",v);q(document,"keyup",v);var u={bind:function(a,c,b){for(var d=a instanceof Array?a:[a],f=0;f<d.length;++f)B(d[f],c,b);i[a+":"+b]=c;return this},unbind:function(a,c){i[a+":"+c]&&(delete i[a+":"+c],this.bind(a,function(){},
c));return this},trigger:function(a,c){i[a+":"+c]();return this},reset:function(){k={};i={};return this},stopCallback:function(a,c){return-1<(" "+c.className+" ").indexOf(" mousetrap ")?!1:"INPUT"==c.tagName||"SELECT"==c.tagName||"TEXTAREA"==c.tagName||c.contentEditable&&"true"==c.contentEditable}};window.Mousetrap=u;"function"===typeof define&&define.amd&&define(u)})();
var ws, isdrawing, addX, tempstop, color, width, addY;
addY=0;
color = "black";
/**
* Draws a rounded rectangle using the current state of the canvas.
* If you omit the last three params, it will draw a rectangle
* outline with a 5 pixel border radius
* @param {CanvasRenderingContext2D} ctx
* @param {Number} x The top left x coordinate
* @param {Number} y The top left y coordinate
* @param {Number} width The width of the rectangle
* @param {Number} height The height of the rectangle
* @param {Number} radius The corner radius. Defaults to 5;
* @param {Boolean} fill Whether to fill the rectangle. Defaults to false.
* @param {Boolean} stroke Whether to stroke the rectangle. Defaults to true.
* @param {Number} strokeWidth The width of the stroke, if one exists
*/
function roundRect(ctx, x, y, width, height, radius, fill, stroke, strokeWidth) {
if (typeof stroke == "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
if (stroke) {
if (strokeWidth) canvas.lineWidth = strokeWidth;
ctx.stroke();
}
if (fill) {
ctx.fill();
}
}
Mousetrap.bind('p', function() {
if (tempstop) { tempstop=false; }
else { tempstop=true; }
});
Mousetrap.bind('n', function() {
document.getElementById('canvas').width=340;
canvas.fillStyle="white";
canvas.fillRect(0,0,340,340);
});
Mousetrap.bind('d', function() {
var vtcolor = color;
color="black"
for (var i=0; i<60; i++) { roundRect(canvas, -(0.5*i), -(0.5*i), 340+i, 340+i, 110, false, true, 1); }
color=vtcolor;
document.getElementById('canvas').toBlob(function(blob) {
saveAs(blob, "drawpad_sketch.png");
});
});
addX=170;
//Support Firefox
if (!typeof(WebSocket) && typeof(MozWebSocket)) {
WebSocket = MozWebSocket;
}
function fingerDown(x,y,z) {
addTap(x, y);
redraw();
}
function fingerMove(x,y,z) {
if(isdrawing){
addTap(x, y, true);
redraw();
}
}
var tapX = new Array();
var tapY = new Array();
var tapDrag = new Array();
function addTap(x, y, dragging)
{
tapX.push(x+addX);
tapY.push((y*-1)+450+addY);
tapDrag.push(dragging);
}
// Create the socket with event handlers
function init() {
isdrawing = false;
//Create and open the socket
ws = new WebSocket("ws://localhost:6437/");
//On connection
ws.onopen = function(event) {
//document.getElementById("connection").innerHTML = "Leap connection good!";
};
//On new message
ws.onmessage = function(event) {
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
isdrawing=false;
if (obj) {
if (obj.pointables) {
if (obj.pointables[0]) {
if (!tempstop && obj.pointables[0].tool) {
if (!isdrawing) {
fingerDown(obj.pointables[0].tipPosition[0], obj.pointables[0].tipPosition[1], obj.pointables[0].tipPosition[2]);
}
else {
fingerMove(obj.pointables[0].tipPosition[0], obj.pointables[0].tipPosition[1], obj.pointables[0].tipPosition[2]);
}
document.getElementById("reddot").style.backgroundColor = "green";
//document.getElementById('bigmenu').style.display = "none";
isdrawing=true;
//detect tool type
if (obj.pointables[0].tool) {
//document.getElementById("tool").innerHTML = "Tool: True"
}
else {
//document.getElementById("tool").innerHTML = "Tool: False"
}
}
else if (!obj.pointables[0].tool) {
//if this is not a tool, bring up the menu
isdrawing=false;
//document.getElementById('bigmenu').style.display = "block";
}
document.getElementById("reddot").style.marginTop = ((obj.pointables[0].tipPosition[1]*-1)+addY+100)+'px';
document.getElementById("reddot").style.marginLeft = (obj.pointables[0].tipPosition[0]+addX)+'px';
}
}
}
if (!isdrawing) {
tapX= [];
tapY=[];
document.getElementById("reddot").style.backgroundColor = "red";
}
};
//On socket close
ws.onclose = function(event) {
ws = null;
}
//On socket error
ws.onerror = function(event) {
alert("Received error");
};
}
function hideMenus() {
//document.getElementById('bigmenu').style.display = "none";
}
function changeColor() {
var tcolor;
if (color=="black") tcolor="red";
if (color=="red") tcolor="green";
if (color=="green") tcolor="blue";
if (color=="blue") tcolor="black";
if (tcolor) color=tcolor;
document.getElementById('colormenu').style.backgroundColor = color;
if (color=="blue" || color=="black") { document.getElementById('colormenu').style.color = "white"; }
else { document.getElementById('colormenu').style.color = "black"; }
}
function setStuffUp() {
canvas = document.getElementById('canvas').getContext("2d");
}
function redraw() {
canvas.width = canvas.width; // Clears the canvas
canvas.strokeStyle = color;
canvas.lineJoin = "round";
canvas.lineWidth = width;
for(var i=0; i < tapX.length; i++)
{
canvas.beginPath();
if(tapDrag[i] && i){
canvas.moveTo(tapX[i-1], tapY[i-1]);
}else{
canvas.moveTo(tapX[i]-1, tapY[i]);
}
canvas.lineTo(tapX[i], tapY[i]);
canvas.closePath();
canvas.stroke();
}
tapX = []; tapY=[]
}
</script>
</head>
<body onload="init();">
<h1>Drawpad</h1>
n=new drawing<br>
p=pause drawing<br>
d=download drawing<br><br>
Use a tool to draw (a chopstick works best) and use your finger to pause drawing. Drawing will resume when you use a tool again.
<br><br>Red dot=drawing paused; green dot=drawing unpaused.<br><br>
You can switch easily from finger to tool (from paused to unpaused) because Drawpad recognizes the farthest-forward object; if you have your finger and a tool paralell to each other, chopstick in front, you can move your finger forward to pause, and move your chopstick forward to begin drawing again.
<br><br><br><br><br>
<div id="center">
<div id="bigmenu" style="margin-bottom: -340px; margin-left: 0px;">
<div class="menusquare" id="colormenu" onclick="changeColor();" style="background-color: green; color: black;">Color</div>
<div class="menusquare" style="margin-top: -113px; margin-left:113px; color:black;width: 114px;"></div>
<div class="menusquare" style="margin-top: -113px; margin-left: 227px;"></div>
<div class="menusquare" style="height: 114px;"></div>
<div class="menusquare" onclick="hideMenus();" style="background-color: lightgreen; margin-top: -114px; margin-left: 113px; height: 114px; width: 114px; margin-top: -114px;">OK</div>
<div class="menusquare" style="margin-left: 227px; margin-top: -114px; height: 114px;"></div>
<div class="menusquare" style=""></div>
<div class="menusquare" style="margin-left: 113px; margin-top: -113px; width: 114px;"></div>
<div class="menusquare" style="margin-left: 227px; margin-top: -113px;"></div>
</div>
<canvas width="340" height="340" id="canvas">It appears your browser doesn't support Canvas! We kindly advise you to get back in your time machine and set it for 1994.</canvas>
<canvas width="340" height="340" id="canvasol"></canvas>
<div id="reddot"></div>
</div>
<script>
setStuffUp();
canvasol = document.getElementById('canvasol').getContext('2d');
for (var i=0; i<60; i++) { roundRect(canvasol, -(0.5*i), -(0.5*i), 340+i, 340+i, 108, false, true, 1); }
canvas.fillStyle="white";
canvas.fillRect(0,0,340,340);
</script>
<style>
#center {
margin-left: auto;
margin-right: auto;
display: block;
width: 340px;
height: 340px;
}
body {
background-color: black;
color: white;
font-family: sans-serif;
}
#bigmenu {
display:none;
}
#canvas {
display: block;
margin-bottom: -340px;
}
#reddot {
width: 10px;
height: 10px;
border-radius: 5px;
background-color: red;
margin-top: -175px;
margin-left: 165px;
position: relative;
}
.menusquare {
height: 113px;
width: 113px;
position: relative;
text-align: center;
vertical-align: middle;
line-height: 104px;
font-size: 32px;
font-family: sans-serif;
}
</style>
</body>
</html>