<!doctype html>
<html class="no-js" lang="">

<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>心理胜任能力评估系统</title>
  <meta name="description"
    content="A website that learns to predict where you are looking at. Written in TensorFlow.js">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <link rel="stylesheet" href="normalize.css">
  <link rel="stylesheet" href="style.css">
  <style>
    .buttonArr button {
      position: fixed;
      width: 10px;
      height: 1px;
      /* opacity: 0.0; */
      z-index: 2000;
      color: #000000;
      border-radius: 10px;
      border: 0px;
    }

    .buttonArr {
      position: unset !important;
      width: 1px;
      height: 0px;
      opacity: 0.0;
      z-index: 2000;
      border-radius: 10px;
      border: 0px;
    }

    #num1 {
      position: absolute;
      left: 20px;
      top: 20px;
    }

    #num2 {
      position: absolute;
      left: 33%;
      top: 20px;
    }

    #num3 {
      position: absolute;
      left: 66%;
      top: 20px;
    }

    #num4 {
      position: absolute;
      right: 80px;
      top: 20px;
    }

    #num5 {
      position: absolute;
      left: 20px;
      top: 33%;
    }

    #num6 {
      position: absolute;
      left: 33%;
      top: 33%;
    }

    #num7 {
      position: absolute;
      left: 66%;
      top: 33%;
    }

    #num8 {
      position: absolute;
      right: 80px;
      top: 33%;
    }

    #num9 {
      position: absolute;
      left: 20px;
      top: 66%;
    }

    #num10 {
      position: absolute;
      left: 33%;
      top: 66%;
    }

    #num11 {
      position: absolute;
      left: 66%;
      top: 66%;
    }

    #num12 {
      position: absolute;
      right: 80px;
      top: 66%;
    }

    #num13 {
      position: absolute;
      left: 20px;
      bottom: 0px;
    }

    #num14 {
      position: absolute;
      left: 33%;
      bottom: 0px;
    }

    #num15 {
      position: absolute;
      left: 66%;
      bottom: 0px;
    }

    #num16 {
      position: absolute;
      right: 80px;
      bottom: 0px;
    }

    #targetMini {
      background-color: #00FF00;
      position: absolute;
      border-radius: 50%;
      height: 55px;
      width: 55px;
      /* z-index: 0; */
      opacity: 0.5;
      left: 0;
      bottom: 0;
      top: 0;
      right: 0;
      margin: auto;
      z-index: 999;
      transition: 1s;
      transition-timing-function: steps(1000, start);
      border: 2.5px solid #000000;
    }

    #target {
      /* text-align: center; */
      justify-content: center;
      background-image: url('calibrate.png');
      background-size: 120px 120px;
      /* background-color: lightgreen; */
      position: fixed;
      /* position: absolute; */

      border-radius: 50%;
      height: 120px;
      width: 120px;
      left: 0px;
      top: 0px;
      transition: left 0.01s, top 0.01s;
      box-shadow: 0 0 10px 5px white;
      opacity: 0.5;
      display: inline-block;
      z-index: 999;
    }

    #targetMini1 {
      background-color: #00FF00;
      /* position: absolute; */
      position: absolute;
      border-radius: 50%;
      height: 54px;
      width: 54px;
      /* z-index: 0; */
      opacity: 0.6;
      left: 0;
      bottom: 0;
      top: 0;
      right: 0;
      margin: auto;
      z-index: 999;
      transition: 1s;
      transition-timing-function: steps(1000, start);
      border: 3px solid #000000;
    }

    #target1 {
      /* text-align: center; */
      justify-content: center;
      background-image: url('calibrate.png');
      background-size: 60px 60px;
      /* background-color: lightgreen; */
      position: relative;
      /* position: absolute; */

      border-radius: 50%;
      height: 60px;
      width: 60px;
      left: 0px;
      top: 0px;
      transition: left 1s, top 1s;
      box-shadow: 0 0 10px 5px white;
      opacity: 0.9;
      display: inline-block;
      z-index: 999;
    }

    #refresh {
      width: 60px;
      font-size: 10px;
      position: fixed;
      top: 0px;
      left: 90%;
      display: inline-block;
      z-index: 999;
      margin-left: -40px;
      top: 2%;
      background-color: rgb(127, 178, 255);

      line-height: 30px;
      color: aliceblue;
      /* border: 1px solid; */
      text-align: center;
      border-radius: 5px;
      cursor: pointer;
    }

    #exit {
      width: 60px;
      font-size: 10px;
      position: fixed;
      top: 0px;
      left: 98%;
      display: inline-block;
      z-index: 999;
      margin-left: -40px;
      top: 2%;
      background-color: rgb(127, 178, 255);

      line-height: 30px;
      color: aliceblue;
      /* border: 1px solid; */
      text-align: center;
      border-radius: 5px;
      cursor: pointer;
      /* padding: 5px; */
    }

    #startCalibration {
      position: fixed;
      top: 0px;
      left: 50%;
      display: inline-block;
      margin-left: -40px;
      top: 30%;
      color: aliceblue;
      background-color: rgb(127, 178, 255);
      width: 80px;
      line-height: 40px;
      /* border: 1px solid; */
      text-align: center;
      border-radius: 5px;
      cursor: pointer;
      padding: 5px;
    }

    #came {
      position: fixed;
      top: 5%;
      left: 50%;
      margin-left: -100px;
      color: #ffffff;
      /* font-size: 16px;
      right:0px;
      bottom:0; */
      background-color: #666666;
      padding-top: 5px;
      padding-left: 5px;
      padding-right: 5px;
      padding-bottom: 5px;
      border-radius: 5px;
      ;

      z-index: 9999
    }
  </style>
</head>

