JohnBrownings JohnBrownings - 2 months ago
211 1

javacode

jQuery

Traffic Light JavaScript John-Brownings

<!DOCTYPE html>
<head>
    <title> Traffic Light</title>   

    <style>
        .rainbow {
            background-image: -webkit-gradient( linear, left top, right top, color-stop(0, red), color-stop(0.1, yellow), color-stop(0.2, green));
            background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
            color:transparent;
            -webkit-background-clip: text;
            background-clip: text;
        }
    </style>

</head>

<body background="street.gif">
    <h1 class="rainbow">Traffic Light</h1>

    <canvas id="myCanvas" width="200" height="300"
    style="border:1px solid #000000;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>


    <script>   

        var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");

        ctx.rect(0, 0, 200, 300);
        ctx.fillStyle = "grey";
        ctx.fill();

        var colours=["red", "yellow", "green", "black","red yellow"];
        var current=colours[0];

        function offlight() {
            ctx.beginPath();
            ctx.arc(95,50,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function offlight1() {
            ctx.beginPath();
            ctx.arc(95,150,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function offlight2() {
            ctx.beginPath();
            ctx.arc(95,250,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight1() {
            ctx.beginPath();
            ctx.arc(95,50,40,10,12*Math.PI);
            ctx.fillStyle = "red";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight2() {
            ctx.beginPath();
            ctx.arc(95,150,40,10,12*Math.PI);
            ctx.fillStyle = "yellow";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight3() {
            ctx.beginPath();
            ctx.arc(95,250,40,10,12*Math.PI);
            ctx.fillStyle = "green";
            ctx.fill();
            ctx.stroke();
        }

        function changelight(){

            if (current==colours[0]){
                drawLight1();
                offlight1();
                offlight2();
                current=colours[4]
            } else if (current==colours[4]){
                drawLight1();
                drawLight2();
                offlight2();
                current=colours[2]
            } else if (current==colours[2]) {
                offlight();
                offlight1();
                drawLight3();
                current=colours[3]
            } else if (current==colours[3]){
                offlight();
                drawLight2();
                offlight2();
                current=colours[0]
            }

        }
        setInterval(changelight,1000);

    </script>

    <br><br>
    <button onclick="changelight()">Click</button>

</body>
</html>


without automatic timer

<!DOCTYPE html>
<head>
    <title> Traffic Light</title>   

    <style>
        .rainbow {
            background-image: -webkit-gradient( linear, left top, right top, color-stop(0, red), color-stop(0.1, yellow), color-stop(0.2, green));
            background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
            color:transparent;
            -webkit-background-clip: text;
            background-clip: text;
        }
    </style>

</head>

<body background="street.gif">
    <h1 class="rainbow">Traffic Light</h1>

    <canvas id="myCanvas" width="200" height="300"
    style="border:1px solid #000000;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>


    <script>   

        var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");

        ctx.rect(0, 0, 200, 300);
        ctx.fillStyle = "grey";
        ctx.fill();

        var colours=["red", "yellow", "green", "black","red yellow"];
        var current=colours[0];

        function offlight() {
            ctx.beginPath();
            ctx.arc(95,50,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function offlight1() {
            ctx.beginPath();
            ctx.arc(95,150,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function offlight2() {
            ctx.beginPath();
            ctx.arc(95,250,40,10,12*Math.PI);
            ctx.fillStyle = "black";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight1() {
            ctx.beginPath();
            ctx.arc(95,50,40,10,12*Math.PI);
            ctx.fillStyle = "red";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight2() {
            ctx.beginPath();
            ctx.arc(95,150,40,10,12*Math.PI);
            ctx.fillStyle = "yellow";
            ctx.fill();
            ctx.stroke();
        }

        function drawLight3() {
            ctx.beginPath();
            ctx.arc(95,250,40,10,12*Math.PI);
            ctx.fillStyle = "green";
            ctx.fill();
            ctx.stroke();
        }

        function changelight(){

            if (current==colours[0]){
                drawLight1();
                offlight1();
                offlight2();
                current=colours[4]
            } else if (current==colours[4]){
                drawLight1();
                drawLight2();
                offlight2();
                current=colours[2]
            } else if (current==colours[2]) {
                offlight();
                offlight1();
                drawLight3();
                current=colours[3]
            } else if (current==colours[3]){
                offlight();
                drawLight2();
                offlight2();
                current=colours[0]
            }

        }
        $('p').trigger('click') ;(changelight,3000);

    </script>

    <br><br>
    <button onclick="changelight()">Click</button>

</body>
</html>