Advertisement
ekrajchevska

[ИП] Семафор

Aug 21st, 2017
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6.     <style>
  7.         canvas{
  8.             border: 1px solid #0e1606;
  9.         }
  10.     </style>
  11.     <script src="jquery-3.2.1.min.js"></script>
  12. </head>
  13. <body onload="turnOn()">
  14. <canvas id="myCanvas" height="360px" width="150px" onclick="iskluchi()"></canvas>
  15.  
  16. <script>
  17.     var c = document.getElementById("myCanvas");
  18.     var ctx = c.getContext("2d");
  19.     var redOn = false,yellowOn = false,greenOn = false;
  20.     var redOff = true, yellowOff = true, greenOff = true;
  21.     var flag = false;
  22.     var click = false;
  23.     var time,iskluchen;
  24.  
  25.     function init(){
  26.         //crveno
  27.         ctx.beginPath();
  28.         ctx.fillStyle = "#A11908";
  29.         ctx.arc(70,60,50,0,2*Math.PI);
  30.         ctx.fill();
  31.         ctx.closePath();
  32.  
  33.         //zolto
  34.         ctx.beginPath();
  35.         ctx.fillStyle = "#CBAF0F";
  36.         ctx.arc(70,165,50,0,2*Math.PI);
  37.         ctx.fill();
  38.         ctx.closePath();
  39.  
  40.         //zeleno
  41.         ctx.beginPath();
  42.         ctx.fillStyle = "#466D1B";
  43.         ctx.arc(70,270,50,0,2*Math.PI);
  44.         ctx.fill();
  45.         ctx.closePath();
  46.     }
  47.  
  48.    function turnOn () {
  49.  
  50.         init();
  51.         click = false;
  52.         clearInterval(iskluchen);
  53.  
  54.         time = setInterval( function() {
  55.  
  56.             if (greenOff) {
  57.  
  58.                 ctx.beginPath();
  59.                 ctx.fillStyle = "#7EF910";
  60.                 ctx.arc(70, 270, 50, 0, 2 * Math.PI);
  61.                 ctx.fill();
  62.                 ctx.closePath();
  63.  
  64.                 greenOff = false;
  65.                 greenOn = true;
  66.             } else if (greenOn)
  67.             {
  68.                 ctx.beginPath();
  69.                 ctx.fillStyle = "#466D1B";
  70.                 ctx.arc(70,270,50,0,2*Math.PI);
  71.                 ctx.fill();
  72.                 ctx.closePath();
  73.  
  74.                 yellowOff = true;
  75.                 flag = false;
  76.                 greenOn = false;
  77.             }
  78.             else if(yellowOff)
  79.             {
  80.                 ctx.beginPath();
  81.                 ctx.fillStyle = "#FFF200";
  82.                 ctx.arc(70,165,50,0,2*Math.PI);
  83.                 ctx.fill();
  84.                 ctx.closePath();
  85.  
  86.                 yellowOff = false;
  87.                 yellowOn = true;
  88.             } else if(yellowOn){
  89.  
  90.                 ctx.beginPath();
  91.                 ctx.fillStyle = "#CBAF0F";
  92.                 ctx.arc(70,165,50,0,2*Math.PI);
  93.                 ctx.fill();
  94.                 ctx.closePath();
  95.  
  96.                 yellowOn = false;
  97.                 if(flag) greenOff = true;
  98.                 else redOff = true;
  99.             } else if(redOff)
  100.             {
  101.                 ctx.beginPath();
  102.                 ctx.fillStyle = "#FF0009";
  103.                 ctx.arc(70,60,50,0,2*Math.PI);
  104.                 ctx.fill();
  105.                 ctx.closePath();
  106.  
  107.                 redOff = false;
  108.                 redOn = true;
  109.             } else if(redOn)
  110.             {
  111.                 ctx.beginPath();
  112.                 ctx.fillStyle = "#A11908";
  113.                 ctx.arc(70,60,50,0,2*Math.PI);
  114.                 ctx.fill();
  115.                 ctx.closePath();
  116.  
  117.                 redOn = false;
  118.                 yellowOff = true;
  119.                 flag = true;
  120.             }
  121.  
  122.  
  123.         },1000);
  124.  
  125.  
  126.     };
  127.  
  128.     function iskluchi(){
  129.        init();
  130.         yellowOff = true;
  131.  
  132.         clearInterval(time);
  133.  
  134.         if(click) {
  135.             turnOn();
  136.         }
  137.  
  138.         click = true;
  139.  
  140.        iskluchen = setInterval(function () {
  141.  
  142.            if(yellowOff)
  143.            {
  144.                ctx.beginPath();
  145.                ctx.fillStyle = "#FFF200";
  146.                ctx.arc(70,165,50,0,2*Math.PI);
  147.                ctx.fill();
  148.                ctx.closePath();
  149.  
  150.                yellowOff = false;
  151.                yellowOn = true;
  152.            } else if(yellowOn){
  153.  
  154.                ctx.beginPath();
  155.                ctx.fillStyle = "#CBAF0F";
  156.                ctx.arc(70,165,50,0,2*Math.PI);
  157.                ctx.fill();
  158.                ctx.closePath();
  159.  
  160.                yellowOn = false;
  161.                yellowOff = true;
  162.            }
  163.  
  164.        }, 1000);
  165.  
  166.     }
  167.  
  168.  
  169.  
  170. </script>
  171.  
  172. </body>
  173. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement