index.html 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. var pre_path = window.location.origin;
  301. $('#came').hide()
  302. var soundSrc = 'sound/worm1.mp3'
  303. var mp3Url = soundSrc;
  304. var soundFlag = true;
  305. var cameraFlag = true
  306. var player = new Audio(mp3Url);
  307. //点击按钮声音
  308. var soundButtonSrc = 'sound/button.mp3'
  309. var mp3Button = soundButtonSrc
  310. var playerButton = new Audio(mp3Button);
  311. //打虫子音效
  312. var soundHitSrc = 'sound/wormHit.mp3'
  313. var mp3Hit = soundHitSrc
  314. var playerHit = new Audio(mp3Hit)
  315. //背景音乐
  316. var soundBgSrc = 'sound/wormMusicBg.mp3'
  317. var mp3Bg = soundBgSrc
  318. var playerBg = new Audio(mp3Bg)
  319. //总分
  320. var totalScore = 0;
  321. var key = CryptoJS.enc.Utf8.parse("Sp5biS+gX+#CqAFF");//密钥
  322. var iv = CryptoJS.enc.Utf8.parse('ud2E8l6wchltwIDA');
  323. //传输参数为字符串
  324. function Encrypt(word) {
  325. let srcs = CryptoJS.enc.Utf8.parse(word);
  326. let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  327. return encrypted.ciphertext.toString().toUpperCase();
  328. }
  329. function Decrypt(word) {
  330. let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  331. let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  332. let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  333. let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  334. return decryptedStr.toString();
  335. }
  336. // levelFun(2, 20)
  337. //
  338. //传输关卡和得分的字符串
  339. function levelFun(level, score) {
  340. //1----获取userId
  341. // let userMi = sessionStorage.getItem('B922C64EC97B4FF08485886B2AAEF40C')
  342. // //解密userMi
  343. // let user = Decrypt(userMi)
  344. // //需要传输的参数
  345. // let userId = JSON.parse(user).id
  346. // //2----获取唯一标识
  347. // let associatedFlag = new Date().getTime() + '';
  348. // var params = {
  349. // userId: userId,
  350. // associatedFlag: associatedFlag,
  351. // gameFlag: 'g_ydrzxlzcz',
  352. // checkPoint: level,
  353. // level: level,
  354. // score: score,
  355. // performance: {},
  356. // data: {}
  357. // }
  358. // // Encrypt(JSON.stringify(params))
  359. // // console.log(Encrypt(JSON.stringify(params)))
  360. // //给参数加密
  361. // let paramsMi = Encrypt(JSON.stringify(params))
  362. // // console.log(Decrypt(paramsMi))
  363. // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
  364. // $.ajax({
  365. // url: "https://child.jue-ming.com:8060/gameLevelRecord/saveResult", //请求接口的地址
  366. // type: "POST",
  367. // contentType: "json/application",
  368. // dataType: 'json', //请求的方法GET/POST
  369. // data: JSON.stringify({ data: paramsMi }),
  370. // headers: {
  371. // 'Authorization': "Bearer " + tokenLin,
  372. // }, //需要传递的参数
  373. // success: function (res) {
  374. // res = Decrypt(res)
  375. // //调用解密方法 //请求成功后的操作
  376. // //在控制台输出返回结果
  377. // },
  378. // error: function (err) { //请求失败后的操作
  379. // //请求失败在控制台输出22
  380. // }
  381. // })
  382. }
  383. function saveResult(score) {
  384. //1----获取userId
  385. let userMi = sessionStorage.getItem('b80bb7740288fda1f201890375a60c8f')
  386. //解密userMi
  387. // let user = Decrypt(userMi)
  388. //需要传输的参数
  389. let userId = userMi
  390. //2----获取唯一标识
  391. let associatedFlag = new Date().getTime() + '';
  392. var params = {
  393. userId: userId,
  394. // associatedFlag: associatedFlag,
  395. // gameFlag: 'g_ydrzxlzcz',
  396. score: score,
  397. testPlanId: getQueryString('testPlanId')
  398. // gameName: '眼动认知训练捉虫子'
  399. }
  400. let paramsMi = Encrypt(JSON.stringify(params))
  401. console.log(paramsMi)
  402. // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
  403. // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
  404. let paramsLin =userId+'token'
  405. let tokenLin = localStorage.getItem(paramsLin);
  406. //获取当前的前端服务器url
  407. //http://cognitive.wistcm.com
  408. //pre_path +
  409. $.ajax({
  410. // url: "http://cognitive.wistcm.com:8090/cognize/InsecthHunting", //请求接口的地址
  411. url: "http://localhost:8099/cognize/InsecthHunting",
  412. type: "POST",
  413. contentType: "json/application",
  414. dataType: 'json', //请求的方法GET/POST
  415. data: JSON.stringify({ data: paramsMi }),
  416. headers: {
  417. 'Authorization': "Bearer " + tokenLin,
  418. }, //需要传递的参数
  419. success: function (res) {
  420. // window.history.back()
  421. res = Decrypt(res)
  422. // goTestResult(res.data);
  423. //返回到浏览器上一页
  424. //调用解密方法 //请求成功后的操作
  425. //在控制台输出返回结果
  426. },
  427. error: function (err) {
  428. //请求失败后的操作
  429. //请求失败在控制台输出22
  430. }
  431. })
  432. }
  433. //
  434. //切水果游戏变量
  435. let apple;
  436. let appleLeft;
  437. let appleRight;
  438. let banana;
  439. let bananaLeft;
  440. let bananaRight;
  441. let sandia;
  442. let sandiaLeft;
  443. let sandiaRight;
  444. let basaha;
  445. let basahaLeft;
  446. let basahaRight;
  447. let bg;
  448. let ctx;
  449. let widthLeft;
  450. //退出按钮
  451. let button1;
  452. let button2;
  453. //等于1时代表在水果游戏页面
  454. //等于2时代表在抓虫子页面
  455. let fruitFlag = 2;
  456. //水果下降的速度
  457. let y = [20, 20, 20, 20];
  458. let speed = [1, 1.5, 1.3, 1.4];
  459. let x = [100, 300, 500, 700]
  460. let rondom;
  461. let timer;
  462. //设置水果的起点
  463. let fruitStart = 10
  464. //设置说过的结尾
  465. let fruitEnd = 900;
  466. //是否隐藏
  467. let flag = [true, true, true, true]
  468. //苹果左右碎片
  469. let apple_x_left = x[0];
  470. let apple_x_right = x[0];
  471. let banana_x_left = x[1];
  472. let banana_x_right = x[1]
  473. let sandia_x_left = x[2];
  474. let sandia_x_right = x[2]
  475. let basaha_x_left = x[3];
  476. let basaha_x_right = x[3];
  477. let targ = [0, 0]
  478. let tempCanvasFruit;
  479. let tempCtxFruit;
  480. let patternFruit;
  481. //水果倒计时初始时间
  482. let startTimeFruit;
  483. //设置倒计时结束时间
  484. let endTimeFruit;
  485. let canvas = document.getElementById('solar');
  486. ctx = canvas.getContext("2d");
  487. window.addEventListener("resize", resizeCanvas, false);
  488. let countdownFlag = false;
  489. let diff = 1;
  490. let count = 0
  491. //第几关进行提示
  492. let diffFlag = false;
  493. //通关提示
  494. //当passFlag默认为1为正常状态
  495. //当passFlag为2时为超时未通过状态
  496. //当passFlag为3时为已通关状态
  497. let passFlag = 1;
  498. //训练提示页flag
  499. let trainingFlag = 1
  500. //第一张训练图片
  501. let trainOne;
  502. let trainTwo;
  503. let trainThree;
  504. let trainFour;
  505. let onStep;
  506. let underStep;
  507. let startGame;
  508. let backMa;
  509. //canvas监听事件
  510. canvas.addEventListener("mouseup", clickButton);
  511. function resizeCanvas() {
  512. canvas.width = window.innerWidth;
  513. x = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]
  514. xWorm = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]
  515. // canvas.height = window.innerHeight;
  516. canvas.height = window.innerHeight;
  517. fruitEnd = canvas.height
  518. wormStart = canvas.height
  519. //canvas.height = '100vh';
  520. apple_x_left = x[0];
  521. apple_x_right = x[0];
  522. banana_x_left = x[1];
  523. banana_x_right = x[1]
  524. sandia_x_left = x[2];
  525. sandia_x_right = x[2]
  526. basaha_x_left = x[3];
  527. basaha_x_right = x[3];
  528. }
  529. //获取
  530. // initWorm()
  531. //初始化切水果的图
  532. //第几关2秒显示
  533. function middleFont() {
  534. diffFlag = true;
  535. setTimeout(() => {
  536. diffFlag = false;
  537. }, 2000);
  538. }
  539. function soundChange() {
  540. if (soundFlag) {
  541. soundOpen()
  542. } else {
  543. soundClose()
  544. }
  545. }
  546. function clickButton(event) {
  547. //鼠标在当前画布的位置
  548. // let x1 = event.clientX;
  549. // let y1 = event.clientY;
  550. let x = event.clientX - canvas.getBoundingClientRect().left;
  551. let y = event.clientY - canvas.getBoundingClientRect().top;
  552. //判断点是否在长方形内
  553. //当在第一步训练时 只有一个按钮下一步的按钮
  554. if (trainingFlag == 1) {
  555. if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 100) && y < (canvas.height / 2 + +100 + 80)) {
  556. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  557. soundButtonOpen()
  558. soundSrc = 'sound/worm1.mp3'
  559. soundClose()
  560. if (soundFlag) {
  561. soundOpen()
  562. }
  563. trainingFlag = 2
  564. }
  565. } else if (trainingFlag == 2) { //当在第二步训练时 有上一步下一步按钮
  566. //当是在点击声音播放声音时
  567. if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
  568. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  569. soundButtonOpen()
  570. soundFlag = !soundFlag
  571. soundChange()
  572. }
  573. // 上一步按钮
  574. if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  575. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  576. soundButtonOpen()
  577. trainingFlag = 1
  578. soundClose()
  579. }
  580. // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  581. // 下一步按钮
  582. if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  583. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  584. soundButtonOpen()
  585. trainingFlag = 3
  586. soundClose()
  587. soundSrc = 'sound/worm2.mp3'
  588. if (soundFlag) {
  589. soundOpen()
  590. }
  591. }
  592. // ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
  593. } else if (trainingFlag == 3) {
  594. //当是在点击声音播放声音时
  595. if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
  596. soundButtonOpen()
  597. soundFlag = !soundFlag
  598. soundChange()
  599. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  600. // soundOpen()
  601. }
  602. // 上一步按钮
  603. if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  604. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  605. soundButtonOpen()
  606. soundSrc = 'sound/worm1.mp3'
  607. soundClose()
  608. if (soundFlag) {
  609. soundOpen()
  610. }
  611. trainingFlag = 2
  612. }
  613. // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  614. // 下一步按钮
  615. if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  616. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  617. // soundClose()
  618. // calibrationAccuracy()
  619. soundBgOpen()
  620. soundButtonOpen()
  621. if (cameraFlag) {
  622. soundClose()
  623. calibrationAccuracy()
  624. } else {
  625. $('#came').show()
  626. setTimeout(() => {
  627. $('#came').hide()
  628. }, 2000);
  629. }
  630. }
  631. } else if (trainingFlag == 4) {
  632. if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
  633. // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  634. soundButtonOpen()
  635. soundBgClose()
  636. trainingFlag = 1
  637. $('#target').hide()
  638. // init()
  639. }
  640. }
  641. //ctx.drawImage(backMa, 20, 20, 40, 30);
  642. if (x > widthLeft + 40 && x < widthLeft + 98 && y > 30 && y < 76) {
  643. soundButtonOpen()
  644. //点击退出按钮就返回到感统综合页面即可
  645. // window.location.href = 'http://49.232.79.112:8088/home'
  646. //http://localhost:8084/gameRecord/saveResult
  647. // window.location.href = 'http://child.jue-ming.com/home'
  648. window.location.href = pre_path + '/welcome/mainTable'
  649. }
  650. }
  651. function init() {
  652. middleFont()
  653. //第一张训练图片
  654. trainOne = new Image();
  655. trainTwo = new Image();
  656. trainThree = new Image();
  657. trainFour = new Image();
  658. onStep = new Image();
  659. underStep = new Image()
  660. startGame = new Image();
  661. backMa = new Image();
  662. button1 = new Image;
  663. //苹果
  664. apple = new Image();
  665. //苹果碎片
  666. appleLeft = new Image();
  667. appleRight = new Image();
  668. //香蕉
  669. banana = new Image();
  670. bananaLeft = new Image();
  671. bananaRight = new Image();
  672. //西瓜
  673. sandia = new Image();
  674. sandiaLeft = new Image();
  675. sandiaRight = new Image();
  676. //草莓
  677. basaha = new Image();
  678. basahaLeft = new Image();
  679. basahaRight = new Image();
  680. bg = new Image();
  681. trainOne.src = './image/fruit1.png'
  682. trainTwo.src = './image/fruit2.png'
  683. trainThree.src = './image/fruit3.png'
  684. trainFour.src = './image/fruit4.png'
  685. onStep.src = './image/onStep.png'
  686. underStep.src = './image/underStep.png'
  687. startGame.src = './image/startGame.png'
  688. backMa.src = './image/backMa.png'
  689. button1.src = './image/button1.png'
  690. apple.src = "./image/apple.png";
  691. appleLeft.src = './image/apple-1.png';
  692. appleRight.src = './image/apple-2.png';
  693. banana.src = './image/banana.png';
  694. bananaLeft.src = './image/banana-1.png';
  695. bananaRight.src = './image/banana-2.png';
  696. sandia.src = './image/sandia.png'
  697. sandiaLeft.src = './image/sandia-2.png'
  698. sandiaRight.src = './image/sandia-1.png'
  699. basaha.src = './image/basaha.png'
  700. basahaLeft.src = './image/basaha-2.png'
  701. basahaRight.src = './image/basaha-1.png'
  702. bg.src = './image/background.png'
  703. //canvas =
  704. //let canvas = document.querySelector("#solar");
  705. apple.onload = function () {
  706. //设置初始时间
  707. countdown()
  708. draw()
  709. }
  710. // bg.onload = imgFn;
  711. }
  712. //初始化虫子的变量
  713. //虫子1
  714. let wormOne;
  715. //虫子二
  716. let wormTwo;
  717. //虫子3
  718. let wormThree;
  719. //虫子4
  720. let wormFour;
  721. let bgtree;
  722. let timerWorm;
  723. //水果下降的速度
  724. let yWorm = [600, 600, 600, 600];
  725. let speedWorm = [1, 1.5, 1.3, 1.4];
  726. let xWorm = [100, 300, 500, 700]
  727. //设置虫子的起点
  728. let wormStart = 600
  729. //设置说过的结尾
  730. let wormEnd = 20;
  731. let tempCanvas;
  732. let tempCtx;
  733. let pattern
  734. //初始化抓虫子的图
  735. //声音的图片
  736. let soundImgClose;
  737. let soundImgOpen;
  738. //成功图片
  739. let success;
  740. function initWorm() {
  741. //第一张训练图片
  742. trainOne = new Image();
  743. trainTwo = new Image();
  744. trainThree = new Image();
  745. trainFour = new Image();
  746. onStep = new Image();
  747. underStep = new Image()
  748. startGame = new Image();
  749. backMa = new Image();
  750. soundImgClose = new Image();
  751. soundImgOpen = new Image();
  752. success = new Image()
  753. wormOne = new Image();
  754. wormTwo = new Image();
  755. wormThree = new Image();
  756. wormFour = new Image();
  757. bgtree = new Image();
  758. wormOne.src = './image/bug1.png';
  759. wormTwo.src = './image/bug2.png';
  760. wormThree.src = './image/bug3.png';
  761. wormFour.src = './image/bug4.png';
  762. bgtree.src = './image/tree.png'
  763. wormFlag = [true, true, true, true]
  764. trainOne.src = './image/worm1.png'
  765. trainTwo.src = './image/worm2.png'
  766. trainThree.src = './image/worm3.png'
  767. trainFour.src = './image/worm4.png'
  768. success.src = './image/success.png'
  769. onStep.src = './image/onStep.png'
  770. underStep.src = './image/underStep.png'
  771. startGame.src = './image/startGame.png'
  772. backMa.src = './image/backMa.png'
  773. soundImgClose.src = './image/close.png'
  774. soundImgOpen.src = './image/open.png'
  775. bgtree.onload = function () {
  776. countdown()
  777. //先把加载好的图片绘制在一个临时的画布上
  778. drawWorm()
  779. }
  780. }
  781. //画虫子
  782. function drawWorm() {
  783. widthLeft = (canvas.width - (1270 / 714 * canvas.height)) / 2
  784. if (trainingFlag == 1) {
  785. tempCanvasFruit = document.createElement('canvas');
  786. tempCanvasFruit.width = canvas.width;
  787. tempCanvasFruit.height = canvas.height;
  788. tempCtxFruit = tempCanvasFruit.getContext('2d');
  789. tempCtxFruit.drawImage(trainOne, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  790. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  791. ctx.fillStyle = patternFruit;
  792. ctx.fillRect(0, 0, canvas.width, canvas.height);
  793. //增加下一步的按钮。点击后将trainningFlag设置2
  794. ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  795. //返回按钮
  796. //ctx.drawImage(backMa, 20, 20, 40, 30);
  797. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  798. }
  799. if (trainingFlag == 2) {
  800. tempCanvasFruit = document.createElement('canvas');
  801. tempCanvasFruit.width = canvas.width;
  802. tempCanvasFruit.height = canvas.height;
  803. tempCtxFruit = tempCanvasFruit.getContext('2d');
  804. tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  805. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  806. ctx.fillStyle = patternFruit;
  807. ctx.fillRect(0, 0, canvas.width, canvas.height);
  808. ctx.drawImage(trainThree, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
  809. // ctx.drawImage(trainThree, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
  810. //上一步按钮
  811. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
  812. //下一步按钮
  813. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
  814. //返回按钮
  815. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  816. if (soundFlag) {
  817. ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
  818. // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30);
  819. } else {
  820. //ctx.drawImage(soundImgClose, canvas.width - 60, 20, 40, 30);
  821. ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
  822. }
  823. }
  824. if (trainingFlag == 3) {
  825. tempCanvasFruit = document.createElement('canvas');
  826. tempCanvasFruit.width = canvas.width;
  827. tempCanvasFruit.height = canvas.height;
  828. tempCtxFruit = tempCanvasFruit.getContext('2d');
  829. tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
  830. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  831. ctx.fillStyle = patternFruit;
  832. ctx.fillRect(0, 0, canvas.width, canvas.height);
  833. ctx.drawImage(trainFour, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
  834. //ctx.drawImage(trainFour, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
  835. //上一步按钮
  836. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
  837. //下一步按钮countdownFlag
  838. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
  839. //返回按钮
  840. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  841. // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30)
  842. if (soundFlag) {
  843. ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
  844. // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30);
  845. } else {
  846. //ctx.drawImage(soundImgClose, canvas.width - 60, 20, 40, 30);
  847. ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
  848. }
  849. }
  850. if (trainingFlag == 4) {
  851. ctx.fillRect(0, 0, canvas.width, canvas.height)
  852. tempCanvas = document.createElement('canvas');
  853. tempCanvas.width = canvas.width;
  854. tempCanvas.height = canvas.height;
  855. tempCtx = tempCanvas.getContext('2d');
  856. tempCtx.drawImage(bgtree, 0, 0, canvas.width, canvas.height);
  857. pattern = ctx.createPattern(tempCanvas, 'repeat');
  858. ctx.fillStyle = pattern;
  859. ctx.fillRect(0, 0, canvas.width, canvas.height);
  860. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  861. var gradient = ctx.createLinearGradient(0, 0, 100, 0);
  862. if (countdownFlag) {
  863. gradient.addColorStop("1.0", "#D1BBFF");
  864. // Fill with gradient
  865. ctx.strokeStyle = gradient;
  866. ctx.font = "30px Arial";
  867. ctx.strokeText("倒计时:" + countdown(), canvas.width - 240, 100);
  868. gradient.addColorStop("1.0", "#D1BBFF");
  869. // Fill with gradient
  870. ctx.strokeStyle = gradient;
  871. ctx.font = "30px Arial";
  872. ctx.strokeText("第" + diff + '关', 100, 100);
  873. ctx.font = "30px Arial";
  874. ctx.strokeText("得分:" + count, canvas.width - 240, 150);
  875. ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
  876. // ctx.fillText("Hello World", 10, 50);
  877. //切水果倒计时三分钟
  878. //初始化
  879. }
  880. if (diffFlag) {
  881. gradient.addColorStop("1.0", "#D1BBFF");
  882. // Fill with gradient
  883. ctx.strokeStyle = gradient;
  884. ctx.font = "80px Arial";
  885. ctx.strokeText("第 " + diff + ' 关', canvas.width / 2 - 120, canvas.height / 2 - 100);
  886. }
  887. if (passFlag == 2) {
  888. gradient.addColorStop("1.0", "red");
  889. // Fill with gradient
  890. ctx.strokeStyle = gradient;
  891. ctx.font = "20px Arial";
  892. ctx.strokeText("很遗憾您已超时,继续加油!", canvas.width / 2 - 100, canvas.height / 2 - 100);
  893. countdownFlag = false;
  894. diffFlag = false;
  895. //ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  896. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
  897. //重新开始按钮 //或返回主页
  898. // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  899. }
  900. if (passFlag == 3) {
  901. gradient.addColorStop("1.0", "#D1BBFF");
  902. // Fill with gradient
  903. ctx.strokeStyle = gradient;
  904. // ctx.font = "50px Arial";
  905. // ctx.strokeText("您已通关!!!", canvas.width / 2 - 120, canvas.height / 2 - 100);success
  906. ctx.drawImage(success, canvas.width / 2 - 325, 10);
  907. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
  908. // ctx.drawImage(success, canvas.width / 2 - 120, canvas.height / 2 - 100);
  909. // ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  910. countdownFlag = false;
  911. diffFlag = false;
  912. //重新开始按钮 //或返回主页
  913. // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  914. }
  915. // ctx.font = "40px Arial";
  916. // ctx.strokeText("Hello World", 10, 50);
  917. // ctx.fillText("Hello World", 10, 50);
  918. //虫子1
  919. ctx.drawImage(wormOne, xWorm[0], yWorm[0], 60, 60);
  920. ctx.drawImage(wormTwo, xWorm[1], yWorm[1], 60, 60);
  921. //虫子三
  922. ctx.drawImage(wormThree, xWorm[2], yWorm[2], 60, 60);
  923. //虫子4
  924. ctx.drawImage(wormFour, xWorm[3], yWorm[3], 60, 60);
  925. let random = Math.random() + 1
  926. if (wormFlag[0]) {
  927. yWorm[0] = yWorm[0] - speedWorm[0] * random
  928. } else {
  929. yWorm[0] = yWorm[0] + speedWorm[0] * random * 1.5
  930. }
  931. if (yWorm[0] < wormEnd || yWorm[0] > wormStart) {
  932. yWorm[0] = wormStart
  933. wormFlag[0] = true
  934. random = Math.random() + 1
  935. }
  936. if (wormFlag[1]) {
  937. yWorm[1] = yWorm[1] - speedWorm[1] * random
  938. } else {
  939. yWorm[1] = yWorm[1] + speedWorm[1] * random * 1.5
  940. }
  941. //yWorm[1] = yWorm[1] - speedWorm[1] * random
  942. if (yWorm[1] < wormEnd || yWorm[1] > wormStart) {
  943. yWorm[1] = wormStart
  944. wormFlag[1] = true;
  945. random = Math.random() + 1
  946. }
  947. if (wormFlag[2]) {
  948. yWorm[2] = yWorm[2] - speedWorm[2] * random
  949. } else {
  950. yWorm[2] = yWorm[2] + speedWorm[2] * random * 1.5
  951. }
  952. //yWorm[2] = yWorm[2] - speedWorm[2] * random
  953. if (yWorm[2] < wormEnd || yWorm[2] > wormStart) {
  954. yWorm[2] = wormStart
  955. wormFlag[2] = true;
  956. random = Math.random() + 1
  957. }
  958. if (wormFlag[3]) {
  959. yWorm[3] = yWorm[3] - speedWorm[3] * random
  960. } else {
  961. yWorm[3] = yWorm[3] + speedWorm[3] * random * 1.5
  962. }
  963. if (yWorm[3] < wormEnd || yWorm[3] > wormStart) {
  964. yWorm[3] = wormStart
  965. wormFlag[3] = true;
  966. random = Math.random() + 1
  967. }
  968. let first = [xWorm[0] + 30, yWorm[0] + 30]
  969. //第二列中心点坐标
  970. let second = [xWorm[1] + 30, yWorm[1] + 30]
  971. let third = [xWorm[2] + 30, yWorm[2] + 30]
  972. let four = [xWorm[3] + 30, yWorm[3] + 30]
  973. collision(first, second, third, four)
  974. }
  975. timerWorm = requestAnimationFrame(drawWorm);
  976. if (fruitFlag !== 2) {
  977. window.cancelAnimationFrame(timerWorm)
  978. ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
  979. }
  980. }
  981. function soundClose() {
  982. player.pause()
  983. }
  984. function soundOpen() {
  985. mp3Url = soundSrc;
  986. player = new Audio(mp3Url);
  987. player.play()
  988. }
  989. //按钮点击音效
  990. function soundButtonOpen() {
  991. playerButton.play()
  992. }
  993. //捉虫子音效
  994. function soundHitOpen() {
  995. playerHit.play()
  996. }
  997. //背景音乐音效
  998. function soundBgOpen() {
  999. playerBg.play()
  1000. playerBg.loop = true;
  1001. }
  1002. function soundBgClose() {
  1003. playerBg.pause()
  1004. }
  1005. function getQueryString(name) {
  1006. const url_string = window.location.href; // window.location.href
  1007. const url = new URL(url_string);
  1008. return url.searchParams.get(name);
  1009. }
  1010. function draw() {
  1011. ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
  1012. // let a = ctx.createPattern(bg, 'repeat')
  1013. // ctx.fillStyle = a;
  1014. // ctx.fillRect(0, 0, canvas.width, canvas.height)
  1015. /*绘制 太阳*/
  1016. tempCanvasFruit = document.createElement('canvas');
  1017. tempCanvasFruit.width = canvas.width;
  1018. tempCanvasFruit.height = canvas.height;
  1019. tempCtxFruit = tempCanvasFruit.getContext('2d');
  1020. tempCtxFruit.drawImage(bg, 0, 0, canvas.width, canvas.height);
  1021. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  1022. ctx.fillStyle = patternFruit;
  1023. ctx.fillRect(0, 0, canvas.width, canvas.height);
  1024. if (trainingFlag == 1) {
  1025. tempCanvasFruit = document.createElement('canvas');
  1026. tempCanvasFruit.width = canvas.width;
  1027. tempCanvasFruit.height = canvas.height;
  1028. tempCtxFruit = tempCanvasFruit.getContext('2d');
  1029. tempCtxFruit.drawImage(trainOne, 0, 0, canvas.width, canvas.height);
  1030. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  1031. ctx.fillStyle = patternFruit;
  1032. ctx.fillRect(0, 0, canvas.width, canvas.height);
  1033. //增加下一步的按钮。点击后将trainningFlag设置2
  1034. ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  1035. //返回按钮
  1036. ctx.drawImage(backMa, 20, 20, 40, 30);
  1037. }
  1038. if (trainingFlag == 2) {
  1039. tempCanvasFruit = document.createElement('canvas');
  1040. tempCanvasFruit.width = canvas.width;
  1041. tempCanvasFruit.height = canvas.height;
  1042. tempCtxFruit = tempCanvasFruit.getContext('2d');
  1043. tempCtxFruit.drawImage(trainTwo, 0, 0, canvas.width, canvas.height);
  1044. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  1045. ctx.fillStyle = patternFruit;
  1046. ctx.fillRect(0, 0, canvas.width, canvas.height);
  1047. ctx.drawImage(trainThree, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
  1048. //上一步按钮
  1049. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  1050. //下一步按钮
  1051. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
  1052. //返回按钮
  1053. ctx.drawImage(backMa, 20, 20, 40, 30);
  1054. }
  1055. if (trainingFlag == 3) {
  1056. tempCanvasFruit = document.createElement('canvas');
  1057. tempCanvasFruit.width = canvas.width;
  1058. tempCanvasFruit.height = canvas.height;
  1059. tempCtxFruit = tempCanvasFruit.getContext('2d');
  1060. tempCtxFruit.drawImage(trainTwo, 0, 0, canvas.width, canvas.height);
  1061. patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
  1062. ctx.fillStyle = patternFruit;
  1063. ctx.fillRect(0, 0, canvas.width, canvas.height);
  1064. ctx.drawImage(trainFour, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
  1065. //上一步按钮
  1066. ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
  1067. //下一步按钮
  1068. ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
  1069. //返回按钮
  1070. ctx.drawImage(backMa, 20, 20, 40, 30);
  1071. }
  1072. if (trainingFlag == 4) {
  1073. var gradient = ctx.createLinearGradient(0, 0, 100, 0);
  1074. //返回按钮
  1075. ctx.drawImage(backMa, 20, 20, 40, 30);
  1076. if (countdownFlag) {
  1077. gradient.addColorStop("1.0", "#D1BBFF");
  1078. // Fill with gradient
  1079. ctx.strokeStyle = gradient;
  1080. ctx.font = "30px Arial";
  1081. ctx.strokeText("倒计时:" + countdown(), canvas.width - 240, 100);
  1082. gradient.addColorStop("1.0", "#D1BBFF");
  1083. // Fill with gradient
  1084. ctx.strokeStyle = gradient;
  1085. ctx.font = "30px Arial";
  1086. ctx.strokeText("第" + diff + '关', 100, 100);
  1087. ctx.font = "30px Arial";
  1088. ctx.strokeText("得分:" + count, canvas.width - 240, 150);
  1089. // ctx.fillText("Hello World", 10, 50);
  1090. //切水果倒计时三分钟
  1091. //初始化
  1092. }
  1093. if (diffFlag) {
  1094. gradient.addColorStop("1.0", "yellow");
  1095. // Fill with gradient
  1096. ctx.strokeStyle = gradient;
  1097. ctx.font = "80px Arial";
  1098. ctx.strokeText("第 " + diff + ' 关', canvas.width / 2 - 120, canvas.height / 2 - 100);
  1099. }
  1100. if (passFlag == 2) {
  1101. gradient.addColorStop("1.0", "red");
  1102. // Fill with gradient
  1103. ctx.strokeStyle = gradient;
  1104. ctx.font = "20px Arial";
  1105. ctx.strokeText("很遗憾您已超时,继续加油!", canvas.width / 2 - 100, canvas.height / 2 - 100);
  1106. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  1107. countdownFlag = false;
  1108. diffFlag = false;
  1109. //重新开始按钮 //或返回主页
  1110. //ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  1111. }
  1112. if (passFlag == 3) {
  1113. gradient.addColorStop("1.0", "yellow");
  1114. // Fill with gradient
  1115. ctx.strokeStyle = gradient;
  1116. ctx.font = "20px Arial";
  1117. ctx.strokeText("您已通关!!!", canvas.width / 2 - 100, canvas.height / 2 - 100);
  1118. ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
  1119. countdownFlag = false;
  1120. diffFlag = false;
  1121. //重新开始按钮 //或返回主页
  1122. // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
  1123. }
  1124. // ctx.drawImage(button1, canvas.width / 2 - 100, canvas.height / 2 + 100, 100, 40);
  1125. // ctx.drawImage(button1, canvas.width / 2 + 100, canvas.height / 2 + 100, 100, 40);
  1126. if (flag[0]) {
  1127. ctx.drawImage(apple, x[0], y[0], 60, 60);
  1128. getColl()
  1129. } else {
  1130. apple_x_left += 0.5
  1131. apple_x_right -= 0.5
  1132. ctx.drawImage(appleLeft, apple_x_right, y[0], 60, 60);
  1133. ctx.drawImage(appleRight, apple_x_left, y[0], 60, 60);
  1134. }
  1135. if (flag[1]) {
  1136. ctx.drawImage(banana, x[1], y[1], 60, 30);
  1137. getColl()
  1138. } else {
  1139. banana_x_left += 0.5
  1140. banana_x_right -= 0.5
  1141. ctx.drawImage(bananaLeft, banana_x_left, y[1], 60, 30);
  1142. ctx.drawImage(bananaRight, banana_x_right, y[1], 60, 30);
  1143. }
  1144. if (flag[2]) {
  1145. ctx.drawImage(sandia, x[2], y[2], 60, 60);
  1146. getColl()
  1147. } else {
  1148. sandia_x_left += 0.5
  1149. sandia_x_right -= 0.5
  1150. ctx.drawImage(sandiaLeft, sandia_x_left, y[2], 60, 60);
  1151. ctx.drawImage(sandiaRight, sandia_x_right, y[2], 60, 60);
  1152. }
  1153. if (flag[3]) {
  1154. ctx.drawImage(basaha, x[3], y[3], 60, 60);
  1155. getColl()
  1156. } else {
  1157. basaha_x_left += 0.5
  1158. basaha_x_right -= 0.5
  1159. ctx.drawImage(basahaLeft, basaha_x_left, y[3], 60, 60);
  1160. ctx.drawImage(basahaRight, basaha_x_right, y[3], 60, 60);
  1161. }
  1162. random = Math.random() + 1
  1163. y[0] = y[0] + speed[0] * random
  1164. if (y[0] > fruitEnd) {
  1165. y[0] = fruitStart
  1166. flag[0] = true
  1167. apple_x_left = x[0]
  1168. apple_x_right = x[0]
  1169. }
  1170. random = Math.random() + 1
  1171. y[1] = y[1] + speed[1] * random
  1172. if (y[1] > fruitEnd) {
  1173. y[1] = fruitStart
  1174. flag[1] = true;
  1175. banana_x_left = x[1];
  1176. banana_x_right = x[1]
  1177. }
  1178. random = Math.random() + 1
  1179. y[2] = y[2] + speed[2] * random
  1180. if (y[2] > fruitEnd) {
  1181. y[2] = fruitStart;
  1182. flag[2] = true;
  1183. sandia_x_left = x[2];
  1184. sandia_x_right = x[2]
  1185. }
  1186. random = Math.random() + 1
  1187. y[3] = y[3] + speed[3] * random
  1188. if (y[3] > fruitEnd) {
  1189. y[3] = fruitStart
  1190. flag[3] = true;
  1191. basaha_x_left = x[3]
  1192. basaha_x_right = x[3]
  1193. }
  1194. }
  1195. timer = requestAnimationFrame(draw);
  1196. if (fruitFlag !== 1) {
  1197. //取消循环
  1198. window.cancelAnimationFrame(timer)
  1199. ctx.clearRect(0, 0, 900, 900); //清空所有的内容
  1200. }
  1201. //
  1202. }
  1203. //在合适的时机调用碰撞检测方法
  1204. function getColl() {
  1205. let first = [x[0] + 30, y[0] + 30]
  1206. //第二列中心点坐标
  1207. let second = [x[1] + 30, y[1] + 15]
  1208. let third = [x[2] + 30, y[2] + 30]
  1209. let four = [x[3] + 30, y[3] + 30]
  1210. //碰撞检测方法
  1211. collision(first, second, third, four)
  1212. }
  1213. //分数判断方法
  1214. function countComputer() {
  1215. if (diff == 1) {
  1216. if (passFlag == 1) {
  1217. count += 5
  1218. }
  1219. }
  1220. if (diff == 2) {
  1221. // resetTime()
  1222. if (passFlag == 1) {
  1223. count += 5
  1224. }
  1225. }
  1226. if (diff == 3) {
  1227. // resetTime()
  1228. if (passFlag == 1) {
  1229. count += 5
  1230. }
  1231. }
  1232. }
  1233. //水果的相交方法
  1234. function collision(first, second, third, four) {
  1235. //判断是否与第一个相交
  1236. if (wormFlag[0]) {
  1237. if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
  1238. //两个面积相交了\
  1239. soundHitOpen()
  1240. wormFlag[0] = false
  1241. countComputer()
  1242. }
  1243. }
  1244. //第二条线相交
  1245. if (wormFlag[1]) {
  1246. if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 60 / 2))) {
  1247. //两个面积相交了
  1248. soundHitOpen()
  1249. wormFlag[1] = false
  1250. countComputer()
  1251. }
  1252. }
  1253. //第三条线相交
  1254. if (wormFlag[2]) {
  1255. if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
  1256. //两个面积相交了
  1257. soundHitOpen()
  1258. wormFlag[2] = false
  1259. countComputer()
  1260. }
  1261. }
  1262. //第四条线相交
  1263. //第三条线相交
  1264. if (wormFlag[3]) {
  1265. if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
  1266. //两个面积相交了
  1267. soundHitOpen()
  1268. wormFlag[3] = false
  1269. countComputer()
  1270. }
  1271. }
  1272. }
  1273. //虫子的相交方法
  1274. function wormCollision(first, second, third, four) {
  1275. //判断是否与第一个相交console
  1276. if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
  1277. //两个面积相交了
  1278. wormFlag[0] = false
  1279. }
  1280. //第二条线相交
  1281. if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 60 / 2))) {
  1282. //两个面积相交了
  1283. wormFlag[1] = false
  1284. }
  1285. //第三条线相交
  1286. if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
  1287. //两个面积相交了
  1288. wormFlag[2] = false
  1289. }
  1290. //第四条线相交
  1291. //第三条线相交
  1292. if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
  1293. //两个面积相交了
  1294. wormFlag[3] = false
  1295. }
  1296. }
  1297. //倒计时方法
  1298. function countdown() {
  1299. startTimeFruit = new Date().getTime();
  1300. let surplusTime = endTimeFruit - startTimeFruit;
  1301. let th = Math.floor(surplusTime / (1000 * 60 * 60) % 24);
  1302. let tm = Math.floor(surplusTime / (1000 * 60) % 60);
  1303. let ts = Math.floor(surplusTime / 1000 % 60);
  1304. if (ts < 10) {
  1305. ts = '0' + ts
  1306. }
  1307. if (surplusTime >= 0) {
  1308. if (!(passFlag == 2 || passFlag == 3)) {
  1309. countdownFlag = true
  1310. }
  1311. return '0' + th + ':' + '0' + tm + ':' + ts
  1312. } else {
  1313. if (diff == 1) {
  1314. if (count >= 90) {
  1315. //执行保存结果
  1316. levelFun(diff, count)
  1317. totalScore += count;
  1318. middleFont()
  1319. speedWorm = [1.5, 2.0, 2.3, 2.4];
  1320. //且进行将关卡设置为2
  1321. diff = 2;
  1322. count = 0
  1323. //将时间重置为3分钟
  1324. resetTime()
  1325. } else {
  1326. if (passFlag != 2) {
  1327. levelFun(diff, count)
  1328. }
  1329. passFlag = 2
  1330. //很遗憾失败了
  1331. }
  1332. } else if (diff == 2) {
  1333. if (count >= 60) {
  1334. middleFont()
  1335. levelFun(diff, count)
  1336. totalScore += count;
  1337. speedWorm = [2.0, 2.5, 2.8, 3.1];
  1338. diff = 3;
  1339. count = 0
  1340. //将时间重置为3分钟
  1341. resetTime()
  1342. } else {
  1343. if (passFlag != 2) {
  1344. levelFun(diff, count)
  1345. }
  1346. passFlag = 2
  1347. }
  1348. } else if (diff == 3) {
  1349. if (count >= 45) {
  1350. if (passFlag != 3) {
  1351. totalScore += count;
  1352. levelFun(diff, count)
  1353. saveResult(totalScore)
  1354. }
  1355. passFlag = 3
  1356. } else {
  1357. if (passFlag != 2) {
  1358. levelFun(diff, count)
  1359. }
  1360. passFlag = 2
  1361. }
  1362. }
  1363. return '00' + ':' + '00' + ':' + '00'
  1364. }
  1365. }
  1366. //捉虫子游戏变量
  1367. $('#solar').hide();
  1368. $('#exit').hide();
  1369. $('#refresh').hide();
  1370. let WASM_URL = "js/gazefilter.wasm";
  1371. var buttonList = []
  1372. $('#target').hide();
  1373. $('#target1').hide();
  1374. var record = {
  1375. beforePoint: -1,
  1376. currentPoint: 0,
  1377. }
  1378. //校准方法
  1379. function calibrationAccuracy() {
  1380. $('#solar').hide();
  1381. handleFullScreen();
  1382. getFun()
  1383. $('#target1').show();
  1384. $('#startCalibration').hide();
  1385. switchTime()
  1386. //初始化游戏
  1387. $('#target1').hide()
  1388. // $('#target').show()
  1389. //clearInterval(time)
  1390. //$('#solar').show();
  1391. //$('#exit').show();
  1392. //$('#refresh').show();
  1393. }
  1394. $("#startCalibration").click(function () {
  1395. // handleFullScreen(); //全屏
  1396. handleFullScreen();
  1397. getFun()
  1398. $('#target1').show();
  1399. $('#startCalibration').hide();
  1400. //switchTime()
  1401. //初始化游戏
  1402. //
  1403. $('#target1').hide()
  1404. $('#target').show()
  1405. clearInterval(time)
  1406. $('#solar').show();
  1407. //$('#exit').show();
  1408. // $('#refresh').show();
  1409. //init();
  1410. // $('.enterClass').css('display','none');
  1411. // $('.returnClass').css('display','block');
  1412. });
  1413. //切换游戏
  1414. $('#exit').click(function () {
  1415. //隐藏画布一
  1416. // $('#exit').innerHtml()
  1417. if (fruitFlag == 1) {
  1418. setTimeout(() => {
  1419. initWorm()
  1420. fruitFlag = 2
  1421. }, 30);
  1422. } else {
  1423. setTimeout(() => {
  1424. // init()
  1425. fruitFlag = 1
  1426. }, 30);
  1427. }
  1428. })
  1429. $('#refresh').click(function () {
  1430. location.reload()
  1431. })
  1432. function handleFullScreen() {
  1433. var de = document.documentElement;
  1434. if (de.requestFullscreen) {
  1435. de.requestFullscreen();
  1436. } else if (de.msRequestFullscreen) {
  1437. de.msRequestFullscreen();
  1438. } else if (de.webkitRequestFullScreen) {
  1439. de.webkitRequestFullScreen();
  1440. } else if (de.mozRequestFullScreen) {
  1441. de.mozRequestFullScreen();
  1442. } else {
  1443. wtx.info("当前浏览器不支持全屏!");
  1444. }
  1445. }
  1446. //重置通过状态
  1447. function resetStatus() {
  1448. //重置是否通过的状态
  1449. passFlag = 1;
  1450. //重置速度
  1451. speedWorm = [1, 1.5, 1.3, 1.4];
  1452. }
  1453. //重置计时函数
  1454. function resetTime() {
  1455. startTimeFruit = new Date().getTime();
  1456. endTimeFruit = startTimeFruit + 180000;
  1457. countdownFlag = true
  1458. }
  1459. //重置难度重置关数
  1460. function resetCount(val, value) {
  1461. //设置难度为多少
  1462. diff = val;
  1463. //设置分数为多少
  1464. count = value;
  1465. }
  1466. //轮询当前的图表
  1467. var time = null;
  1468. function switchTime() {
  1469. let a = 0
  1470. time = setInterval(() => {
  1471. $('#target1').show()
  1472. $('#targetMini').show()
  1473. if (a == 16) {
  1474. $('#target1').hide()
  1475. $('#target').show()
  1476. clearInterval(time)
  1477. $('#solar').show();
  1478. //$('#exit').show();
  1479. // $('#refresh').show();
  1480. trainingFlag = 4
  1481. //重置计时函数
  1482. resetTime()
  1483. //重置分数和参数
  1484. resetCount(1, 0)
  1485. //都重置到初始状态
  1486. resetStatus();
  1487. } else {
  1488. $('#targetMini1').css('transition', '0s');
  1489. $('#targetMini1').css('width', 54 + 'px');
  1490. $('#targetMini1').css('height', 54 + 'px');
  1491. let x = buttonList[a].x1;
  1492. x = Math.floor(x)
  1493. let y = buttonList[a].y1;
  1494. y = Math.floor(y)
  1495. $('#target1').css('left', x + 'px');
  1496. $('#target1').css('top', y + 'px');
  1497. // init();
  1498. setTimeout(() => {
  1499. $('#targetMini1').css('transition', '1s');
  1500. $('#targetMini1').css('width', 20 + 'px');
  1501. $('#targetMini1').css('height', 20 + 'px');
  1502. }, 1000);
  1503. setTimeout(() => {
  1504. coordinates(x + 30, y + 30)
  1505. }, 1500);
  1506. setTimeout(() => {
  1507. coordinates(x + 30, y + 30)
  1508. }, 1840);
  1509. setTimeout(() => {
  1510. coordinates(x + 30, y + 30)
  1511. }, 1940);
  1512. a = a + 1
  1513. }
  1514. }, 2000)
  1515. }
  1516. function coordinates(x, y) {
  1517. gazefilter.tracker.calibrate( //校正,发送当前点击位置进行校正
  1518. //event.timeStamp,
  1519. window.performance.now(),
  1520. //window.performance.now(),
  1521. //mill,
  1522. //1000, // relative to performance.timeOrigin
  1523. x, // in pixels
  1524. y, // in pixels
  1525. 1.0 // see note below
  1526. );
  1527. }
  1528. //获取各个按钮的left和top
  1529. /**
  1530. * 在屏幕上画出注视点
  1531. * 是否画点,以及画点方式均可自定义
  1532. **/
  1533. function drawTargetOnScreen(position) {
  1534. if (position != null && position.length == 2) {
  1535. //判断是否是NAN如果是NAN则不执行
  1536. //如果不是则需要判断当前点是否在第一个点中,如果在则执行settimeout函数1秒后改变改块状态
  1537. //如果在第一个点中但第二次坐标出来的时候还在第一个坐标里那就没问题
  1538. // $('#target').css('display', 'block');
  1539. targ[0] = position[0] + 60;
  1540. targ[1] = position[1] + 60;
  1541. $('#target').css('left', position[0] + 'px');
  1542. $('#target').css('top', position[1] + 'px');
  1543. // $('#target').css('z-index', 100);
  1544. //judge(position[0], position[1])
  1545. // judge(position[0], position[1])
  1546. }
  1547. }
  1548. //判断是否在第一个点中
  1549. //获取全部的校验点
  1550. function getFun() {
  1551. for (let i = 0; i < 16; i++) {
  1552. let a = document.getElementById('num' + (i + 1));
  1553. buttonList.push({
  1554. x1: a.getBoundingClientRect().left,
  1555. y1: a.getBoundingClientRect().top
  1556. })
  1557. }
  1558. }
  1559. async function initGazefilter() {
  1560. cameraFlag = false
  1561. await gazefilter.init(WASM_URL); //初始化
  1562. await gazefilter.tracker.connect(); //连接摄像头
  1563. let canvas = document.getElementById("tracker-canvas");
  1564. await gazefilter.visualizer.setCanvas(canvas); //绘制图像
  1565. cameraFlag = true
  1566. await gazefilter.tracker.addListener("calib", response => {
  1567. //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.
  1568. });
  1569. }
  1570. function startTrace() {
  1571. gazefilter.tracker.addListener("filter", event => { //注视点
  1572. drawTargetOnScreen(event.bestGazePoint()); //Best gaze point of left or right eye. 其他取点可参考https://beehiveor.gitlab.io/gazefilter/data.html,有不同类型的点 fixationPoint
  1573. });
  1574. }
  1575. function hideSence() {
  1576. //隐藏目标物
  1577. $('#target').hide()
  1578. $('#target1').hide()
  1579. $('#targetMini').hide()
  1580. //隐藏摄像头
  1581. $('#solar').show()
  1582. }
  1583. $("#finishcalibration").click(function () {
  1584. window.removeEventListener("click", onmouseclick); //九点校正完成,取消鼠标事件,并开始绘制轨迹点
  1585. $("#finishcalibration").hide();
  1586. })
  1587. $(document).ready(function () {
  1588. //将canvas自适应屏幕
  1589. resizeCanvas()
  1590. //init()
  1591. initWorm()
  1592. hideSence()
  1593. //将该隐藏的隐藏然后初始化游戏场景
  1594. //初始华摄像头
  1595. initGazefilter();
  1596. startTrace();
  1597. });
  1598. </script>
  1599. </body>
  1600. </html>