[Javascript] Canvas circle e coordinate

lidya123

Nuovo Utente
16 Mag 2017
5
1
3
30
Salve a tutti.
Sono nuova del forum e spero che qualcuno possa aiutarmi.
Dovrei realizzare una pagina html in fullscreen in cui vi sono dei cerchi numerati posizionati in 9 punti precisi dello schermo, come mostrato nell'immagine

esempio.jpg


L'utente deve premere(cliccare o touch su tablet) questi cerchi in sequenza, dall'1 al 9, e di volta in volta i cerchi divengono verdi per indicare che il click è stato avvenuto e visualizza in un alert o in un form le coordinate del tocco.

Non sono molto esperta di html e javascript, presumo che si debba creare un canvas a pieno schermo(o piena pagina, dato che ho bisogno del full screen) e dei metodi per disegnare e prelevare le coordinate. Qualcuno può aiutarmi anche con un esempio di progetto già pronto? prediligo un progetto visto che sono alle prime armi e per evitare di fare enormi domande, va bene anche con uno o due cerchi, giusto come esempio in modo che lo studi e modifichi secondo le mie specifiche.

grazie infinite in anticipo e spero di trovare qualcuno che mi aiuti
 
Ciao, non puoi aprire in fullscreen il browser senza che l'utente richiami la funzione.
Codice:
<script>
    function fullScreen() {
        var el = document.documentElement;
        var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;

        if (typeof rfs !== "undefined" && rfs) {
            rfs.call(el);
        } else if (typeof window.ActiveXObject !== "undefined") {
            // for Internet Explorer
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            } else {
                alert("funzione non supportata dal browsernPremi F11");
            }
        } else {
            alert("funzione non supportata dal browsernPremi F11");
        }
    }

</script>
<input type="button" onclick="fullScreen()" value="fullscreen"/>
 
Grazie infinite Max 1 e Criric per le risposte tempestive.
Vi posto il mio pezzo di codice che sono riuscita a fare fino ad ora:
HTML:
<!DOCTYPE html>
<html>
    <body>
            <canvas id="canvas" style="border:1px solid #d3d3d3;"></canvas>

        
                <script>
                //document.write(screen.width+" x "+screen.height);  //per visualizzare la risoluzione dello schermo

                            //cliccando o premendo(touch) in un punto qualsiasi dello schermo, si attiva la modalità fullscreen
                            addEventListener("click", function() {
                                        var
                                              el = document.documentElement
                                            , rfs =
                                                   el.requestFullScreen
                                    || el.webkitRequestFullScreen
                                                || el.mozRequestFullScreen
                                        ;
                                        rfs.call(el);
                                    });


                        //impostazione del canvas in grandezza di fullscreen
                        var canvas = document.getElementById('canvas');
                            canvas.width = screen.width-25;
                            canvas.height = screen.height-25;


                        var c1 = canvas.getContext('2d');
                        var c2 = canvas.getContext('2d');
                        var c3 = canvas.getContext('2d');
                        var c4 = canvas.getContext('2d');
                        var c5 = canvas.getContext('2d');
                        var c6 = canvas.getContext('2d');
                        var c7 = canvas.getContext('2d');
                        var c8 = canvas.getContext('2d');
                        var c9 = canvas.getContext('2d');
                        var radius = 24;
                        var linestroke = 2.5;

                        c1.beginPath();
                        c1.arc(25, 25 , radius, 0, 2 * Math.PI);
                        c1.lineWidth = linestroke;
                        c1.strokeStyle = '#003300';
                        c1.stroke();
                        c1.font="30px Georgia";
                        c1.fillText("1",18,30);
                        //c1.fillStyle = 'green';
                        //c1.fill();


                        c2.beginPath();
                        c2.arc(canvas.width/2, 25 , radius, 0, 2 * Math.PI);
                        c2.lineWidth = linestroke;
                        c2.strokeStyle = '#003300';
                        c2.stroke();
                        c2.font="30px Georgia";
                        c2.fillText("2",canvas.width/2-10,30);


                        c3.beginPath();
                        c3.arc(canvas.width-25, 25 , radius, 0, 2 * Math.PI);
                        c3.lineWidth = linestroke;
                        c3.strokeStyle = '#003300';
                        c3.stroke();
                        c3.font="30px Georgia";
                        c3.fillText("3",canvas.width-35,30);


                        c4.beginPath();
                        c4.arc(25,canvas.height/2, radius, 0, 2 * Math.PI);
                        c4.lineWidth = linestroke;
                        c4.strokeStyle = '#003300';
                        c4.stroke();
                        c4.font="30px Georgia";
                        c4.fillText("4",15,canvas.height/2+5);


                        c5.beginPath();
                        c5.arc(canvas.width/2, canvas.height/2, radius, 0, 2 * Math.PI);
                        c5.lineWidth = linestroke;
                        c5.strokeStyle = '#003300';
                        c5.stroke();
                        c5.font="30px Georgia";
                        c5.fillText("5",canvas.width/2-10,canvas.height/2+5);


                        c6.beginPath();
                        c6.arc(canvas.width-25, canvas.height/2, radius, 0, 2 * Math.PI);
                        c6.lineWidth = linestroke;
                        c6.strokeStyle = '#003300';
                        c6.stroke();
                        c6.font="30px Georgia";
                        c6.fillText("6",canvas.width-35,canvas.height/2+10);


                        c7.beginPath();
                        c7.arc(25,canvas.height-25, radius, 0, 2 * Math.PI);
                        c7.lineWidth = linestroke;
                        c7.strokeStyle = '#003300';
                        c7.stroke();
                        c7.font="30px Georgia";
                        c7.fillText("7",15,canvas.height-20);


                        c8.beginPath();
                        c8.arc(canvas.width/2,canvas.height-25, radius, 0, 2 * Math.PI);
                        c8.lineWidth = linestroke;
                        c8.strokeStyle = '#003300';
                        c8.stroke();
                        c8.font="30px Georgia";
                        c8.fillText("8",canvas.width/2-10,canvas.height-18);

                        c9.beginPath();
                        c9.arc(canvas.width-25, canvas.height-25, radius, 0, 2 * Math.PI);
                        c9.lineWidth = linestroke;
                        c9.strokeStyle = '#003300';
                        c9.stroke();
                        c9.font="30px Georgia";
                        c9.fillText("9",canvas.width-33,canvas.height-20);


                </script>


    </body>

</html>
Questo il risultato: http://www.dittavalente.it/lm/calibrazione.html

Secondo voi sto procedendo bene?
Ora mi domando, come faccio ad associare ad ogni context (c1,c2,c3 ecc...) una metodo che preleva le coordinate del tocco e creare un alert?

Mi va bene un alert, ma il mio compito e quello di inviare queste coordinate ad un webserver java attraverso le websocket (websocket già utilizzate in precedenza per un altro progetto).

Da premettere che ho più domestichezza col Java rispetto all'html e javascript =) ...per questo chiedo aiuto ^.^

Grazie infinite per l'aiuto ^.^
 
Grazie mille........ciò che mi serve.
Ma ho ricopiato il codice in un unico file .html per eseguire qualche test, ma mi crea solo il canvas con i bordi rossi senza crearmi i rettangoli

HTML:
<!DOCTYPE html>
<html>
    <body>
    <p>Hover over a rect = mouseover</p>
    <p>Click on a rect = mouseclick</p>
            <canvas id="canvas" width=1000 height=500></canvas>
            <style>
                        body {
                            background-color: white;
                        }
                        canvas {
                            border:1px solid red;
                        }
            </style>

        
                <script>
                    var canvas = document.getElementById("canvas");
                    var ctx = canvas.getContext("2d");
                    var canvasOffset = $("#canvas").offset();
                    var offsetX = canvasOffset.left;
                    var offsetY = canvasOffset.top;

        //
                    var rect = (function () {

                        // constructor
                        function rect(id, x, y, width, height, fill, stroke, strokewidth) {
                            this.x = x;
                            this.y = y;
                            this.id = id;
                            this.width = width;
                            this.height = height;
                            this.fill = fill || "gray";
                            this.stroke = stroke || "skyblue";
                            this.strokewidth = strokewidth || 2;
                            this.redraw(this.x, this.y);
                            return (this);
                        }
                        rect.prototype.redraw = function (x, y) {
                            this.x = x || this.x;
                            this.y = y || this.y;
                            this.draw(this.stroke);
                            return (this);
                        }
                        //
                        rect.prototype.highlight = function (x, y) {
                            this.x = x || this.x;
                            this.y = y || this.y;
                            this.draw("orange");
                            return (this);
                        }
                        //
                        rect.prototype.draw = function (stroke) {
                            ctx.save();
                            ctx.beginPath();
                            ctx.fillStyle = this.fill;
                            ctx.strokeStyle = stroke;
                            ctx.lineWidth = this.strokewidth;
                            ctx.rect(this.x, this.y, this.width, this.height);
                            ctx.stroke();
                            ctx.fill();
                            ctx.restore();
                        }
                        //
                        rect.prototype.isPointInside = function (x, y) {
                            return (x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height);
                        }


                        return rect;
                    })();


                    //
                    function handleMouseDown(e) {
                        mouseX = parseInt(e.clientX - offsetX);
                        mouseY = parseInt(e.clientY - offsetY);

                        // Put your mousedown stuff here
                        var clicked = "";
                        for (var i = 0; i < rects.length; i++) {
                            if (rects[i].isPointInside(mouseX, mouseY)) {
                                clicked += rects[i].id + " "
                            }
                        }
                        if (clicked.length > 0) {
                            alert("Clicked rectangles: " + clicked);
                        }
                    }

                    //
                    function handleMouseMove(e) {
                        mouseX = parseInt(e.clientX - offsetX);
                        mouseY = parseInt(e.clientY - offsetY);

                        // Put your mousemove stuff here
                        ctx.clearRect(0, 0, canvas.width, canvas.height);
                        for (var i = 0; i < rects.length; i++) {
                            if (rects[i].isPointInside(mouseX, mouseY)) {
                                rects[i].highlight();
                            } else {
                                rects[i].redraw();
                            }
                        }
                    }


                    //
                    var rects = [];
                    //
                    rects.push(new rect("Red-Rectangle", 15, 35, 65, 60, "red", "black", 10));
                    rects.push(new rect("Green-Rectangle", 60, 80, 70, 50, "green", "black", 10));
                    rects.push(new rect("Blue-Rectangle", 125, 25, 25, 25, "blue", "black", 10));

                    //
                    $("#canvas").click(handleMouseDown);
                    $("#canvas").mousemove(handleMouseMove);

                </script>


    </body>

</html>

Come mai non funziona?
 
devi includere la libreria jquery, gli eventi vengono gestiti dalla libreria
Codice:
$("#canvas").click(handleMouseDown);
$("#canvas").mousemove(handleMouseMove);
o la scarichi in una tua directory o la puoi prendere anche online. Guardi qui.
EDIT
naturalmente puoi anche modificare le funzioni jquery con puro javascipt
 
Ultima modifica:
  • Like
Reactions: lidya123
Grazie mille Criric.......sei grande ( :* kiss)
Mi hai dato veramente una mano.

Ora funziona e sono riuscita a fare ciò che mi serve
 
  • Like
Reactions: criric

Discussioni simili