index.html 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. <!doctype html>
  2. <html class="no-js" lang="">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="x-ua-compatible" content="ie=edge">
  6. <title>心灵照相机</title>
  7. <meta name="description"
  8. content="A website that learns to predict where you are looking at. Written in TensorFlow.js">
  9. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  10. <link rel="stylesheet" href="normalize.css">
  11. <link rel="stylesheet" href="style.css">
  12. <style>
  13. .buttonArr button {
  14. position: fixed;
  15. width: 10px;
  16. height: 1px;
  17. /* opacity: 0.0; */
  18. z-index: 2000;
  19. color: #000000;
  20. border-radius: 10px;
  21. border: 0px;
  22. }
  23. .buttonArr {
  24. position: unset !important;
  25. width: 1px;
  26. height: 0px;
  27. opacity: 0.0;
  28. z-index: 2000;
  29. border-radius: 10px;
  30. border: 0px;
  31. }
  32. #num1 {
  33. position: absolute;
  34. left: 20px;
  35. top: 20px;
  36. }
  37. #num2 {
  38. position: absolute;
  39. left: 33%;
  40. top: 20px;
  41. }
  42. #num3 {
  43. position: absolute;
  44. left: 66%;
  45. top: 20px;
  46. }
  47. #num4 {
  48. position: absolute;
  49. right: 80px;
  50. top: 20px;
  51. }
  52. #num5 {
  53. position: absolute;
  54. left: 20px;
  55. top: 33%;
  56. }
  57. #num6 {
  58. position: absolute;
  59. left: 33%;
  60. top: 33%;
  61. }
  62. #num7 {
  63. position: absolute;
  64. left: 66%;
  65. top: 33%;
  66. }
  67. #num8 {
  68. position: absolute;
  69. right: 80px;
  70. top: 33%;
  71. }
  72. #num9 {
  73. position: absolute;
  74. left: 20px;
  75. top: 66%;
  76. }
  77. #num10 {
  78. position: absolute;
  79. left: 33%;
  80. top: 66%;
  81. }
  82. #num11 {
  83. position: absolute;
  84. left: 66%;
  85. top: 66%;
  86. }
  87. #num12 {
  88. position: absolute;
  89. right: 80px;
  90. top: 66%;
  91. }
  92. #num13 {
  93. position: absolute;
  94. left: 20px;
  95. bottom: 0px;
  96. }
  97. #num14 {
  98. position: absolute;
  99. left: 33%;
  100. bottom: 0px;
  101. }
  102. #num15 {
  103. position: absolute;
  104. left: 66%;
  105. bottom: 0px;
  106. }
  107. #num16 {
  108. position: absolute;
  109. right: 80px;
  110. bottom: 0px;
  111. }
  112. #targetMini {
  113. background-color: #00FF00;
  114. position: absolute;
  115. border-radius: 50%;
  116. height: 55px;
  117. width: 55px;
  118. /* z-index: 0; */
  119. opacity: 0.5;
  120. left: 0;
  121. bottom: 0;
  122. top: 0;
  123. right: 0;
  124. margin: auto;
  125. z-index: 999;
  126. transition: 1s;
  127. transition-timing-function: steps(1000, start);
  128. border: 2.5px solid #000000;
  129. }
  130. #target {
  131. /* text-align: center; */
  132. justify-content: center;
  133. background-image: url('calibrate.png');
  134. background-size: 120px 120px;
  135. /* background-color: lightgreen; */
  136. position: fixed;
  137. /* position: absolute; */
  138. border-radius: 50%;
  139. height: 120px;
  140. width: 120px;
  141. left: 0px;
  142. top: 0px;
  143. transition: left 0.01s, top 0.01s;
  144. box-shadow: 0 0 10px 5px white;
  145. opacity: 0.5;
  146. display: inline-block;
  147. z-index: 999;
  148. }
  149. #targetMini1 {
  150. background-color: #00FF00;
  151. /* position: absolute; */
  152. position: absolute;
  153. border-radius: 50%;
  154. height: 54px;
  155. width: 54px;
  156. /* z-index: 0; */
  157. opacity: 0.6;
  158. left: 0;
  159. bottom: 0;
  160. top: 0;
  161. right: 0;
  162. margin: auto;
  163. z-index: 999;
  164. transition: 1s;
  165. transition-timing-function: steps(1000, start);
  166. border: 3px solid #000000;
  167. }
  168. #target1 {
  169. /* text-align: center; */
  170. justify-content: center;
  171. background-image: url('calibrate.png');
  172. background-size: 60px 60px;
  173. /* background-color: lightgreen; */
  174. position: relative;
  175. /* position: absolute; */
  176. border-radius: 50%;
  177. height: 60px;
  178. width: 60px;
  179. left: 0px;
  180. top: 0px;
  181. transition: left 1s, top 1s;
  182. box-shadow: 0 0 10px 5px white;
  183. opacity: 0.9;
  184. display: inline-block;
  185. z-index: 999;
  186. }
  187. #refresh {
  188. width: 60px;
  189. font-size: 10px;
  190. position: fixed;
  191. top: 0px;
  192. left: 90%;
  193. display: inline-block;
  194. z-index: 999;
  195. margin-left: -40px;
  196. top: 2%;
  197. background-color: rgb(127, 178, 255);
  198. line-height: 30px;
  199. color: aliceblue;
  200. /* border: 1px solid; */
  201. text-align: center;
  202. border-radius: 5px;
  203. cursor: pointer;
  204. }
  205. #exit {
  206. width: 60px;
  207. font-size: 10px;
  208. position: fixed;
  209. top: 0px;
  210. left: 98%;
  211. display: inline-block;
  212. z-index: 999;
  213. margin-left: -40px;
  214. top: 2%;
  215. background-color: rgb(127, 178, 255);
  216. line-height: 30px;
  217. color: aliceblue;
  218. /* border: 1px solid; */
  219. text-align: center;
  220. border-radius: 5px;
  221. cursor: pointer;
  222. /* padding: 5px; */
  223. }
  224. #startCalibration {
  225. position: fixed;
  226. top: 0px;
  227. left: 50%;
  228. display: inline-block;
  229. margin-left: -40px;
  230. top: 30%;
  231. color: aliceblue;
  232. background-color: rgb(127, 178, 255);
  233. width: 80px;
  234. line-height: 40px;
  235. /* border: 1px solid; */
  236. text-align: center;
  237. border-radius: 5px;
  238. cursor: pointer;
  239. padding: 5px;
  240. }
  241. #came {
  242. position: fixed;
  243. top: 5%;
  244. left: 50%;
  245. margin-left: -100px;
  246. color: #ffffff;
  247. /* font-size: 16px;
  248. right:0px;
  249. bottom:0; */
  250. background-color: #666666;
  251. padding-top: 5px;
  252. padding-left: 5px;
  253. padding-right: 5px;
  254. padding-bottom: 5px;
  255. border-radius: 5px;
  256. ;
  257. z-index: 9999
  258. }
  259. </style>
  260. </head>
  261. <body>
  262. <div id="content">
  263. <div class="buttonArr">
  264. <button id='num1'>&nbsp;</button><button id="num2">&nbsp;</button><button id="num3">&nbsp;</button><button
  265. id='num4'>&nbsp;</button><br>
  266. <button id='num5'>&nbsp;</button><button id='num6'>&nbsp;</button><button id='num7'>&nbsp;</button><button
  267. id='num8'>&nbsp;</button><br>
  268. <button id='num9'>&nbsp;</button> <button id='num10'>&nbsp;</button> <button id='num11'>&nbsp;</button> <button
  269. id='num12'>&nbsp;</button><br>
  270. <button id='num13'>&nbsp;</button> <button id='num14'>&nbsp;</button> <button id='num15'>&nbsp;</button> <button
  271. id='num16'>&nbsp;</button>
  272. </div>
  273. <div id="target">
  274. <div id="targetMini"></div>
  275. </div>
  276. <div id="target1">
  277. <div id="targetMini1"></div>
  278. </div>
  279. <canvas id="solar" width="900" height="auto" style="background:#000000;position: fixed; z-index:998"></canvas>
  280. <div id="came">摄像头初始化中稍后再试...</div>
  281. <!-- <div id="finishcalibration" margin-left:300px;position: fixed;top:60px
  282. style="position: fixed;top:20px;left:20px;display: inline-block;width: 80px;height: 17px;border:1px solid;text-align: center;border-radius: 5px;cursor: pointer;padding: 5px;">
  283. 关闭校正</div> onclick="startCal()" -->
  284. <!-- <div id="startCalibration">
  285. 开始校准</div> -->
  286. <div id="exit">
  287. 切换游戏</div>
  288. <div id="refresh">
  289. 重新校验</div>
  290. <canvas id="tracker-canvas"
  291. style="position: fixed;top:0px;left:50%;width:250px;height:200px;margin-left:-125px;"></canvas>
  292. </div>
  293. <!-- <script src="js/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  294. crossorigin="anonymous"></script> -->
  295. <script src="js/jquery-3.3.1.min.js"></script>
  296. <script src="js/gazefilter.js"></script>
  297. <script src="js/aes.js" type="text/javascript" charset="utf-8"></script>
  298. <!--script src="js/calibration.js"></script-->
  299. <script>
  300. $('#came').hide()
  301. var soundSrc = 'sound/fruitMus1.mp3'
  302. var mp3Url = soundSrc;
  303. var soundFlag = true;
  304. var cameraFlag = true
  305. //点击按钮声音
  306. var soundButtonSrc = 'sound/button.mp3'
  307. var mp3Button = soundButtonSrc
  308. var playerButton = new Audio(mp3Button);
  309. //切水果音效
  310. var soundHitSrc = 'sound/fruitHit.mp3'
  311. var mp3Hit = soundHitSrc
  312. var playerHit = new Audio(mp3Hit)
  313. //背景音乐
  314. var soundBgSrc = 'sound/fruitMusicBg.mp3'
  315. var mp3Bg = soundBgSrc
  316. var playerBg = new Audio(mp3Bg)
  317. //Total score
  318. var totalScore = 0;
  319. var player = new Audio(mp3Url);
  320. var key = CryptoJS.enc.Utf8.parse("Sp5biS+gX+#CqAFF");//密钥
  321. var iv = CryptoJS.enc.Utf8.parse('ud2E8l6wchltwIDA');
  322. //传输参数为字符串
  323. function Encrypt(word) {
  324. let srcs = CryptoJS.enc.Utf8.parse(word);
  325. let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  326. return encrypted.ciphertext.toString().toUpperCase();
  327. }
  328. function Decrypt(word) {
  329. let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  330. let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  331. let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  332. let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  333. return decryptedStr.toString();
  334. }
  335. // levelFun(2, 20)
  336. //
  337. //传输关卡和得分的字符串
  338. function levelFun(level, score) {
  339. //1----获取userId
  340. let userMi = sessionStorage.getItem('B922C64EC97B4FF08485886B2AAEF40C')
  341. //解密userMi
  342. let user = Decrypt(userMi)
  343. //需要传输的参数
  344. let userId = JSON.parse(user).id
  345. //2----获取唯一标识
  346. let associatedFlag = new Date().getTime() + '';
  347. var params = {
  348. userId: userId,
  349. associatedFlag: associatedFlag,
  350. gameFlag: 'g_ydrzxlqsg',
  351. checkPoint: level.toString(),
  352. level: level.toString(),
  353. score: score.toString(),
  354. performance: {
  355. correct: 0.9, //当前难度等级正确率
  356. reactionTime: 400, //当前难度等级平均反应时间
  357. clickTime: 1231
  358. },
  359. data: {
  360. index: 1, //序号
  361. needResponse: 0, //是否需要反应 0无需反应,1需反应
  362. responseTrue: true, //反应是否正确 true false
  363. responseTime: 766
  364. }
  365. }
  366. // Encrypt(JSON.stringify(params))
  367. // console.log(Encrypt(JSON.stringify(params)))
  368. //给参数加密
  369. let paramsMi = Encrypt(JSON.stringify(params))
  370. //config.headers.Authorization = oSessionStorage.getItem("token") ? `Bearer ${oSessionStorage.getItem('token')}` : '';
  371. let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
  372. $.ajax({
  373. url: "https://gengnao.cn:8070/gameLevelRecord/saveResult", //请求接口的地址
  374. type: "POST",
  375. contentType: "json/application",
  376. dataType: 'json', //请求的方法GET/POST
  377. data: JSON.stringify({ data: paramsMi }),
  378. headers: {
  379. 'Authorization': "Bearer " + tokenLin,
  380. }, //需要传递的参数
  381. success: function (res) {
  382. res = Decrypt(res)
  383. //调用解密方法 //请求成功后的操作 //在控制台输出返回结果
  384. },
  385. error: function (err) { //请求失败后的操作 //请求失败在控制台输出22
  386. }
  387. })
  388. }
  389. function saveResult(score) {
  390. //1----获取userId
  391. let userMi = sessionStorage.getItem('B922C64EC97B4FF08485886B2AAEF40C')
  392. //解密userMi
  393. let user = Decrypt(userMi)
  394. //需要传输的参数
  395. let userId = JSON.parse(user).id
  396. //2----获取唯一标识
  397. let associatedFlag = new Date().getTime() + '';
  398. var params = {
  399. userId: userId,
  400. associatedFlag: associatedFlag,
  401. gameFlag: 'g_ydrzxlqsg',
  402. totalScore: score.toString(),
  403. gameName: '眼动认知训练切水果'
  404. }
  405. let paramsMi = Encrypt(JSON.stringify(params))
  406. let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
  407. $.ajax({
  408. url: "https://gengnao.cn:8070/gameRecord/saveResult", //请求接口的地址
  409. type: "POST",
  410. contentType: "json/application",
  411. dataType: 'json',
  412. headers: {
  413. 'Authorization': "Bearer " + tokenLin,
  414. }, //请求的方法GET/POST
  415. data: JSON.stringify({ data: paramsMi }), //需要传递的参数
  416. success: function (res) {
  417. res = Decrypt(res)
  418. //调用解密方法 //请求成功后的操作 //在控制台输出返回结果
  419. },
  420. error: function (err) { //请求失败后的操作 //请求失败在控制台输出22
  421. }
  422. })
  423. }
  424. //切水果游戏变量
  425. let apple;
  426. let appleLeft;
  427. let appleRight;
  428. let banana;
  429. let bananaLeft;
  430. let bananaRight;
  431. let sandia;
  432. let sandiaLeft;
  433. let sandiaRight;
  434. let basaha;
  435. let basahaLeft;
  436. let basahaRight;
  437. let bg;
  438. let ctx;
  439. let widthLeft;
  440. //退出按钮
  441. let button1;
  442. let button2;
  443. //等于1时代表在水果游戏页面
  444. //等于2时代表在抓虫子页面
  445. let fruitFlag = 1;
  446. //水果下降的速度
  447. let y = [20, 20, 20, 20];
  448. let speed = [1, 1.5, 1.3, 1.4];
  449. let x = [100, 300, 500, 700]
  450. let rondom;
  451. let timer;
  452. //设置水果的起点
  453. let fruitStart = 10
  454. //设置说过的结尾
  455. let fruitEnd = 900;
  456. //是否隐藏
  457. let flag = [true, true, true, true]
  458. //苹果左右碎片
  459. let apple_x_left = x[0];
  460. let apple_x_right = x[0];
  461. let banana_x_left = x[1];
  462. let banana_x_right = x[1]
  463. let sandia_x_left = x[2];
  464. let sandia_x_right = x[2]
  465. let basaha_x_left = x[3];
  466. let basaha_x_right = x[3];
  467. let targ = [0, 0]
  468. let tempCanvasFruit;
  469. let tempCtxFruit;
  470. let patternFruit;
  471. //水果倒计时初始时间
  472. let startTimeFruit;
  473. //设置倒计时结束时间
  474. let endTimeFruit;
  475. let canvas = document.getElementById('solar');
  476. ctx = canvas.getContext("2d");
  477. window.addEventListener("resize", resizeCanvas, false);
  478. let countdownFlag = false;
  479. let diff = 1;
  480. let count = 0
  481. //第几关进行提示
  482. let diffFlag = false;
  483. //通关提示
  484. //当passFlag默认为1为正常状态
  485. //当passFlag为2时为超时未通过状态
  486. //当passFlag为3时为已通关状态
  487. let passFlag = 1;
  488. //训练提示页flag
  489. let trainingFlag = 1
  490. //第一张训练图片
  491. let trainOne;
  492. let trainTwo;
  493. let trainThree;
  494. let trainFour;
  495. let onStep;
  496. let underStep;
  497. let startGame;
  498. let backMa;
  499. //通关图片
  500. let success;
  501. //声音的图片
  502. let soundImgClose;
  503. let soundImgOpen;
  504. //canvas监听事件
  505. canvas.addEventListener("mouseup", clickButton);
  506. function resizeCanvas() {
  507. canvas.width = window.innerWidth;
  508. x = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]
  509. xWorm = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]
  510. // canvas.height = window.innerHeight;
  511. canvas.height = window.innerHeight;
  512. fruitEnd = canvas.height
  513. wormStart = canvas.height
  514. //canvas.height = '100vh';
  515. apple_x_left = x[0];
  516. apple_x_right = x[0];
  517. banana_x_left = x[1];
  518. banana_x_right = x[1]
  519. sandia_x_left = x[2];
  520. sandia_x_right = x[2]
  521. basaha_x_left = x[3];
  522. basaha_x_right = x[3];
  523. }
  524. //获取
  525. // init()
  526. //初始化切水果的图
  527. //第几关2秒显示
  528. function middleFont() {
  529. diffFlag = true;
  530. setTimeout(() => {
  531. diffFlag = false;
  532. }, 2000);
  533. }
  534. function soundChange() {
  535. if (soundFlag) {
  536. soundOpen()
  537. } else {
  538. soundClose()
  539. }
  540. }
  541. function clickButton(event) {
  542. //鼠标在当前画布的位置
  543. // let x1 = event.clientX;
  544. // let y1 = event.clientY;
  545. let x = event.clientX - canvas.getBoundingClientRect().left;
  546. let y = event.clientY - canvas.getBoundingClientRect().top;
  547. //判断点是否在长方形内
  548. //当在第一步训练时 只有一个按钮下一步的按钮
  549. if (trainingFlag == 1) {
  550. if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 100) && y < (canvas.height / 2 + +100 + 80)) {
  551. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  552. soundButtonOpen()
  553. soundSrc = 'sound/fruitMus1.mp3'
  554. soundClose()
  555. if (soundFlag) {
  556. soundOpen()
  557. }
  558. trainingFlag = 2
  559. }
  560. } else if (trainingFlag == 2) { //当在第二步训练时 有上一步下一步按钮
  561. //当是在点击声音播放声音时
  562. //ctx.drawImage(soundImgOpen, canvas.width -widthLeft- 90, 30, 58, 46);
  563. if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
  564. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  565. soundButtonOpen()
  566. soundFlag = !soundFlag
  567. soundChange()
  568. }
  569. // 上一步按钮
  570. if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  571. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  572. soundButtonOpen()
  573. trainingFlag = 1
  574. soundClose()
  575. }
  576. // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  577. // 上一步按钮
  578. if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  579. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  580. soundButtonOpen()
  581. trainingFlag = 3
  582. soundClose()
  583. soundSrc = 'sound/fruitMus2.mp3'
  584. if (soundFlag) {
  585. soundOpen()
  586. }
  587. }
  588. // ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
  589. } else if (trainingFlag == 3) {
  590. //当是在点击声音播放声音时
  591. if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
  592. soundButtonOpen()
  593. soundFlag = !soundFlag
  594. soundChange()
  595. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  596. // soundOpen()
  597. }
  598. // 上一步按钮
  599. if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  600. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  601. soundButtonOpen()
  602. trainingFlag = 2
  603. soundSrc = 'sound/fruitMus1.mp3'
  604. soundClose()
  605. if (soundFlag) {
  606. soundOpen()
  607. }
  608. }
  609. // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  610. // 上一步按钮
  611. if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  612. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  613. //判断是否摄像头初始化完成,如果初始化完成则继续往下走,如果未初始化完成则停止且进行提示
  614. soundButtonOpen()
  615. soundBgOpen()
  616. if (cameraFlag) {
  617. soundClose()
  618. calibrationAccuracy()
  619. } else {
  620. $('#came').show()
  621. setTimeout(() => {
  622. $('#came').hide()
  623. }, 2000);
  624. }
  625. }
  626. } else if (trainingFlag == 4) {
  627. if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  628. soundButtonOpen()
  629. soundBgClose();
  630. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  631. trainingFlag = 1
  632. $('#target').hide()
  633. // init()
  634. }
  635. }
  636. //ctx.drawImage(startGame, canvas.width / 2 -80 , canvas.height / 2 + 100, 160, 80);
  637. // if (x > (canvas.width / 2 - 100) && x < (canvas.width / 2) && y > (canvas.height / 2 + 100) && y < (canvas.height / 2 + 140)) {
  638. // if (trainingFlag == 1) {
  639. // trainingFlag = 2
  640. // } else if (trainingFlag == 2) {
  641. // trainingFlag = 3
  642. // } else if (trainingFlag == 3) {
  643. // calibrationAccuracy()
  644. // }
  645. // }
  646. //ctx.drawImage(backMa, 20, 20, 40, 30);
  647. // ctx.drawImage(backMa, widthLeft+40, 30, 58, 46);
  648. if (x > widthLeft + 40 && x < widthLeft + 98 && y > 30 && y < 76) {
  649. soundButtonOpen()
  650. //点击退出按钮就返回到感统综合页面即可
  651. //window.location.href = 'http://49.232.79.112:8088/home'
  652. // window.location.href = 'https://gengnao.cn/home'
  653. window.location.href = 'https://org.jue-ming.com/welcome/mainTable'
  654. }
  655. }
  656. function soundClose() {
  657. player.pause()
  658. }
  659. function soundOpen() {
  660. mp3Url = soundSrc;
  661. player = new Audio(mp3Url);
  662. player.play()
  663. }
  664. //按钮点击音效
  665. function soundButtonOpen() {
  666. playerButton.play()
  667. }
  668. //切水果音效
  669. function soundHitOpen() {
  670. playerHit.play()
  671. }
  672. //背景音乐音效
  673. function soundBgOpen() {
  674. playerBg.play()
  675. playerBg.loop = true;
  676. }
  677. function soundBgClose() {
  678. playerBg.pause()
  679. }
  680. function init() {
  681. middleFont()
  682. //第一张训练图片
  683. trainOne = new Image();
  684. trainTwo = new Image();
  685. trainThree = new Image();
  686. trainFour = new Image();
  687. onStep = new Image();
  688. underStep = new Image()
  689. startGame = new Image();
  690. backMa = new Image();
  691. soundImgClose = new Image();
  692. soundImgOpen = new Image();
  693. success = new Image();
  694. button1 = new Image;
  695. //苹果
  696. apple = new Image();
  697. //苹果碎片
  698. appleLeft = new Image();
  699. appleRight = new Image();
  700. //香蕉
  701. banana = new Image();
  702. bananaLeft = new Image();
  703. bananaRight = new Image();
  704. //西瓜
  705. sandia = new Image();
  706. sandiaLeft = new Image();
  707. sandiaRight = new Image();
  708. //草莓
  709. basaha = new Image();
  710. basahaLeft = new Image();
  711. basahaRight = new Image();
  712. bg = new Image();
  713. success.src = './image/success.png'
  714. trainOne.src = './image/fruit1.png'
  715. trainTwo.src = './image/fruit2.png'
  716. trainThree.src = './image/fruit3.png'
  717. trainFour.src = './image/fruit4.png'
  718. onStep.src = './image/onStep.png'
  719. underStep.src = './image/underStep.png'
  720. startGame.src = './image/startGame.png'
  721. backMa.src = './image/backMa.png'
  722. soundImgClose.src = './image/close.png'
  723. soundImgOpen.src = './image/open.png'
  724. button1.src = './image/button1.png'
  725. apple.src = "./image/apple.png";
  726. appleLeft.src = './image/apple-1.png';
  727. appleRight.src = './image/apple-2.png';
  728. banana.src = './image/banana.png';
  729. bananaLeft.src = './image/banana-1.png';
  730. bananaRight.src = './image/banana-2.png';
  731. sandia.src = './image/sandia.png'
  732. sandiaLeft.src = './image/sandia-2.png'
  733. sandiaRight.src = './image/sandia-1.png'
  734. basaha.src = './image/basaha.png'
  735. basahaLeft.src = './image/basaha-2.png'
  736. basahaRight.src = './image/basaha-1.png'
  737. bg.src = './image/background.png'
  738. //canvas =
  739. //let canvas = document.querySelector("#solar");
  740. apple.onload = function () {
  741. //设置初始时间
  742. //countdown()
  743. draw()
  744. }
  745. // bg.onload = imgFn;
  746. }
  747. //初始化虫子的变量
  748. //虫子1
  749. let wormOne;
  750. //虫子二
  751. let wormTwo;
  752. //虫子3
  753. let wormThree;
  754. //虫子4
  755. let wormFour;
  756. let bgtree;
  757. let timerWorm;
  758. //水果下降的速度
  759. let yWorm = [600, 600, 600, 600];
  760. let speedWorm = [1, 1.5, 1.3, 1.4];
  761. let xWorm = [100, 300, 500, 700]
  762. //设置虫子的起点
  763. let wormStart = 600
  764. //设置说过的结尾
  765. let wormEnd = 20;
  766. let tempCanvas;
  767. let tempCtx;
  768. let pattern
  769. //初始化抓虫子的图
  770. function initWorm() {
  771. wormOne = new Image();
  772. wormTwo = new Image();
  773. wormThree = new Image();
  774. wormFour = new Image();
  775. bgtree = new Image();
  776. wormOne.src = './image/bug1.png';
  777. wormTwo.src = './image/bug2.png';
  778. wormThree.src = './image/bug3.png';
  779. wormFour.src = './image/bug4.png';
  780. bgtree.src = './image/tree.png'
  781. wormFlag = [true, true, true, true]
  782. bgtree.onload = function () {
  783. //先把加载好的图片绘制在一个临时的画布上
  784. drawWorm()
  785. }
  786. }
  787. //画虫子
  788. function drawWorm() {
  789. ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
  790. // let a = ctx.createPattern(bgtree, 'repeat')
  791. // ctx.fillStyle = a;
  792. // ctx.fillRect(0, 0, canvas.width, canvas.height)
  793. tempCanvas = document.createElement('canvas');
  794. tempCanvas.width = canvas.width;
  795. tempCanvas.height = canvas.height;
  796. tempCtx = tempCanvas.getContext('2d');
  797. tempCtx.drawImage(bgtree, 0, 0, canvas.width, canvas.height);
  798. pattern = ctx.createPattern(tempCanvas, 'repeat');
  799. ctx.fillStyle = pattern;
  800. ctx.fillRect(0, 0, canvas.width, canvas.height);
  801. ctx.font = "40px Arial";
  802. ctx.strokeText("Hello World", 10, 50);
  803. // ctx.fillText("Hello World", 10, 50);
  804. //虫子1
  805. ctx.drawImage(wormOne, xWorm[0], yWorm[0], 60, 60);
  806. ctx.drawImage(wormTwo, xWorm[1], yWorm[1], 60, 60);
  807. //虫子三
  808. ctx.drawImage(wormThree, xWorm[2], yWorm[2], 60, 60);
  809. //虫子4
  810. ctx.drawImage(wormFour, xWorm[3], yWorm[3], 60, 60);
  811. if (wormFlag[0]) {
  812. yWorm[0] = yWorm[0] - speedWorm[0] * random
  813. } else {
  814. yWorm[0] = yWorm[0] + speedWorm[0] * random * 1.5
  815. }
  816. if (yWorm[0] < wormEnd || yWorm[0] > wormStart) {
  817. yWorm[0] = wormStart
  818. wormFlag[0] = true
  819. random = Math.random() + 1
  820. }
  821. if (wormFlag[1]) {
  822. yWorm[1] = yWorm[1] - speedWorm[1] * random
  823. } else {
  824. yWorm[1] = yWorm[1] + speedWorm[1] * random * 1.5
  825. }
  826. //yWorm[1] = yWorm[1] - speedWorm[1] * random
  827. if (yWorm[1] < wormEnd || yWorm[1] > wormStart) {
  828. yWorm[1] = wormStart
  829. wormFlag[1] = true;
  830. random = Math.random() + 1
  831. }
  832. if (wormFlag[2]) {
  833. yWorm[2] = yWorm[2] - speedWorm[2] * random
  834. } else {
  835. yWorm[2] = yWorm[2] + speedWorm[2] * random * 1.5
  836. }
  837. //yWorm[2] = yWorm[2] - speedWorm[2] * random
  838. if (yWorm[2] < wormEnd || yWorm[2] > wormStart) {
  839. yWorm[2] = wormStart
  840. wormFlag[2] = true;
  841. random = Math.random() + 1
  842. }
  843. if (wormFlag[3]) {
  844. yWorm[3] = yWorm[3] - speedWorm[3] * random
  845. } else {
  846. yWorm[3] = yWorm[3] + speedWorm[3] * random * 1.5
  847. }
  848. if (yWorm[3] < wormEnd || yWorm[3] > wormStart) {
  849. yWorm[3] = wormStart
  850. wormFlag[3] = true;
  851. random = Math.random() + 1
  852. }
  853. let first = [xWorm[0] + 30, yWorm[0] + 30]
  854. //第二列中心点坐标
  855. let second = [xWorm[1] + 30, yWorm[1] + 30]
  856. let third = [xWorm[2] + 30, yWorm[2] + 30]
  857. let four = [xWorm[3] + 30, yWorm[3] + 30]
  858. wormCollision(first, second, third, four)
  859. timerWorm = requestAnimationFrame(drawWorm);
  860. if (fruitFlag !== 2) {
  861. window.cancelAnimationFrame(timerWorm)
  862. ctx.clearRect(0, 0, 900, 900); //清空所有的内容
  863. }
  864. }
  865. function draw() {
  866. widthLeft = (canvas.width - (1270 / 714 * canvas.height)) / 2
  867. ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
  868. // // let a = ctx.createPattern(bg, 'repeat')
  869. // // ctx.fillStyle = a;
  870. // // ctx.fillRect(0, 0, canvas.width, canvas.height)
  871. // /*绘制 太阳*/
  872. // tempCanvasFruit = document.createElement('canvas');
  873. // tempCanvasFruit.width = 814;
  874. // tempCanvasFruit.height = canvas.height;
  875. // tempCtxFruit = tempCanvasFruit.getContext('2d');
  876. // // tempCtxFruit.drawImage(bg, 0, 0, canvas.width, canvas.height);
  877. // tempCtxFruit.drawImage(bg, 0, 0, canvas.width, canvas.height);
  878. // patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  879. // ctx.fillStyle = patternFruit;
  880. // //ctx.fillRect(0, 0, canvas.width, canvas.height);
  881. // ctx.fillRect(0, 0, 814, canvas.height);
  882. // 1270/714*
  883. if (trainingFlag == 1) {
  884. tempCanvasFruit = document.createElement('canvas');
  885. tempCanvasFruit.width = canvas.width;
  886. tempCanvasFruit.height = canvas.height;
  887. tempCtxFruit = tempCanvasFruit.getContext('2d');
  888. //tempCtxFruit.drawImage(trainOne, 0, 0, 814/608*canvas.height, canvas.height);
  889. tempCtxFruit.drawImage(trainOne, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  890. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  891. ctx.fillStyle = patternFruit;
  892. ctx.fillRect(0, 0, canvas.width, canvas.height);
  893. //增加下一步的按钮。点击后将trainningFlag设置2
  894. ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  895. //返回按钮 //(canvas.width - (1270/714*canvas.height)) / 2
  896. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  897. }
  898. if (trainingFlag == 2) {
  899. tempCanvasFruit = document.createElement('canvas');
  900. tempCanvasFruit.width = canvas.width;
  901. tempCanvasFruit.height = canvas.height;
  902. tempCtxFruit = tempCanvasFruit.getContext('2d');
  903. tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  904. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  905. ctx.fillStyle = patternFruit;
  906. ctx.fillRect(0, 0, canvas.width, canvas.height);
  907. ctx.drawImage(trainThree, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
  908. //上一步按钮
  909. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
  910. //下一步按钮
  911. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
  912. //返回按钮
  913. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  914. if (soundFlag) {
  915. ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
  916. } else {
  917. ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
  918. }
  919. }
  920. if (trainingFlag == 3) {
  921. tempCanvasFruit = document.createElement('canvas');
  922. tempCanvasFruit.width = canvas.width;
  923. tempCanvasFruit.height = canvas.height;
  924. tempCtxFruit = tempCanvasFruit.getContext('2d');
  925. tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  926. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  927. ctx.fillStyle = patternFruit;
  928. ctx.fillRect(0, 0, canvas.width, canvas.height);
  929. ctx.drawImage(trainFour, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
  930. //上一步按钮
  931. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
  932. //下一步按钮
  933. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
  934. //返回按钮
  935. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  936. if (soundFlag) {
  937. // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30);
  938. ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
  939. } else {
  940. // ctx.drawImage(soundImgClose, canvas.width - 60, 20, 40, 30);
  941. ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
  942. }
  943. }
  944. if (trainingFlag == 4) {
  945. tempCanvasFruit = document.createElement('canvas');
  946. tempCanvasFruit.width = canvas.width;
  947. tempCanvasFruit.height = canvas.height;
  948. tempCtxFruit = tempCanvasFruit.getContext('2d');
  949. tempCtxFruit.drawImage(trainTwo, 0, 0, canvas.width, canvas.height);
  950. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  951. ctx.fillStyle = patternFruit;
  952. ctx.fillRect(0, 0, canvas.width, canvas.height);
  953. var gradient = ctx.createLinearGradient(0, 0, 100, 0);
  954. //返回按钮
  955. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  956. if (countdownFlag) {
  957. gradient.addColorStop("1.0", "#D1BBFF");
  958. // Fill with gradient
  959. ctx.strokeStyle = gradient;
  960. ctx.font = "30px Arial";
  961. ctx.strokeText("倒计时:" + countdown(), canvas.width - 240, 100);
  962. gradient.addColorStop("1.0", "#D1BBFF");
  963. // Fill with gradient
  964. ctx.strokeStyle = gradient;
  965. ctx.font = "30px Arial";
  966. ctx.strokeText("第" + diff + '关', 100, 100);
  967. ctx.font = "30px Arial";
  968. ctx.strokeText("得分:" + count, canvas.width - 240, 150);
  969. // ctx.fillText("Hello World", 10, 50);
  970. //切水果倒计时三分钟
  971. //初始化
  972. }
  973. if (diffFlag) {
  974. gradient.addColorStop("1.0", "yellow");
  975. // Fill with gradient
  976. ctx.strokeStyle = gradient;
  977. ctx.font = "80px Arial";
  978. ctx.strokeText("第 " + diff + ' 关', canvas.width / 2 - 120, canvas.height / 2 - 100);
  979. }
  980. if (passFlag == 2) {
  981. gradient.addColorStop("1.0", "red");
  982. // Fill with gradient
  983. ctx.strokeStyle = gradient;
  984. ctx.font = "20px Arial";
  985. ctx.strokeText("很遗憾您已超时,继续加油!", canvas.width / 2 - 100, canvas.height / 2 - 100);
  986. //ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  987. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
  988. countdownFlag = false;
  989. diffFlag = false;
  990. //重新开始按钮 //或返回主页
  991. // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  992. }
  993. //
  994. if (passFlag == 3) {
  995. gradient.addColorStop("1.0", "yellow");
  996. // Fill with gradient
  997. ctx.strokeStyle = gradient;
  998. // ctx.font = "50px Arial";
  999. // ctx.strokeText("您已通关!!!", canvas.width / 2 - 120, canvas.height / 2 - 100);
  1000. ctx.drawImage(success, canvas.width / 2 - 325, 10);
  1001. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
  1002. countdownFlag = false;
  1003. diffFlag = false;
  1004. //重新开始按钮 //或返回主页
  1005. // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  1006. }
  1007. // ctx.drawImage(button1, canvas.width / 2 - 100, canvas.height / 2 + 100, 100, 40);
  1008. // ctx.drawImage(button1, canvas.width / 2 + 100, canvas.height / 2 + 100, 100, 40);
  1009. if (flag[0]) {
  1010. ctx.drawImage(apple, x[0], y[0], 60, 60);
  1011. getColl()
  1012. } else {
  1013. apple_x_left += 0.5
  1014. apple_x_right -= 0.5
  1015. ctx.drawImage(appleLeft, apple_x_right, y[0], 60, 60);
  1016. ctx.drawImage(appleRight, apple_x_left, y[0], 60, 60);
  1017. }
  1018. if (flag[1]) {
  1019. ctx.drawImage(banana, x[1], y[1], 60, 30);
  1020. getColl()
  1021. } else {
  1022. banana_x_left += 0.5
  1023. banana_x_right -= 0.5
  1024. ctx.drawImage(bananaLeft, banana_x_left, y[1], 60, 30);
  1025. ctx.drawImage(bananaRight, banana_x_right, y[1], 60, 30);
  1026. }
  1027. if (flag[2]) {
  1028. ctx.drawImage(sandia, x[2], y[2], 60, 60);
  1029. getColl()
  1030. } else {
  1031. sandia_x_left += 0.5
  1032. sandia_x_right -= 0.5
  1033. ctx.drawImage(sandiaLeft, sandia_x_left, y[2], 60, 60);
  1034. ctx.drawImage(sandiaRight, sandia_x_right, y[2], 60, 60);
  1035. }
  1036. if (flag[3]) {
  1037. ctx.drawImage(basaha, x[3], y[3], 60, 60);
  1038. getColl()
  1039. } else {
  1040. basaha_x_left += 0.5
  1041. basaha_x_right -= 0.5
  1042. ctx.drawImage(basahaLeft, basaha_x_left, y[3], 60, 60);
  1043. ctx.drawImage(basahaRight, basaha_x_right, y[3], 60, 60);
  1044. }
  1045. random = Math.random() + 1
  1046. y[0] = y[0] + speed[0] * random
  1047. if (y[0] > fruitEnd) {
  1048. y[0] = fruitStart
  1049. flag[0] = true
  1050. apple_x_left = x[0]
  1051. apple_x_right = x[0]
  1052. }
  1053. random = Math.random() + 1
  1054. y[1] = y[1] + speed[1] * random
  1055. if (y[1] > fruitEnd) {
  1056. y[1] = fruitStart
  1057. flag[1] = true;
  1058. banana_x_left = x[1];
  1059. banana_x_right = x[1]
  1060. }
  1061. random = Math.random() + 1
  1062. y[2] = y[2] + speed[2] * random
  1063. if (y[2] > fruitEnd) {
  1064. y[2] = fruitStart;
  1065. flag[2] = true;
  1066. sandia_x_left = x[2];
  1067. sandia_x_right = x[2]
  1068. }
  1069. random = Math.random() + 1
  1070. y[3] = y[3] + speed[3] * random
  1071. if (y[3] > fruitEnd) {
  1072. y[3] = fruitStart
  1073. flag[3] = true;
  1074. basaha_x_left = x[3]
  1075. basaha_x_right = x[3]
  1076. }
  1077. }
  1078. timer = requestAnimationFrame(draw);
  1079. if (fruitFlag !== 1) {
  1080. //取消循环
  1081. window.cancelAnimationFrame(timer)
  1082. ctx.clearRect(0, 0, 900, 900); //清空所有的内容
  1083. }
  1084. //
  1085. }
  1086. //在合适的时机调用碰撞检测方法
  1087. function getColl() {
  1088. let first = [x[0] + 30, y[0] + 30]
  1089. //第二列中心点坐标
  1090. let second = [x[1] + 30, y[1] + 15]
  1091. let third = [x[2] + 30, y[2] + 30]
  1092. let four = [x[3] + 30, y[3] + 30]
  1093. //碰撞检测方法
  1094. collision(first, second, third, four)
  1095. }
  1096. //分数判断方法
  1097. function countComputer() {
  1098. if (diff == 1) {
  1099. if (passFlag == 1) {
  1100. count += 5
  1101. }
  1102. }
  1103. if (diff == 2) {
  1104. // resetTime()
  1105. if (passFlag == 1) {
  1106. count += 5
  1107. }
  1108. }
  1109. if (diff == 3) {
  1110. // resetTime()
  1111. if (passFlag == 1) {
  1112. count += 5
  1113. }
  1114. }
  1115. }
  1116. //水果的相交方法
  1117. function collision(first, second, third, four) {
  1118. //判断是否与第一个相交
  1119. if (flag[0]) {
  1120. if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
  1121. //两个面积相交了\
  1122. soundHitOpen()
  1123. flag[0] = false
  1124. countComputer()
  1125. }
  1126. }
  1127. //第二条线相交
  1128. if (flag[1]) {
  1129. if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 30 / 2))) {
  1130. //两个面积相交了
  1131. soundHitOpen()
  1132. flag[1] = false
  1133. countComputer()
  1134. }
  1135. }
  1136. //第三条线相交
  1137. if (flag[2]) {
  1138. if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
  1139. //两个面积相交了
  1140. soundHitOpen()
  1141. flag[2] = false
  1142. countComputer()
  1143. }
  1144. }
  1145. //第四条线相交
  1146. //第三条线相交
  1147. if (flag[3]) {
  1148. if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
  1149. //两个面积相交了
  1150. soundHitOpen()
  1151. flag[3] = false
  1152. countComputer()
  1153. }
  1154. }
  1155. }
  1156. //虫子的相交方法
  1157. function wormCollision(first, second, third, four) {
  1158. //判断是否与第一个相交console
  1159. if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
  1160. //两个面积相交了
  1161. wormFlag[0] = false
  1162. }
  1163. //第二条线相交
  1164. if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 60 / 2))) {
  1165. //两个面积相交了
  1166. wormFlag[1] = false
  1167. }
  1168. //第三条线相交
  1169. if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
  1170. //两个面积相交了
  1171. wormFlag[2] = false
  1172. }
  1173. //第四条线相交
  1174. //第三条线相交
  1175. if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
  1176. //两个面积相交了
  1177. wormFlag[3] = false
  1178. }
  1179. }
  1180. //倒计时方法
  1181. function countdown() {
  1182. startTimeFruit = new Date().getTime();
  1183. let surplusTime = endTimeFruit - startTimeFruit;
  1184. let th = Math.floor(surplusTime / (1000 * 60 * 60) % 24);
  1185. let tm = Math.floor(surplusTime / (1000 * 60) % 60);
  1186. let ts = Math.floor(surplusTime / 1000 % 60);
  1187. if (ts < 10) {
  1188. ts = '0' + ts
  1189. }
  1190. if (surplusTime >= 0) {
  1191. if (!(passFlag == 2 || passFlag == 3)) {
  1192. countdownFlag = true
  1193. }
  1194. return '0' + th + ':' + '0' + tm + ':' + ts
  1195. } else {
  1196. //当计时为小于0时
  1197. //如果是第一关
  1198. //判断当前的分数是否超过90分
  1199. //当分数是第一关且分数超过90分执行保存命令且进入第二关
  1200. //然后重新计时
  1201. if (diff == 1) {
  1202. if (count >= 90) {
  1203. //执行保存结果
  1204. levelFun(diff, count)
  1205. totalScore += count
  1206. middleFont()
  1207. speed = [1.5, 2.0, 2.3, 2.4];
  1208. //且进行将关卡设置为2
  1209. diff = 2;
  1210. count = 0
  1211. //将时间重置为3分钟
  1212. resetTime()
  1213. } else {
  1214. if (passFlag != 2) {
  1215. levelFun(diff, count)
  1216. }
  1217. passFlag = 2
  1218. //很遗憾失败了
  1219. }
  1220. } else if (diff == 2) {
  1221. if (count >= 60) {
  1222. middleFont()
  1223. levelFun(diff, count)
  1224. totalScore += count
  1225. speed = [2.0, 2.5, 2.8, 3.1];
  1226. diff = 3;
  1227. count = 0
  1228. //将时间重置为3分钟
  1229. resetTime()
  1230. } else {
  1231. if (passFlag != 2) {
  1232. levelFun(diff, count)
  1233. }
  1234. passFlag = 2
  1235. }
  1236. } else if (diff == 3) {
  1237. if (count >= 45) {
  1238. if (passFlag != 3) {
  1239. totalScore += count
  1240. levelFun(diff, count)
  1241. saveResult(totalScore)
  1242. }
  1243. passFlag = 3
  1244. } else {
  1245. if (passFlag != 2) {
  1246. levelFun(diff, count)
  1247. }
  1248. passFlag = 2
  1249. }
  1250. }
  1251. return '00' + ':' + '00' + ':' + '00'
  1252. // levelFun(diff, count)
  1253. // saveResult(count)
  1254. // countdownFlag = false
  1255. }
  1256. }
  1257. //捉虫子游戏变量
  1258. $('#solar').hide();
  1259. $('#exit').hide();
  1260. $('#refresh').hide();
  1261. let WASM_URL = "js/gazefilter.wasm";
  1262. var buttonList = []
  1263. $('#target').hide();
  1264. $('#target1').hide();
  1265. var record = {
  1266. beforePoint: -1,
  1267. currentPoint: 0,
  1268. }
  1269. //校准方法
  1270. function calibrationAccuracy() {
  1271. $('#solar').hide();
  1272. handleFullScreen();
  1273. getFun()
  1274. $('#target1').show();
  1275. $('#startCalibration').hide();
  1276. switchTime()
  1277. //初始化游戏
  1278. $('#target1').hide()
  1279. // $('#target').show()
  1280. //clearInterval(time)
  1281. //$('#solar').show();
  1282. //$('#exit').show();
  1283. // $('#refresh').show();
  1284. }
  1285. $("#startCalibration").click(function () {
  1286. // handleFullScreen(); //全屏
  1287. handleFullScreen();
  1288. getFun()
  1289. $('#target1').show();
  1290. $('#startCalibration').hide();
  1291. //switchTime()
  1292. //初始化游戏
  1293. //
  1294. $('#target1').hide()
  1295. $('#target').show()
  1296. clearInterval(time)
  1297. $('#solar').show();
  1298. //$('#exit').show();
  1299. // $('#refresh').show();
  1300. init();
  1301. // $('.enterClass').css('display','none');
  1302. // $('.returnClass').css('display','block');
  1303. });
  1304. //切换游戏
  1305. $('#exit').click(function () {
  1306. //隐藏画布一
  1307. // $('#exit').innerHtml()
  1308. if (fruitFlag == 1) {
  1309. setTimeout(() => {
  1310. initWorm()
  1311. fruitFlag = 2
  1312. }, 30);
  1313. } else {
  1314. setTimeout(() => {
  1315. init()
  1316. fruitFlag = 1
  1317. }, 30);
  1318. }
  1319. })
  1320. $('#refresh').click(function () {
  1321. location.reload()
  1322. })
  1323. function handleFullScreen() {
  1324. var de = document.documentElement;
  1325. if (de.requestFullscreen) {
  1326. de.requestFullscreen();
  1327. } else if (de.msRequestFullscreen) {
  1328. de.msRequestFullscreen();
  1329. } else if (de.webkitRequestFullScreen) {
  1330. de.webkitRequestFullScreen();
  1331. } else if (de.mozRequestFullScreen) {
  1332. de.mozRequestFullScreen();
  1333. } else {
  1334. wtx.info("当前浏览器不支持全屏!");
  1335. }
  1336. }
  1337. //重置通过状态
  1338. function resetStatus() {
  1339. //重置是否通过的状态
  1340. passFlag = 1;
  1341. //重置速度
  1342. speed = [1, 1.5, 1.3, 1.4];
  1343. }
  1344. //重置计时函数
  1345. function resetTime() {
  1346. startTimeFruit = new Date().getTime();
  1347. endTimeFruit = startTimeFruit + 180000;
  1348. countdownFlag = true
  1349. }
  1350. //重置难度重置关数
  1351. function resetCount(val, value) {
  1352. //设置难度为多少
  1353. diff = val;
  1354. //设置分数为多少
  1355. count = value;
  1356. }
  1357. //轮询当前的图表
  1358. var time = null;
  1359. function switchTime() {
  1360. let a = 0
  1361. time = setInterval(() => {
  1362. $('#target1').show()
  1363. $('#targetMini').show()
  1364. if (a == 16) {
  1365. $('#target1').hide()
  1366. $('#target').show()
  1367. clearInterval(time)
  1368. $('#solar').show();
  1369. // $('#exit').show();
  1370. // $('#refresh').show();
  1371. trainingFlag = 4
  1372. //重置计时函数
  1373. resetTime()
  1374. //重置分数和参数
  1375. resetCount(1, 0)
  1376. //都重置到初始状态
  1377. resetStatus();
  1378. } else {
  1379. $('#targetMini1').css('transition', '0s');
  1380. $('#targetMini1').css('width', 54 + 'px');
  1381. $('#targetMini1').css('height', 54 + 'px');
  1382. let x = buttonList[a].x1;
  1383. x = Math.floor(x)
  1384. let y = buttonList[a].y1;
  1385. y = Math.floor(y)
  1386. $('#target1').css('left', x + 'px');
  1387. $('#target1').css('top', y + 'px');
  1388. // init();
  1389. setTimeout(() => {
  1390. $('#targetMini1').css('transition', '1s');
  1391. $('#targetMini1').css('width', 20 + 'px');
  1392. $('#targetMini1').css('height', 20 + 'px');
  1393. }, 1000);
  1394. setTimeout(() => {
  1395. coordinates(x + 30, y + 30)
  1396. }, 1500);
  1397. setTimeout(() => {
  1398. coordinates(x + 30, y + 30)
  1399. }, 1840);
  1400. setTimeout(() => {
  1401. coordinates(x + 30, y + 30)
  1402. }, 1940);
  1403. a = a + 1
  1404. }
  1405. }, 2000)
  1406. }
  1407. function coordinates(x, y) {
  1408. gazefilter.tracker.calibrate( //校正,发送当前点击位置进行校正
  1409. //event.timeStamp,
  1410. window.performance.now(),
  1411. //window.performance.now(),
  1412. //mill,
  1413. //1000, // relative to performance.timeOrigin
  1414. x, // in pixels
  1415. y, // in pixels
  1416. 1.0 // see note below
  1417. );
  1418. }
  1419. //获取各个按钮的left和top
  1420. /**
  1421. * 在屏幕上画出注视点
  1422. * 是否画点,以及画点方式均可自定义
  1423. **/
  1424. function drawTargetOnScreen(position) {
  1425. if (position != null && position.length == 2) {
  1426. //判断是否是NAN如果是NAN则不执行
  1427. //如果不是则需要判断当前点是否在第一个点中,如果在则执行settimeout函数1秒后改变改块状态
  1428. //如果在第一个点中但第二次坐标出来的时候还在第一个坐标里那就没问题
  1429. // $('#target').css('display', 'block');
  1430. targ[0] = position[0] + 60;
  1431. targ[1] = position[1] + 60;
  1432. $('#target').css('left', position[0] + 'px');
  1433. $('#target').css('top', position[1] + 'px');
  1434. // $('#target').css('z-index', 100);
  1435. //judge(position[0], position[1])
  1436. // judge(position[0], position[1])
  1437. }
  1438. }
  1439. //判断是否在第一个点中
  1440. //获取全部的校验点
  1441. function getFun() {
  1442. for (let i = 0; i < 16; i++) {
  1443. let a = document.getElementById('num' + (i + 1));
  1444. buttonList.push({
  1445. x1: a.getBoundingClientRect().left,
  1446. y1: a.getBoundingClientRect().top
  1447. })
  1448. }
  1449. }
  1450. async function initGazefilter() {
  1451. cameraFlag = false
  1452. await gazefilter.init(WASM_URL); //初始化
  1453. console.log(1)
  1454. await gazefilter.tracker.connect(); //连接摄像头
  1455. let canvas = document.getElementById("tracker-canvas");
  1456. console.log(12)
  1457. await gazefilter.visualizer.setCanvas(canvas); //绘制图像
  1458. console.log(123)
  1459. cameraFlag = true
  1460. await gazefilter.tracker.addListener("calib", response => {
  1461. //console.log(response.errorCode, event.errorValue); //校验结果 errorCode 0: calibration is successfull;1: screen dimentions should be set;2: calibration sample cannot be created;3: not enough data to fit calibration model.
  1462. });
  1463. }
  1464. function startTrace() {
  1465. gazefilter.tracker.addListener("filter", event => { //注视点
  1466. drawTargetOnScreen(event.bestGazePoint()); //Best gaze point of left or right eye. 其他取点可参考https://beehiveor.gitlab.io/gazefilter/data.html,有不同类型的点 fixationPoint
  1467. });
  1468. }
  1469. function hideSence() {
  1470. //隐藏目标物
  1471. $('#target').hide()
  1472. $('#target1').hide()
  1473. $('#targetMini').hide()
  1474. //隐藏摄像头
  1475. $('#solar').show()
  1476. }
  1477. $("#finishcalibration").click(function () {
  1478. window.removeEventListener("click", onmouseclick); //九点校正完成,取消鼠标事件,并开始绘制轨迹点
  1479. $("#finishcalibration").hide();
  1480. })
  1481. $(document).ready(function () {
  1482. //将canvas自适应屏幕
  1483. resizeCanvas()
  1484. init()
  1485. hideSence()
  1486. //将该隐藏的隐藏然后初始化游戏场景
  1487. //初始华摄像头
  1488. initGazefilter();
  1489. startTrace();
  1490. });
  1491. </script>
  1492. </body>
  1493. </html>