function8.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. function FingerComponent(obj_list,sync_obj,canvas){
  2. this.TRANSFORM_PREFIXED = this.getVendorPrefixed(["transform", "msTransform", "MozTransform", "webkitTransform", "OTransform"]);
  3. this.canvas=canvas;
  4. this.boxList = [];
  5. this.car = this.assignBox(sync_obj);
  6. this.car["beginRun"]=false;
  7. this.car["newStage"]=0;
  8. this.car["yforLine"]=[0];
  9. this.car["dragDeg"]=[];
  10. this.boxList.push(this.car);
  11. for(var i = 0,objId;objId=obj_list[i];i++){
  12. var temp = this.assignBox(objId);
  13. this.boxList.push(temp);
  14. this.listen(temp);
  15. }
  16. this.arrow=this.boxList[1];
  17. var _this=this;
  18. var loop=function(){
  19. _this.moveCar();
  20. for(var i=0; i<_this.boxList.length; i++) {
  21. _this.transform(_this.boxList[i]);
  22. }
  23. requestAnimationFrame(loop);
  24. };
  25. loop();
  26. this.loop=loop;
  27. }
  28. FingerComponent.prototype.moveCar=function() {
  29. if(this.car.beginRun){
  30. var xMargin=[0,610,800,0,0];
  31. var maxStage= 9; //沙面
  32. var step=0.5; //行進速度
  33. var ctx = this.canvas.getContext("2d");
  34. //ctx.moveTo(295,190);
  35. ctx.lineTo(this.car.x+30,this.car.y-10);
  36. //ctx.lineTo(this.car.x+30,this.car.y+232);
  37. ctx.lineWidth = 5;
  38. ctx.lineLength = 6;
  39. ctx.strokeStyle = "red";
  40. ctx.stroke();
  41. this.car.isUpdated = true;
  42. if(this.car.x==510){
  43. //console.log("!!!");
  44. var tol=5e-1;
  45. if(this.car.rad < this.arrow.rad && this.arrow.rad-this.car.rad>=tol){
  46. this.car.rad += 0.2;
  47. return;
  48. }
  49. else if(this.car.rad > this.arrow.rad && this.car.rad-this.arrow.rad>=tol){
  50. this.car.rad -= 0.2;
  51. return;
  52. }
  53. /*else{
  54. }*/
  55. }
  56. if(this.car.x>510)
  57. step=1;
  58. var deltay=step*Math.tan(this.car.rad*Math.PI/180); //行進角度
  59. this.car.x+=step;
  60. this.car.y+=deltay;
  61. //this.car.style.transformOrigin="20% 40%";
  62. if(this.car.x >= 610){
  63. this.car.beginRun=false;
  64. //this.car.yforLine.push(this.car.y);
  65. //var $normal_line=$("#normal-line").clone().attr("id","");//.css("left","0px");
  66. var left=(this.car.x-70);
  67. var carWidth=10;
  68. var carHeight=50;
  69. var top=this.car.y+((carHeight/3)/Math.tan(this.car.rad*Math.PI/850)+carWidth)*Math.sin(this.car.rad*Math.PI/180);
  70. var style="position:absolute;left:"+left+"px;top:"+top+"px;";
  71. //$("div").append($check.attr("style",style));
  72. this.arrow.x=this.car.x;
  73. this.arrow.y=this.car.y;
  74. this.arrow.isUpdated=true;
  75. //confirm(this.car.x);
  76. //confirm(this.car.y);
  77. saveRecordr(0,this.car.x,0,0,0);
  78. saveRecordr(0,0,this.car.y,0,0);
  79. if(this.car.x == 610.5 && this.car.y>=360)
  80. {
  81. //this.arrow.element.className="hidden";
  82. this.arrow.element.className="arrow-begin hidden";
  83. check.style.display = "inline";
  84. check.style.top = 650;
  85. check.style.left = 500;
  86. saveRecord(250);
  87. saveRecord(100);
  88. setTimeout(
  89. function(){
  90. correct.style.display = "inline";
  91. correct.style.top = 650;
  92. correct.style.left = 500;
  93. next.style.display = "inline";
  94. next.style.top =600;
  95. next.style.left = 750;
  96. back.style.display = "inline";
  97. back.style.top =600;
  98. back.style.left = 630;
  99. },3000)
  100. }
  101. if(this.car.x == 610.5 && this.car.y <= 361)
  102. {
  103. //this.arrow.element.className="hidden";
  104. this.arrow.element.className="arrow-begin hidden";
  105. check.style.display = "inline";
  106. check.style.top = 650;
  107. check.style.left = 500;
  108. saveRecord(250);
  109. saveRecord(200);
  110. setTimeout(
  111. function(){
  112. wrong.style.display = "inline";
  113. wrong.style.top = 650;
  114. wrong.style.left = 500;
  115. next.style.display = "inline";
  116. next.style.top =600;
  117. next.style.left = 750;
  118. back.style.display = "inline";
  119. back.style.top =600;
  120. back.style.left = 630;
  121. },3000)
  122. }
  123. // if(this.car.x == 330 && this.car.y <= 200 || this.car.y>=331)
  124. // {
  125. //this.arrow.element.className="hidden";
  126. //$("normal_line").className="hidden1";
  127. // this.arrow.element.className="arrow-begin hidden";
  128. // check.className= "";
  129. // setTimeout(
  130. // function(){
  131. // $("#wrong").removeClass("hidden");
  132. // check.className = "hidden";
  133. // next.className = "";
  134. // back.className = "";
  135. //},3000)
  136. //}
  137. //if( this.car.x == 640 && this.car.y >= 470 && this.car.y<=520)
  138. // {
  139. //this.arrow.element.className="hidden1";
  140. //$("#correct").removeClass("hidden");
  141. // wrong.className="hidden";
  142. //this.arrow.element.className="arrow-begin hidden";
  143. // }
  144. }
  145. else
  146. {
  147. this.arrow.element.className="arrow-begin hidden";
  148. }
  149. }
  150. }
  151. function nextPage(code,num){
  152. saveRecord(code);
  153. setTimeout(1500,location.replace(num+'.php'));
  154. }
  155. function dragPic(){
  156. dragTime = Math.floor(Date.now() / 1000);
  157. }
  158. FingerComponent.prototype.checkRes=function() {
  159. //window.location.reload();
  160. document.getElementById("Answer1").style.display="inline";
  161. }
  162. FingerComponent.prototype.assignBox=function(picS) {
  163. var pic = document.getElementById(picS);
  164. //console.log(pic);
  165. return {
  166. element: pic,
  167. isUpdated: true,
  168. x: 360,//Math.random() * (window.innerWidth - 200),
  169. y: 200,//Math.random() * (window.innerHeight - 300),
  170. rad: 45,//Math.random()*Math.PI * 2,
  171. scale: 1,
  172. rafId: 0,
  173. zIndex: 0,
  174. degx: 0,
  175. degy: 0,
  176. }
  177. }
  178. FingerComponent.prototype.listen=function(pBoxO) {
  179. var finger = new Fingers(pBoxO.element);
  180. var flag=false;
  181. var _this=this;
  182. finger.addGesture(Fingers.gesture.Drag, null).addHandler(function(pEventType, pData, pFingers) {
  183. if(pEventType === Fingers.Gesture.EVENT_TYPE.move) {
  184. //pBoxO.x += pFingers[0].getDeltaX();
  185. //pBoxO.y += pFingers[0].getDeltaY();
  186. var x=pFingers[0].getDeltaX();
  187. var y=pFingers[0].getDeltaY();
  188. //console.log("X:"+x+" Y:"+y);
  189. var temp=pBoxO.rad+y/3;
  190. if(temp>=15 && temp<=105)
  191. pBoxO.rad = temp;//(y/100);
  192. //_this.car.rad=pBoxO.rad;
  193. _this.arrow.rad = pBoxO.rad;
  194. _this.arrow.isUpdated=true
  195. //_this.car.isUpdated=true
  196. //pBoxO.isUpdated = true;
  197. }
  198. else if(pEventType === Fingers.Gesture.EVENT_TYPE.end) {
  199. //pBoxO.element.className+=" hidden";
  200. _this.car.dragDeg.push(_this.car.rad);
  201. _this.car.beginRun=true;
  202. _this.car.newStage++;
  203. //_this.car.lastRad=_this.car.rad;
  204. }
  205. });
  206. pBoxO.fingers = finger ;
  207. }
  208. FingerComponent.prototype.transform=function(pBoxO) {
  209. if(pBoxO.isUpdated) {
  210. pBoxO.element.style[this.TRANSFORM_PREFIXED] = "translateZ(0) " +
  211. "translate(" + pBoxO.x + "px, " + pBoxO.y + "px) " +
  212. "scale(" + pBoxO.scale + ") " +//+ "rotate3d(0,0,0," + pBoxO.rad + "rad) ";
  213. "rotate(" + pBoxO.rad + "deg) ";
  214. pBoxO.element.style.zIndex = pBoxO.zIndex;
  215. pBoxO.isUpdated = false;
  216. }
  217. }
  218. FingerComponent.prototype.getVendorPrefixed=function(pArrayOfPrefixes) {
  219. var result = null;
  220. for (var i=0; i<pArrayOfPrefixes.length; i++) {
  221. if (document.body.style[pArrayOfPrefixes[i]] !== undefined) {
  222. result = pArrayOfPrefixes[i];
  223. break;
  224. }
  225. }
  226. return result;
  227. }
  228. function saveRecord(code){
  229. xmlhttp = new XMLHttpRequest();
  230. xmlhttp.onreadystatechange = function() {
  231. if (xmlhttp.readyState==4) {
  232. if(xmlhttp.status==200) {
  233. xmlhttp.abort();
  234. }
  235. }
  236. };
  237. xmlhttp.open("POST","saveRecord.php",true);
  238. xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  239. xmlhttp.send("codenum=" + code + "&time=" + Math.floor(Date.now() / 1000));
  240. }
  241. function saveRecords(code,time){
  242. xmlhttp = new XMLHttpRequest();
  243. xmlhttp.onreadystatechange = function() {
  244. if (xmlhttp.readyState==4) {
  245. if(xmlhttp.status==200) {
  246. xmlhttp.abort();
  247. }
  248. }
  249. };
  250. xmlhttp.open("POST","saveRecord.php",true);
  251. xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  252. xmlhttp.send("codenum=" + code + "&dragtime=" + time);
  253. }
  254. function saveRecordr(code,rotX,rotY,rotZ,rotS){
  255. xmlhttp = new XMLHttpRequest();
  256. xmlhttp.onreadystatechange = function() {
  257. if (xmlhttp.readyState==4) {
  258. if(xmlhttp.status==200) {
  259. xmlhttp.abort();
  260. }
  261. }
  262. };
  263. xmlhttp.open("POST","saveRecord.php",true);
  264. xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  265. xmlhttp.send("codenum=" + code + "&time=" + Math.floor(Date.now() / 1000) + "&rotx="+rotX+"&roty="+rotY+"&rotz="+rotZ+"&scale="+rotS);
  266. }