-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas3DrawTouch.html
191 lines (188 loc) · 8.49 KB
/
canvas3DrawTouch.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Canvas 3Draw Touch</title>
<style type="text/css">
body { background:#666; margin:0; overflow:hidden; }
canvas { background:#ddd; border:1px solid #000; display:block; margin:0 auto; width:100%; height:100%; }
</style>
</head>
<body>
<canvas class="pad"></canvas>
<script type="text/javascript">
function Thing(plot, x, y, z){
this.plot = plot;
this.obj = new Array(this.plot.length);
for( var i=0; i<this.obj.length; ++i ){ this.obj[i] = new Array(); }
this.xfrm = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [x,y,z,1] ];
}
Thing.prototype.rotate = function(radX, radY, radZ){
var tmpMatrix = [ [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0] ],
rotateMatrix = radX!=null? [ [1,0,0,0], [0,Math.cos(radX),Math.sin(radX),0], [0,-Math.sin(radX),Math.cos(radX),0], [0,0,0,1] ]
: radY!=null? [ [Math.cos(radY),0,-Math.sin(radY),0], [0,1,0,0], [Math.sin(radY),0,Math.cos(radY),0], [0,0,0,1] ]
: radZ!=null? [ [Math.cos(radZ),Math.sin(radZ),0,0], [-Math.sin(radZ),Math.cos(radZ),0,0], [0,0,1,0], [0,0,0,1] ]
: [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ],
i, j, k;
for( i=0; i<this.xfrm.length; i++ ){ for( j=0; j<this.xfrm.length; j++ ){ for( k=0; k<this.xfrm.length; k++ ){
tmpMatrix[i][j] += rotateMatrix[i][k] * this.xfrm[k][j];
} } }
this.xfrm = tmpMatrix;
};
Thing.prototype.transform = function(){
for( var r=0; r<this.obj.length; r++ ){ //construct current cubeMatrix from currentTransformation.
this.obj[r][0] = (this.xfrm[0][0] * this.plot[r][0]) + (this.xfrm[1][0] * this.plot[r][1]) + (this.xfrm[2][0] * this.plot[r][2]) + this.xfrm[3][0];
this.obj[r][1] = (this.xfrm[0][1] * this.plot[r][0]) + (this.xfrm[1][1] * this.plot[r][1]) + (this.xfrm[2][1] * this.plot[r][2]) + this.xfrm[3][1];
this.obj[r][2] = (this.xfrm[0][2] * this.plot[r][0]) + (this.xfrm[1][2] * this.plot[r][1]) + (this.xfrm[2][2] * this.plot[r][2]) + this.xfrm[3][2];
}
}
var _mobileDevice = ( window.innerWidth<=1280 && !!('ontouchstart' in window) && !!window.DeviceOrientationEvent );
function addHandler(el,evt,fn){ if( el.addEventListener ){ el.addEventListener(evt,fn,false); }else if( el.attachEvent ){ el.attachEvent('on'+evt,fn); } }
function rmHandler(el,evt,fn){ if( el.removeEventListener ){el.removeEventListener(evt,fn,false); }else if( el.detachEvent ){ el.detachEvent('on'+evt,fn); } }
function getComputed(el,style){ if(window.getComputedStyle){ var y=document.defaultView.getComputedStyle(el,null).getPropertyValue(style); }else if(el.currentStyle){ if( style=='width' ){ y=el.getBoundingClientRect().right-el.getBoundingClientRect().left }else if( style=='height' ){ y=el.getBoundingClientRect().bottom-el.getBoundingClientRect().top }else{ var y=el.currentStyle[style] } } return parseFloat(y); }
function getPgOffset(el){ var o=[0,0]; if( el.offsetParent ){ do{ o=[o[0]+el.offsetLeft,o[1]+el.offsetTop]; }while( el=el.offsetParent ) } return [o[0]+document.body.scrollLeft,o[1]+document.body.scrollTop]; }
function getXY(e){ var xy=[]; if( e.touches && e.touches.length ){ if( e.touches.length>1 ){ xy[0] = (e.touches[0].pageX+e.touches[0].pageX)/2; xy[1] = (e.touches[0].pageY+e.touches[0].pageY)/2;}else{ xy[0] = e.touches[0].pageX; xy[1] = e.touches[0].pageY; } }else{ xy[0] = e.pageX; xy[1] = e.pageY; } return xy; }
var cnvPad;
cnvPad = {
cnv:null, t1:null, t2:null, mv:null, sp:0.01, o1:Date.now(), o2:null,
init: function(cnv){
if(typeof cnv==='string'){ cnv = document.getElementById(cnv); }
cnv.onmousedown = cnvPad.touch;
cnv.ontouchstart = cnvPad.touch;
cnv.xy = getPgOffset(cnv);
cnv.wh = [window.innerWidth, window.innerHeight];
cnv.width = cnv.wh[0]; cnv.height = cnv.wh[1];
cnv.xy0 = [cnv.wh[0]/2, cnv.wh[1]/2];
cnv.plots = [];
cnv.things = [];
cnv.draw = function(){
var ctx = cnv.getContext('2d'), i, thing, p, x, y, pspctvX=500, pspctvY=500;
cnv.width = cnv.wh[0]; cnv.height = cnv.wh[1];
ctx.strokeStyle = "#000000";
ctx.beginPath();
for( i in cnv.things ){
thing = cnv.things[i];
thing.transform();
for( p=0; p<thing.obj.length; p++ ){ //plot, projecting 3d to 2d
x = Math.round(cnv.xy0[0] + (thing.obj[p][0] * pspctvX / thing.obj[p][2]));
y = Math.round(cnv.xy0[1] - (thing.obj[p][1] * pspctvY / thing.obj[p][2]));
if( p==0 ){ ctx.moveTo(x,y); }else{ ctx.lineTo(x,y); }
}
}
ctx.stroke();
};
if( _mobileDevice ){
addHandler(window,'deviceorientation',function(e){
e = e || window.event;
cnvPad.o2 = e.timeStamp;
if( !cnvPad.mv && cnvPad.o2-cnvPad.o1>50 ){
cnvPad.o1 = cnvPad.o2;
for( i in cnv.things ){
cnv.things[i].rotate(e.gamma/270,null,null);
cnv.things[i].rotate(null,e.beta/270,null);
}
cnv.draw();
}
});
}
},
touch: function(e){
e = e || window.event;
cnvPad.cnv = this;
cnvPad.t1 = cnvPad.t2;
cnvPad.t2 = e.timeStamp;
clearTimeout(cnvPad.cnv.t);
cnvPad.cnv.plots.push([]);
cnvPad.cnv.things.push(null);
if( e.type==='touchstart' ){
cnvPad.cnv.ontouchmove = cnvPad.move;
cnvPad.cnv.onmousedown = null;
cnvPad.cnv.ontouchend = function(){
cnvPad.cnv.ontouchmove = null;
cnvPad.cnv.ontouchend = null;
cnvPad.release();
};
window.ondeviceorientation = null;
}else{
document.onmousemove = cnvPad.move;
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
cnvPad.release();
};
}
return false;
},
move: function(e){
e = e || window.event;
var i = 0,
p = cnvPad.cnv.plots.length-1,
plot = cnvPad.cnv.plots[p],
xyE = getXY(e);
cnvPad.mv = 1;
plot.push([xyE[0]-cnvPad.cnv.xy[0]-cnvPad.cnv.xy0[0], -xyE[1]-cnvPad.cnv.xy[1]+cnvPad.cnv.xy0[1], 0]);
cnvPad.cnv.things[p] = new Thing(cnvPad.cnv.plots[p], 0, 0, 500);
cnvPad.cnv.draw();
return false;
},
release: function(){
if( !cnvPad.mv ){/* must be a tap */
cnvPad.cnv.plots.pop();
cnvPad.cnv.things.pop();
if( cnvPad.t2-cnvPad.t1<300 ){/* double tap */
cnvPad.cnv.plots = [];
cnvPad.cnv.things = [];
}else{
cnvPad.sp = (cnvPad.sp+0.01)%0.1;
}
}else{
var f = 2,
n = cnvPad.cnv.plots.length-1,
p = cnvPad.cnv.plots[n], t = cnvPad.cnv.things[n],
l = p.length,
z1, z2, h = 400, r,
i = 1;
while( p.length > (l/f)+1 ){
p.splice(i, f-1);
t.obj.splice(i, f-1);
++i;
}
l = p.length, i = 2;
while( p.length < (5*l/3)-2 ){
z1 = [p[i-1][0],p[i-1][1],h];
z2 = [p[i][0],p[i][1],h];
p.splice(i, 0, z1, z2);
z1 = [t.obj[i-1][0],t.obj[i-1][1],h];
z2 = [t.obj[i][0],t.obj[i][1],h];
t.obj.splice(i, 0, z1, z2);
i+=5;
}
}
cnvPad.mv = 0;
if( !_mobileDevice ){
cnvPad.cnv.t = setInterval(function(){
for( i in cnvPad.cnv.things ){
r = i%2>0? [cnvPad.sp,null,null] : [null,cnvPad.sp,null];
cnvPad.cnv.things[i].rotate.apply(cnvPad.cnv.things[i], r);
}
cnvPad.cnv.draw();
}, 42);
}
}
};
var i, els = document.querySelectorAll('.pad');
for( i=0; i<els.length; ++i ){
var el = els[i];
cnvPad.init(el);
addHandler(window,'resize',function(){
el.wh = [window.innerWidth, window.innerHeight];
el.width = el.wh[0]; el.height = el.wh[1];
el.xy0 = [el.wh[0]/2, el.wh[1]/2];
});
}
</script>
</body>
</html>