<body>
  <div id="content">
    <div class="buttonArr">
      <button id='num1'>&nbsp;</button><button id="num2">&nbsp;</button><button id="num3">&nbsp;</button><button
        id='num4'>&nbsp;</button><br>
      <button id='num5'>&nbsp;</button><button id='num6'>&nbsp;</button><button id='num7'>&nbsp;</button><button
        id='num8'>&nbsp;</button><br>
      <button id='num9'>&nbsp;</button> <button id='num10'>&nbsp;</button> <button id='num11'>&nbsp;</button> <button
        id='num12'>&nbsp;</button><br>
      <button id='num13'>&nbsp;</button> <button id='num14'>&nbsp;</button> <button id='num15'>&nbsp;</button> <button
        id='num16'>&nbsp;</button>
    </div>

    <div id="target">
      <div id="targetMini"></div>
    </div>
    <div id="target1">
      <div id="targetMini1"></div>
    </div>
    <canvas id="solar" width="900" height="auto" style="background:#000000;position: fixed; z-index:998"></canvas>
    <div id="came">摄像头初始化中稍后再试...</div>

    <!-- <div id="finishcalibration" margin-left:300px;position: fixed;top:60px
      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;">
      关闭校正</div> onclick="startCal()" -->
    <!-- <div id="startCalibration">
      开始校准</div> -->
    <div id="exit">
      切换游戏</div>
    <div id="refresh">
      重新校验</div>
    <canvas id="tracker-canvas"
      style="position: fixed;top:0px;left:50%;width:250px;height:200px;margin-left:-125px;"></canvas>
  </div>

  <!-- <script src="js/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script> -->
  <script src="js/jquery-3.3.1.min.js"></script>

  <script src="js/gazefilter.js"></script>
  <script src="js/aes.js" type="text/javascript" charset="utf-8"></script>
  <!--script src="js/calibration.js"></script-->
  <script>
    var pre_path = window.location.origin;
    $('#came').hide()
    var soundSrc = 'sound/worm1.mp3'
    var mp3Url = soundSrc;
    var soundFlag = true;
    var cameraFlag = true
    var player = new Audio(mp3Url);

    //点击按钮声音
    var soundButtonSrc = 'sound/button.mp3'
    var mp3Button = soundButtonSrc
    var playerButton = new Audio(mp3Button);

    //打虫子音效
    var soundHitSrc = 'sound/wormHit.mp3'
    var mp3Hit = soundHitSrc
    var playerHit = new Audio(mp3Hit)

    //背景音乐
    var soundBgSrc = 'sound/wormMusicBg.mp3'
    var mp3Bg = soundBgSrc
    var playerBg = new Audio(mp3Bg)

    //总分
    var totalScore = 0;

    var key = CryptoJS.enc.Utf8.parse("Sp5biS+gX+#CqAFF");//密钥
    var iv = CryptoJS.enc.Utf8.parse('ud2E8l6wchltwIDA');
    //传输参数为字符串
    function Encrypt(word) {
      let srcs = CryptoJS.enc.Utf8.parse(word);
      let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
      return encrypted.ciphertext.toString().toUpperCase();
    }
    function Decrypt(word) {
      let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
      let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
      let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
      let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
      return decryptedStr.toString();
    }


    // levelFun(2, 20)
    //
    //传输关卡和得分的字符串
    function levelFun(level, score) {
      //1----获取userId
      // let userMi = sessionStorage.getItem('B922C64EC97B4FF08485886B2AAEF40C')
      // //解密userMi
      // let user = Decrypt(userMi)
      // //需要传输的参数
      // let userId = JSON.parse(user).id
      // //2----获取唯一标识
      // let associatedFlag = new Date().getTime() + '';
      // var params = {
      //   userId: userId,
      //   associatedFlag: associatedFlag,
      //   gameFlag: 'g_ydrzxlzcz',
      //   checkPoint: level,
      //   level: level,
      //   score: score,
      //   performance: {},
      //   data: {}
      // }
      // // Encrypt(JSON.stringify(params))
      // // console.log(Encrypt(JSON.stringify(params)))
      // //给参数加密
      // let paramsMi = Encrypt(JSON.stringify(params))
      // // console.log(Decrypt(paramsMi))
      // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
      // $.ajax({
      //   url: "https://child.jue-ming.com:8060/gameLevelRecord/saveResult",      //请求接口的地址
      //   type: "POST",
      //   contentType: "json/application",
      //   dataType: 'json',                                    //请求的方法GET/POST
      //   data: JSON.stringify({ data: paramsMi }),
      //   headers: {
      //     'Authorization': "Bearer " + tokenLin,
      //   },                                     //需要传递的参数
      //   success: function (res) {
      //     res = Decrypt(res)
      //     //调用解密方法                     //请求成功后的操作
      //     //在控制台输出返回结果
      //   },
      //   error: function (err) {                       //请求失败后的操作
      //     //请求失败在控制台输出22
      //   }
      // })
    }

    function saveResult(score) {
      //1----获取userId
      let userMi = sessionStorage.getItem('b80bb7740288fda1f201890375a60c8f')
      //解密userMi
      // let user = Decrypt(userMi)
      //需要传输的参数
      let userId = userMi
      //2----获取唯一标识
      let associatedFlag = new Date().getTime() + '';
      var params = {
        userId: userId,
        // associatedFlag: associatedFlag,
        // gameFlag: 'g_ydrzxlzcz',
        score: score,
        testPlanId: getQueryString('testPlanId')
        // gameName: '眼动认知训练捉虫子'
      }
      let paramsMi = Encrypt(JSON.stringify(params))
      console.log(paramsMi)
      // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
      // let tokenLin = Decrypt(sessionStorage.getItem(Encrypt('token'))) || '';
      let paramsLin =userId+'token'


      let tokenLin = localStorage.getItem(paramsLin);
      //获取当前的前端服务器url

     //http://cognitive.wistcm.com
     //pre_path +
      $.ajax({
        // url:  "http://cognitive.wistcm.com:8090/cognize/InsecthHunting",      //请求接口的地址
        url:  "http://localhost:8099/cognize/InsecthHunting",
        type: "POST",
        contentType: "json/application",
        dataType: 'json',                                   //请求的方法GET/POST
        data: JSON.stringify({ data: paramsMi }),
        headers: {
          'Authorization': "Bearer " + tokenLin,
        },                                 //需要传递的参数
        success: function (res) {

        // window.history.back()
          res = Decrypt(res)
          // goTestResult(res.data);
          //返回到浏览器上一页
          //调用解密方法                     //请求成功后的操作
          //在控制台输出返回结果
        },
        error: function (err) {
          //请求失败后的操作
          //请求失败在控制台输出22
        }
      })
    }

    //
    //切水果游戏变量
    let apple;
    let appleLeft;
    let appleRight;
    let banana;
    let bananaLeft;
    let bananaRight;
    let sandia;
    let sandiaLeft;
    let sandiaRight;
    let basaha;
    let basahaLeft;
    let basahaRight;
    let bg;
    let ctx;


    let widthLeft;
    //退出按钮
    let button1;
    let button2;
    //等于1时代表在水果游戏页面
    //等于2时代表在抓虫子页面
    let fruitFlag = 2;


    //水果下降的速度
    let y = [20, 20, 20, 20];
    let speed = [1, 1.5, 1.3, 1.4];
    let x = [100, 300, 500, 700]
    let rondom;
    let timer;
    //设置水果的起点
    let fruitStart = 10
    //设置说过的结尾
    let fruitEnd = 900;
    //是否隐藏
    let flag = [true, true, true, true]
    //苹果左右碎片
    let apple_x_left = x[0];
    let apple_x_right = x[0];

    let banana_x_left = x[1];
    let banana_x_right = x[1]

    let sandia_x_left = x[2];
    let sandia_x_right = x[2]

    let basaha_x_left = x[3];
    let basaha_x_right = x[3];

    let targ = [0, 0]

    let tempCanvasFruit;
    let tempCtxFruit;
    let patternFruit;

    //水果倒计时初始时间
    let startTimeFruit;
    //设置倒计时结束时间
    let endTimeFruit;
    let canvas = document.getElementById('solar');
    ctx = canvas.getContext("2d");
    window.addEventListener("resize", resizeCanvas, false);

    let countdownFlag = false;
    let diff = 1;
    let count = 0
    //第几关进行提示
    let diffFlag = false;
    //通关提示
    //当passFlag默认为1为正常状态
    //当passFlag为2时为超时未通过状态
    //当passFlag为3时为已通关状态
    let passFlag = 1;

    //训练提示页flag
    let trainingFlag = 1
    //第一张训练图片
    let trainOne;
    let trainTwo;
    let trainThree;
    let trainFour;
    let onStep;
    let underStep;
    let startGame;
    let backMa;
    //canvas监听事件
    canvas.addEventListener("mouseup", clickButton);

    function resizeCanvas() {
      canvas.width = window.innerWidth;
      x = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]

      xWorm = [canvas.width / 5, canvas.width / 5 * 2, canvas.width / 5 * 3, canvas.width / 5 * 4]
      //  canvas.height = window.innerHeight;
      canvas.height = window.innerHeight;
      fruitEnd = canvas.height
      wormStart = canvas.height

      //canvas.height = '100vh';
      apple_x_left = x[0];
      apple_x_right = x[0];

      banana_x_left = x[1];
      banana_x_right = x[1]

      sandia_x_left = x[2];
      sandia_x_right = x[2]

      basaha_x_left = x[3];
      basaha_x_right = x[3];
    }
    //获取
    //  initWorm()
    //初始化切水果的图

    //第几关2秒显示
    function middleFont() {
      diffFlag = true;
      setTimeout(() => {
        diffFlag = false;
      }, 2000);
    }
    function soundChange() {
      if (soundFlag) {
        soundOpen()

      } else {
        soundClose()
      }
    }
    function clickButton(event) {
      //鼠标在当前画布的位置
      // let x1 = event.clientX;
      // let y1 = event.clientY;

      let x = event.clientX - canvas.getBoundingClientRect().left;
      let y = event.clientY - canvas.getBoundingClientRect().top;
      //判断点是否在长方形内

      //当在第一步训练时 只有一个按钮下一步的按钮
      if (trainingFlag == 1) {
        if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 100) && y < (canvas.height / 2 + +100 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          soundSrc = 'sound/worm1.mp3'
          soundClose()
          if (soundFlag) {
            soundOpen()
          }
          trainingFlag = 2
        }
      } else if (trainingFlag == 2) {  //当在第二步训练时  有上一步下一步按钮
        //当是在点击声音播放声音时
        if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          soundFlag = !soundFlag
          soundChange()

        }
        // 上一步按钮
        if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          trainingFlag = 1
          soundClose()
        }

        // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
        // 下一步按钮
        if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          trainingFlag = 3
          soundClose()
          soundSrc = 'sound/worm2.mp3'
          if (soundFlag) {
            soundOpen()
          }
        }
        // ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
      } else if (trainingFlag == 3) {

        //当是在点击声音播放声音时
        if (x > (canvas.width - widthLeft - 90) && x < (canvas.width - widthLeft - 58) && y > (30) && y < (76)) {
          soundButtonOpen()
          soundFlag = !soundFlag
          soundChange()
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          // soundOpen()
        }
        // 上一步按钮
        if (x > (canvas.width / 2 - 200) && x < (canvas.width / 2 + 40) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          soundSrc = 'sound/worm1.mp3'
          soundClose()
          if (soundFlag) {
            soundOpen()
          }

          trainingFlag = 2
        }

        // ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
        // 下一步按钮
        if (x > (canvas.width / 2 + 40) && x < (canvas.width / 2 + 200) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          // soundClose()
          // calibrationAccuracy()
          soundBgOpen()
          soundButtonOpen()
          if (cameraFlag) {
            soundClose()
            calibrationAccuracy()
          } else {
            $('#came').show()
            setTimeout(() => {
              $('#came').hide()
            }, 2000);
          }

        }

      } else if (trainingFlag == 4) {
        if (x > (canvas.width / 2 - 80) && x < (canvas.width / 2 + 80) && y > (canvas.height / 2 + 220) && y < (canvas.height / 2 + 220 + 80)) {
          // ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          soundButtonOpen()
          soundBgClose()
          trainingFlag = 1
          $('#target').hide()
          // init()
        }
      }
      //ctx.drawImage(backMa, 20, 20, 40, 30);
      if (x > widthLeft + 40 && x < widthLeft + 98 && y > 30 && y < 76) {
        soundButtonOpen()
        //点击退出按钮就返回到感统综合页面即可
        // window.location.href = 'http://49.232.79.112:8088/home'
        //http://localhost:8084/gameRecord/saveResult
        // window.location.href = 'http://child.jue-ming.com/home'
        window.location.href = pre_path + '/welcome/mainTable'

      }
    }
    function init() {

      middleFont()
      //第一张训练图片
      trainOne = new Image();
      trainTwo = new Image();
      trainThree = new Image();
      trainFour = new Image();
      onStep = new Image();
      underStep = new Image()
      startGame = new Image();
      backMa = new Image();



      button1 = new Image;
      //苹果
      apple = new Image();
      //苹果碎片
      appleLeft = new Image();
      appleRight = new Image();
      //香蕉
      banana = new Image();
      bananaLeft = new Image();
      bananaRight = new Image();
      //西瓜
      sandia = new Image();
      sandiaLeft = new Image();
      sandiaRight = new Image();
      //草莓
      basaha = new Image();
      basahaLeft = new Image();
      basahaRight = new Image();

      bg = new Image();
      trainOne.src = './image/fruit1.png'
      trainTwo.src = './image/fruit2.png'
      trainThree.src = './image/fruit3.png'
      trainFour.src = './image/fruit4.png'
      onStep.src = './image/onStep.png'
      underStep.src = './image/underStep.png'
      startGame.src = './image/startGame.png'
      backMa.src = './image/backMa.png'



      button1.src = './image/button1.png'
      apple.src = "./image/apple.png";
      appleLeft.src = './image/apple-1.png';
      appleRight.src = './image/apple-2.png';
      banana.src = './image/banana.png';
      bananaLeft.src = './image/banana-1.png';
      bananaRight.src = './image/banana-2.png';
      sandia.src = './image/sandia.png'
      sandiaLeft.src = './image/sandia-2.png'
      sandiaRight.src = './image/sandia-1.png'
      basaha.src = './image/basaha.png'
      basahaLeft.src = './image/basaha-2.png'
      basahaRight.src = './image/basaha-1.png'
      bg.src = './image/background.png'

      //canvas =
      //let canvas = document.querySelector("#solar");
      apple.onload = function () {
        //设置初始时间
        countdown()
        draw()
      }
      // bg.onload = imgFn;
    }
    //初始化虫子的变量
    //虫子1
    let wormOne;
    //虫子二
    let wormTwo;
    //虫子3
    let wormThree;
    //虫子4
    let wormFour;
    let bgtree;
    let timerWorm;
    //水果下降的速度
    let yWorm = [600, 600, 600, 600];
    let speedWorm = [1, 1.5, 1.3, 1.4];
    let xWorm = [100, 300, 500, 700]

    //设置虫子的起点
    let wormStart = 600
    //设置说过的结尾
    let wormEnd = 20;
    let tempCanvas;
    let tempCtx;
    let pattern
    //初始化抓虫子的图

    //声音的图片
    let soundImgClose;
    let soundImgOpen;
    //成功图片
    let success;
    function initWorm() {
      //第一张训练图片
      trainOne = new Image();
      trainTwo = new Image();
      trainThree = new Image();
      trainFour = new Image();
      onStep = new Image();
      underStep = new Image()
      startGame = new Image();
      backMa = new Image();
      soundImgClose = new Image();
      soundImgOpen = new Image();
      success = new Image()


      wormOne = new Image();
      wormTwo = new Image();
      wormThree = new Image();
      wormFour = new Image();
      bgtree = new Image();
      wormOne.src = './image/bug1.png';
      wormTwo.src = './image/bug2.png';
      wormThree.src = './image/bug3.png';
      wormFour.src = './image/bug4.png';
      bgtree.src = './image/tree.png'
      wormFlag = [true, true, true, true]
      trainOne.src = './image/worm1.png'
      trainTwo.src = './image/worm2.png'
      trainThree.src = './image/worm3.png'
      trainFour.src = './image/worm4.png'
      success.src = './image/success.png'
      onStep.src = './image/onStep.png'
      underStep.src = './image/underStep.png'
      startGame.src = './image/startGame.png'
      backMa.src = './image/backMa.png'
      soundImgClose.src = './image/close.png'
      soundImgOpen.src = './image/open.png'
      bgtree.onload = function () {
        countdown()
        //先把加载好的图片绘制在一个临时的画布上
        drawWorm()
      }
    }
    //画虫子
    function drawWorm() {
      widthLeft = (canvas.width - (1270 / 714 * canvas.height)) / 2
      if (trainingFlag == 1) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainOne, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        //增加下一步的按钮。点击后将trainningFlag设置2
        ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);

        //返回按钮
        //ctx.drawImage(backMa, 20, 20, 40, 30);
        ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);


      }
      if (trainingFlag == 2) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(trainThree, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
        // ctx.drawImage(trainThree, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
        //上一步按钮
        ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
        //下一步按钮
        ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
        //返回按钮
        ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);

        if (soundFlag) {
          ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
          // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30);
        } else {
          //ctx.drawImage(soundImgClose, canvas.width - 60, 20, 40, 30);
          ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
        }


      }
      if (trainingFlag == 3) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainTwo, widthLeft, 0, 1270 / 714 * canvas.height, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(trainFour, (canvas.width - (canvas.width / 2 + 200)) / 2, 80, canvas.width / 2 + 200, canvas.height / 2 + 130);
        //ctx.drawImage(trainFour, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
        //上一步按钮
        ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 220, 160, 80);
        //下一步按钮countdownFlag
        ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 220, 160, 80);
        //返回按钮
        ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
        // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30)
        if (soundFlag) {
          ctx.drawImage(soundImgOpen, canvas.width - widthLeft - 90, 30, 58, 46);
          // ctx.drawImage(soundImgOpen, canvas.width - 60, 20, 40, 30);
        } else {
          //ctx.drawImage(soundImgClose, canvas.width - 60, 20, 40, 30);
          ctx.drawImage(soundImgClose, canvas.width - widthLeft - 90, 30, 58, 46);
        }

      }
      if (trainingFlag == 4) {

        ctx.fillRect(0, 0, canvas.width, canvas.height)
        tempCanvas = document.createElement('canvas');
        tempCanvas.width = canvas.width;
        tempCanvas.height = canvas.height;
        tempCtx = tempCanvas.getContext('2d');
        tempCtx.drawImage(bgtree, 0, 0, canvas.width, canvas.height);
        pattern = ctx.createPattern(tempCanvas, 'repeat');
        ctx.fillStyle = pattern;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
        var gradient = ctx.createLinearGradient(0, 0, 100, 0);
        if (countdownFlag) {
          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "30px Arial";
          ctx.strokeText("倒计时:" + countdown(), canvas.width - 240, 100);

          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "30px Arial";
          ctx.strokeText("第" + diff + '关', 100, 100);

          ctx.font = "30px Arial";
          ctx.strokeText("得分:" + count, canvas.width - 240, 150);
          ctx.drawImage(backMa, widthLeft + 40, 30, 58, 46);
          // ctx.fillText("Hello World", 10, 50);
          //切水果倒计时三分钟
          //初始化
        }
        if (diffFlag) {
          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "80px Arial";
          ctx.strokeText("第 " + diff + ' 关', canvas.width / 2 - 120, canvas.height / 2 - 100);
        }
        if (passFlag == 2) {
          gradient.addColorStop("1.0", "red");
          // Fill with gradient
          ctx.strokeStyle = gradient;
          ctx.font = "20px Arial";
          ctx.strokeText("很遗憾您已超时,继续加油!", canvas.width / 2 - 100, canvas.height / 2 - 100);
          countdownFlag = false;
          diffFlag = false;
          //ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
          //重新开始按钮 //或返回主页
          // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
        }
        if (passFlag == 3) {
          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;
          // ctx.font = "50px Arial";
          // ctx.strokeText("您已通关!!!", canvas.width / 2 - 120, canvas.height / 2 - 100);success
          ctx.drawImage(success, canvas.width / 2 - 325, 10);
          ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 220, 160, 80);
          //  ctx.drawImage(success, canvas.width / 2 - 120, canvas.height / 2 - 100);
          // ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          countdownFlag = false;
          diffFlag = false;
          //重新开始按钮 //或返回主页
          // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
        }


        // ctx.font = "40px Arial";
        // ctx.strokeText("Hello World", 10, 50);
        // ctx.fillText("Hello World", 10, 50);
        //虫子1
        ctx.drawImage(wormOne, xWorm[0], yWorm[0], 60, 60);

        ctx.drawImage(wormTwo, xWorm[1], yWorm[1], 60, 60);

        //虫子三
        ctx.drawImage(wormThree, xWorm[2], yWorm[2], 60, 60);
        //虫子4
        ctx.drawImage(wormFour, xWorm[3], yWorm[3], 60, 60);
        let random = Math.random() + 1
        if (wormFlag[0]) {
          yWorm[0] = yWorm[0] - speedWorm[0] * random
        } else {
          yWorm[0] = yWorm[0] + speedWorm[0] * random * 1.5
        }
        if (yWorm[0] < wormEnd || yWorm[0] > wormStart) {
          yWorm[0] = wormStart
          wormFlag[0] = true
          random = Math.random() + 1
        }
        if (wormFlag[1]) {
          yWorm[1] = yWorm[1] - speedWorm[1] * random
        } else {
          yWorm[1] = yWorm[1] + speedWorm[1] * random * 1.5
        }
        //yWorm[1] = yWorm[1] - speedWorm[1] * random
        if (yWorm[1] < wormEnd || yWorm[1] > wormStart) {
          yWorm[1] = wormStart
          wormFlag[1] = true;
          random = Math.random() + 1
        }

        if (wormFlag[2]) {
          yWorm[2] = yWorm[2] - speedWorm[2] * random
        } else {
          yWorm[2] = yWorm[2] + speedWorm[2] * random * 1.5
        }
        //yWorm[2] = yWorm[2] - speedWorm[2] * random
        if (yWorm[2] < wormEnd || yWorm[2] > wormStart) {
          yWorm[2] = wormStart
          wormFlag[2] = true;
          random = Math.random() + 1
        }
        if (wormFlag[3]) {
          yWorm[3] = yWorm[3] - speedWorm[3] * random
        } else {
          yWorm[3] = yWorm[3] + speedWorm[3] * random * 1.5
        }

        if (yWorm[3] < wormEnd || yWorm[3] > wormStart) {
          yWorm[3] = wormStart
          wormFlag[3] = true;
          random = Math.random() + 1
        }
        let first = [xWorm[0] + 30, yWorm[0] + 30]
        //第二列中心点坐标
        let second = [xWorm[1] + 30, yWorm[1] + 30]
        let third = [xWorm[2] + 30, yWorm[2] + 30]
        let four = [xWorm[3] + 30, yWorm[3] + 30]
        collision(first, second, third, four)
      }
      timerWorm = requestAnimationFrame(drawWorm);
      if (fruitFlag !== 2) {
        window.cancelAnimationFrame(timerWorm)
        ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
      }
    }
    function soundClose() {
      player.pause()
    }

    function soundOpen() {
      mp3Url = soundSrc;
      player = new Audio(mp3Url);
      player.play()
    }
    //按钮点击音效
    function soundButtonOpen() {

      playerButton.play()
    }
    //捉虫子音效
    function soundHitOpen() {

      playerHit.play()
    }

    //背景音乐音效
    function soundBgOpen() {
      playerBg.play()
      playerBg.loop = true;
    }
    function soundBgClose() {
      playerBg.pause()
    }
    function getQueryString(name) {
      const url_string = window.location.href; // window.location.href
      const url = new URL(url_string);
      return url.searchParams.get(name);
    }
    function draw() {
      ctx.clearRect(0, 0, canvas.width, canvas.height); //清空所有的内容
      // let a = ctx.createPattern(bg, 'repeat')
      // ctx.fillStyle = a;
      // ctx.fillRect(0, 0, canvas.width, canvas.height)
      /*绘制 太阳*/
      tempCanvasFruit = document.createElement('canvas');
      tempCanvasFruit.width = canvas.width;
      tempCanvasFruit.height = canvas.height;
      tempCtxFruit = tempCanvasFruit.getContext('2d');
      tempCtxFruit.drawImage(bg, 0, 0, canvas.width, canvas.height);
      patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
      ctx.fillStyle = patternFruit;
      ctx.fillRect(0, 0, canvas.width, canvas.height);


      if (trainingFlag == 1) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainOne, 0, 0, canvas.width, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        //增加下一步的按钮。点击后将trainningFlag设置2
        ctx.drawImage(underStep, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);

        //返回按钮
        ctx.drawImage(backMa, 20, 20, 40, 30);


      }
      if (trainingFlag == 2) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainTwo, 0, 0, canvas.width, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        ctx.drawImage(trainThree, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
        //上一步按钮
        ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
        //下一步按钮
        ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
        //返回按钮
        ctx.drawImage(backMa, 20, 20, 40, 30);

      }
      if (trainingFlag == 3) {
        tempCanvasFruit = document.createElement('canvas');
        tempCanvasFruit.width = canvas.width;
        tempCanvasFruit.height = canvas.height;
        tempCtxFruit = tempCanvasFruit.getContext('2d');
        tempCtxFruit.drawImage(trainTwo, 0, 0, canvas.width, canvas.height);
        patternFruit = ctx.createPattern(tempCanvasFruit, 'repeat');
        ctx.fillStyle = patternFruit;
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        ctx.drawImage(trainFour, (canvas.width - canvas.width / 2) / 2, 100, canvas.width / 2, canvas.height / 2 + 100);
        //上一步按钮
        ctx.drawImage(onStep, canvas.width / 2 - 200, canvas.height / 2 + 100, 160, 80);
        //下一步按钮
        ctx.drawImage(underStep, canvas.width / 2 + 40, canvas.height / 2 + 100, 160, 80);
        //返回按钮
        ctx.drawImage(backMa, 20, 20, 40, 30);

      }

      if (trainingFlag == 4) {
        var gradient = ctx.createLinearGradient(0, 0, 100, 0);
        //返回按钮
        ctx.drawImage(backMa, 20, 20, 40, 30);
        if (countdownFlag) {
          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "30px Arial";
          ctx.strokeText("倒计时:" + countdown(), canvas.width - 240, 100);

          gradient.addColorStop("1.0", "#D1BBFF");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "30px Arial";
          ctx.strokeText("第" + diff + '关', 100, 100);

          ctx.font = "30px Arial";
          ctx.strokeText("得分:" + count, canvas.width - 240, 150);
          // ctx.fillText("Hello World", 10, 50);
          //切水果倒计时三分钟
          //初始化
        }
        if (diffFlag) {
          gradient.addColorStop("1.0", "yellow");
          // Fill with gradient
          ctx.strokeStyle = gradient;

          ctx.font = "80px Arial";
          ctx.strokeText("第 " + diff + ' 关', canvas.width / 2 - 120, canvas.height / 2 - 100);
        }
        if (passFlag == 2) {
          gradient.addColorStop("1.0", "red");
          // Fill with gradient
          ctx.strokeStyle = gradient;
          ctx.font = "20px Arial";
          ctx.strokeText("很遗憾您已超时,继续加油!", canvas.width / 2 - 100, canvas.height / 2 - 100);
          ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          countdownFlag = false;
          diffFlag = false;
          //重新开始按钮 //或返回主页
          //ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
        }
        if (passFlag == 3) {
          gradient.addColorStop("1.0", "yellow");
          // Fill with gradient
          ctx.strokeStyle = gradient;
          ctx.font = "20px Arial";
          ctx.strokeText("您已通关!!!", canvas.width / 2 - 100, canvas.height / 2 - 100);
          ctx.drawImage(startGame, canvas.width / 2 - 80, canvas.height / 2 + 100, 160, 80);
          countdownFlag = false;
          diffFlag = false;
          //重新开始按钮 //或返回主页
          // ctx.drawImage(button1, canvas.width / 2 - 200, canvas.height / 2 + 100, 60, 30);
        }

        // ctx.drawImage(button1, canvas.width / 2 - 100, canvas.height / 2 + 100, 100, 40);
        // ctx.drawImage(button1, canvas.width / 2 + 100, canvas.height / 2 + 100, 100, 40);
        if (flag[0]) {

          ctx.drawImage(apple, x[0], y[0], 60, 60);
          getColl()
        } else {
          apple_x_left += 0.5
          apple_x_right -= 0.5
          ctx.drawImage(appleLeft, apple_x_right, y[0], 60, 60);
          ctx.drawImage(appleRight, apple_x_left, y[0], 60, 60);
        }

        if (flag[1]) {
          ctx.drawImage(banana, x[1], y[1], 60, 30);
          getColl()
        } else {
          banana_x_left += 0.5
          banana_x_right -= 0.5
          ctx.drawImage(bananaLeft, banana_x_left, y[1], 60, 30);
          ctx.drawImage(bananaRight, banana_x_right, y[1], 60, 30);
        }

        if (flag[2]) {
          ctx.drawImage(sandia, x[2], y[2], 60, 60);
          getColl()
        } else {
          sandia_x_left += 0.5
          sandia_x_right -= 0.5
          ctx.drawImage(sandiaLeft, sandia_x_left, y[2], 60, 60);
          ctx.drawImage(sandiaRight, sandia_x_right, y[2], 60, 60);
        }
        if (flag[3]) {
          ctx.drawImage(basaha, x[3], y[3], 60, 60);
          getColl()
        } else {
          basaha_x_left += 0.5
          basaha_x_right -= 0.5
          ctx.drawImage(basahaLeft, basaha_x_left, y[3], 60, 60);
          ctx.drawImage(basahaRight, basaha_x_right, y[3], 60, 60);
        }


        random = Math.random() + 1
        y[0] = y[0] + speed[0] * random
        if (y[0] > fruitEnd) {
          y[0] = fruitStart
          flag[0] = true
          apple_x_left = x[0]
          apple_x_right = x[0]
        }
        random = Math.random() + 1
        y[1] = y[1] + speed[1] * random
        if (y[1] > fruitEnd) {
          y[1] = fruitStart
          flag[1] = true;
          banana_x_left = x[1];
          banana_x_right = x[1]
        }
        random = Math.random() + 1
        y[2] = y[2] + speed[2] * random
        if (y[2] > fruitEnd) {
          y[2] = fruitStart;
          flag[2] = true;
          sandia_x_left = x[2];
          sandia_x_right = x[2]
        }
        random = Math.random() + 1
        y[3] = y[3] + speed[3] * random
        if (y[3] > fruitEnd) {
          y[3] = fruitStart
          flag[3] = true;
          basaha_x_left = x[3]
          basaha_x_right = x[3]
        }
      }
      timer = requestAnimationFrame(draw);

      if (fruitFlag !== 1) {
        //取消循环
        window.cancelAnimationFrame(timer)
        ctx.clearRect(0, 0, 900, 900); //清空所有的内容
      }
      //
    }
    //在合适的时机调用碰撞检测方法
    function getColl() {
      let first = [x[0] + 30, y[0] + 30]
      //第二列中心点坐标
      let second = [x[1] + 30, y[1] + 15]

      let third = [x[2] + 30, y[2] + 30]

      let four = [x[3] + 30, y[3] + 30]
      //碰撞检测方法
      collision(first, second, third, four)
    }
    //分数判断方法
    function countComputer() {
      if (diff == 1) {
        if (passFlag == 1) {
          count += 5
        }
      }
      if (diff == 2) {
        // resetTime()
        if (passFlag == 1) {
          count += 5
        }
      }
      if (diff == 3) {
        // resetTime()
        if (passFlag == 1) {
          count += 5
        }
      }
    }
    //水果的相交方法
    function collision(first, second, third, four) {

      //判断是否与第一个相交
      if (wormFlag[0]) {
        if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
          //两个面积相交了\
          soundHitOpen()
          wormFlag[0] = false
          countComputer()
        }
      }

      //第二条线相交
      if (wormFlag[1]) {
        if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 60 / 2))) {
          //两个面积相交了
          soundHitOpen()
          wormFlag[1] = false
          countComputer()
        }
      }


      //第三条线相交
      if (wormFlag[2]) {
        if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
          //两个面积相交了
          soundHitOpen()
          wormFlag[2] = false
          countComputer()
        }
      }

      //第四条线相交
      //第三条线相交
      if (wormFlag[3]) {
        if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
          //两个面积相交了
          soundHitOpen()
          wormFlag[3] = false

          countComputer()
        }
      }

    }
    //虫子的相交方法
    function wormCollision(first, second, third, four) {

      //判断是否与第一个相交console

      if ((Math.abs(targ[0] - (first[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (first[1])) <= (60 / 2 + 60 / 2))) {
        //两个面积相交了

        wormFlag[0] = false
      }
      //第二条线相交
      if ((Math.abs(targ[0] - (second[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (second[1])) <= (60 / 2 + 60 / 2))) {
        //两个面积相交了

        wormFlag[1] = false
      }
      //第三条线相交
      if ((Math.abs(targ[0] - (third[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (third[1])) <= (60 / 2 + 60 / 2))) {
        //两个面积相交了

        wormFlag[2] = false
      }
      //第四条线相交
      //第三条线相交
      if ((Math.abs(targ[0] - (four[0])) <= (60 / 2 + 60 / 2)) && (Math.abs(targ[1] - (four[1])) <= (60 / 2 + 60 / 2))) {
        //两个面积相交了

        wormFlag[3] = false
      }
    }


    //倒计时方法
    function countdown() {
      startTimeFruit = new Date().getTime();
      let surplusTime = endTimeFruit - startTimeFruit;
      let th = Math.floor(surplusTime / (1000 * 60 * 60) % 24);
      let tm = Math.floor(surplusTime / (1000 * 60) % 60);
      let ts = Math.floor(surplusTime / 1000 % 60);
      if (ts < 10) {
        ts = '0' + ts
      }
      if (surplusTime >= 0) {
        if (!(passFlag == 2 || passFlag == 3)) {
          countdownFlag = true
        }
        return '0' + th + ':' + '0' + tm + ':' + ts
      } else {
        if (diff == 1) {
          if (count >= 90) {
            //执行保存结果
            levelFun(diff, count)
            totalScore += count;
            middleFont()
            speedWorm = [1.5, 2.0, 2.3, 2.4];
            //且进行将关卡设置为2
            diff = 2;
            count = 0
            //将时间重置为3分钟
            resetTime()
          } else {
            if (passFlag != 2) {
              levelFun(diff, count)
            }
            passFlag = 2
            //很遗憾失败了
          }
        } else if (diff == 2) {
          if (count >= 60) {
            middleFont()
            levelFun(diff, count)
            totalScore += count;
            speedWorm = [2.0, 2.5, 2.8, 3.1];
            diff = 3;
            count = 0
            //将时间重置为3分钟
            resetTime()
          } else {
            if (passFlag != 2) {
              levelFun(diff, count)
            }
            passFlag = 2
          }
        } else if (diff == 3) {
          if (count >= 45) {
            if (passFlag != 3) {
              totalScore += count;
              levelFun(diff, count)
              saveResult(totalScore)
            }
            passFlag = 3

          } else {
            if (passFlag != 2) {
              levelFun(diff, count)
            }
            passFlag = 2
          }
        }
        return '00' + ':' + '00' + ':' + '00'
      }
    }


    //捉虫子游戏变量
    $('#solar').hide();
    $('#exit').hide();
    $('#refresh').hide();


    let WASM_URL = "js/gazefilter.wasm";
    var buttonList = []
    $('#target').hide();
    $('#target1').hide();


    var record = {
      beforePoint: -1,
      currentPoint: 0,
    }

    //校准方法
    function calibrationAccuracy() {
      $('#solar').hide();
      handleFullScreen();
      getFun()
      $('#target1').show();
      $('#startCalibration').hide();
      switchTime()
      //初始化游戏
      $('#target1').hide()
      // $('#target').show()
      //clearInterval(time)
      //$('#solar').show();
      //$('#exit').show();
      //$('#refresh').show();

    }

    $("#startCalibration").click(function () {
      //  handleFullScreen();  //全屏
      handleFullScreen();
      getFun()
      $('#target1').show();
      $('#startCalibration').hide();
      //switchTime()
      //初始化游戏

      //
      $('#target1').hide()
      $('#target').show()
      clearInterval(time)
      $('#solar').show();
      //$('#exit').show();
      // $('#refresh').show();
      //init();
      //  $('.enterClass').css('display','none');
      //  $('.returnClass').css('display','block');
    });
    //切换游戏
    $('#exit').click(function () {
      //隐藏画布一
      // $('#exit').innerHtml()
      if (fruitFlag == 1) {
        setTimeout(() => {
          initWorm()
          fruitFlag = 2
        }, 30);
      } else {
        setTimeout(() => {
          // init()
          fruitFlag = 1
        }, 30);
      }
    })
    $('#refresh').click(function () {
      location.reload()
    })
    function handleFullScreen() {
      var de = document.documentElement;
      if (de.requestFullscreen) {
        de.requestFullscreen();
      } else if (de.msRequestFullscreen) {
        de.msRequestFullscreen();
      } else if (de.webkitRequestFullScreen) {
        de.webkitRequestFullScreen();
      } else if (de.mozRequestFullScreen) {
        de.mozRequestFullScreen();
      } else {
        wtx.info("当前浏览器不支持全屏!");
      }
    }
    //重置通过状态
    function resetStatus() {
      //重置是否通过的状态
      passFlag = 1;
      //重置速度
      speedWorm = [1, 1.5, 1.3, 1.4];
    }
    //重置计时函数
    function resetTime() {
      startTimeFruit = new Date().getTime();
      endTimeFruit = startTimeFruit + 180000;
      countdownFlag = true
    }
    //重置难度重置关数
    function resetCount(val, value) {
      //设置难度为多少
      diff = val;
      //设置分数为多少
      count = value;
    }
    //轮询当前的图表
    var time = null;

    function switchTime() {
      let a = 0
      time = setInterval(() => {
        $('#target1').show()
        $('#targetMini').show()
        if (a == 16) {
          $('#target1').hide()
          $('#target').show()
          clearInterval(time)
          $('#solar').show();
          //$('#exit').show();
          // $('#refresh').show();
          trainingFlag = 4
          //重置计时函数
          resetTime()
          //重置分数和参数
          resetCount(1, 0)
          //都重置到初始状态
          resetStatus();

        } else {

          $('#targetMini1').css('transition', '0s');
          $('#targetMini1').css('width', 54 + 'px');
          $('#targetMini1').css('height', 54 + 'px');

          let x = buttonList[a].x1;
          x = Math.floor(x)
          let y = buttonList[a].y1;
          y = Math.floor(y)

          $('#target1').css('left', x + 'px');
          $('#target1').css('top', y + 'px');



          //  init();
          setTimeout(() => {
            $('#targetMini1').css('transition', '1s');
            $('#targetMini1').css('width', 20 + 'px');
            $('#targetMini1').css('height', 20 + 'px');
          }, 1000);
          setTimeout(() => {
            coordinates(x + 30, y + 30)

          }, 1500);
          setTimeout(() => {
            coordinates(x + 30, y + 30)

          }, 1840);
          setTimeout(() => {
            coordinates(x + 30, y + 30)

          }, 1940);
          a = a + 1
        }

      }, 2000)


    }
    function coordinates(x, y) {

      gazefilter.tracker.calibrate(    //校正,发送当前点击位置进行校正
        //event.timeStamp,
        window.performance.now(),
        //window.performance.now(),
        //mill,
        //1000,  // relative to performance.timeOrigin
        x,  // in pixels
        y,  // in pixels
        1.0  // see note below
      );
    }
    //获取各个按钮的left和top

    /**
     * 在屏幕上画出注视点
     * 是否画点,以及画点方式均可自定义
     **/
    function drawTargetOnScreen(position) {

      if (position != null && position.length == 2) {
        //判断是否是NAN如果是NAN则不执行
        //如果不是则需要判断当前点是否在第一个点中,如果在则执行settimeout函数1秒后改变改块状态
        //如果在第一个点中但第二次坐标出来的时候还在第一个坐标里那就没问题
        // $('#target').css('display', 'block');
        targ[0] = position[0] + 60;
        targ[1] = position[1] + 60;
        $('#target').css('left', position[0] + 'px');
        $('#target').css('top', position[1] + 'px');
        // $('#target').css('z-index', 100);
        //judge(position[0], position[1])
        // judge(position[0], position[1])

      }
    }
    //判断是否在第一个点中


    //获取全部的校验点
    function getFun() {
      for (let i = 0; i < 16; i++) {
        let a = document.getElementById('num' + (i + 1));

        buttonList.push({
          x1: a.getBoundingClientRect().left,
          y1: a.getBoundingClientRect().top
        })
      }
    }
    async function initGazefilter() {
      cameraFlag = false
      await gazefilter.init(WASM_URL);  //初始化
      await gazefilter.tracker.connect();  //连接摄像头
      let canvas = document.getElementById("tracker-canvas");
      await gazefilter.visualizer.setCanvas(canvas);  //绘制图像
      cameraFlag = true
      await gazefilter.tracker.addListener("calib", response => {

        //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.
      });
    }

    function startTrace() {
      gazefilter.tracker.addListener("filter", event => { //注视点
        drawTargetOnScreen(event.bestGazePoint());  //Best gaze point of left or right eye.  其他取点可参考https://beehiveor.gitlab.io/gazefilter/data.html,有不同类型的点 fixationPoint
      });
    }
    function hideSence() {
      //隐藏目标物
      $('#target').hide()
      $('#target1').hide()
      $('#targetMini').hide()
      //隐藏摄像头
      $('#solar').show()

    }
    $("#finishcalibration").click(function () {
      window.removeEventListener("click", onmouseclick);    //九点校正完成,取消鼠标事件,并开始绘制轨迹点
      $("#finishcalibration").hide();
    })
    $(document).ready(function () {
      //将canvas自适应屏幕
      resizeCanvas()
      //init()
      initWorm()
      hideSence()
      //将该隐藏的隐藏然后初始化游戏场景
      //初始华摄像头
      initGazefilter();
      startTrace();

    });

  </script>
</body>

</html>