index.html 51 KB

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