-1

im developing a roulette that takes the info from db and show the registered users and it works but i need the overflow hidden or overflow ellipsis from css or something usable in canvas/javascript.

Here is the code:

                        <canvas id="canvas" width="500" height="500"></canvas>
                        <!-- Ruleta -->
                        <!--<script src="ruleta.js"></script>-->
                        <script>
                            var counter = 0;
                            var options = [
                                <?php

                                $queryHasWinner = "SELECT * FROM usuarios WHERE ganador='1'";
                                $resultHasWinner = mysqli_query($conn, $queryHasWinner);
                                $queryDontHasWinner = "SELECT * FROM usuarios WHERE ganador='0'";
                                $resultDontHasWinner = mysqli_query($conn, $queryDontHasWinner);

                                if (mysqli_num_rows($resultHasWinner) > 0) {
                                    for ($i = 0; $i < $count[0]; $i++) {
                                        $queryCheckUser = mysqli_query($conn, "SELECT * FROM usuarios WHERE ganador='1'") or die(mysqli_error($conn));
                                        while ($row = mysqli_fetch_array($queryCheckUser)) {
                                            echo "'{$row['email']}',";
                                        }
                                    }
                                    $queryChangeMail = mysqli_query($conn, "UPDATE usuarios SET ganador='2' WHERE ganador='1'");
                                }
                                if (mysqli_num_rows($resultHasWinner) == 0 && mysqli_num_rows($resultDontHasWinner) > 0) {
                                    while ($row = mysqli_fetch_array($resultDontHasWinner)) {
                                        echo "'{$row['email']}',";
                                    }
                                    header("refresh: 1");
                                }

                                ?>
                            ];
                            console.log(options);


                            var startAngle = 0;
                            var arc = Math.PI / (options.length / 2);
                            var spinTimeout = null;

                            var spinArcStart = 10;
                            var spinTime = 0;
                            var spinTimeTotal = 0;

                            var ctx;

                            document.getElementById("spin").addEventListener("click", spin);

                            function byte2Hex(n) {
                                var nybHexString = "0123456789ABCDEF";
                                return String(nybHexString.substr((n >> 4) & 0x0F, 1)) + nybHexString.substr(n & 0x0F, 1);
                            }

                            function RGB2Color(r, g, b) {
                                return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
                            }

                            function getColor(item, maxitem) {
                                var phase = 0;
                                var center = 128;
                                var width = 127;
                                var frequency = Math.PI * 2 / maxitem;

                                red = Math.sin(frequency * item + 2 + phase) * width + center;
                                green = Math.sin(frequency * item + 0 + phase) * width + center;
                                blue = Math.sin(frequency * item + 4 + phase) * width + center;

                                return RGB2Color(red, green, blue);
                            }

                            function drawRouletteWheel() {
                                var canvas = document.getElementById("canvas");
                                if (canvas.getContext) {
                                    var outsideRadius = 200;
                                    var textRadius = 120;
                                    var insideRadius = 50; //radius interno, estaba en 50

                                    ctx = canvas.getContext("2d");
                                    ctx.clearRect(0, 0, 500, 500);

                                    ctx.strokeStyle = "white";
                                    ctx.lineWidth = 2; //separacion entre resultados

                                    ctx.font = 'bold 10px Helvetica, Arial';

                                    for (var i = 0; i < options.length; i++) {
                                        var angle = startAngle + i * arc;
                                        //ctx.fillStyle = colors[i];
                                        ctx.fillStyle = getColor(i, options.length);

                                        ctx.beginPath();
                                        ctx.arc(250, 250, outsideRadius, angle, angle + arc, false);
                                        ctx.arc(250, 250, insideRadius, angle + arc, angle, true);
                                        ctx.stroke();
                                        ctx.fill();

                                        ctx.save();
                                        ctx.fillStyle = "black";
                                        ctx.translate(250 + Math.cos(angle + arc / 2) * textRadius,
                                            250 + Math.sin(angle + arc / 2) * textRadius);
                                        ctx.rotate(angle + arc / 2 + Math.PI / 80);
                                        var text = options[i];
                                        //ctx.fillText(text, -ctx.measureText(text).width / 4, 0); //aqui llena los mails <-- original
                                        ctx.fillText(text, -ctx.measureText(text).width / 4, 0);
                                        ctx.restore();
                                    }

                                    //Arrow
                                    ctx.fillStyle = "black";
                                    ctx.beginPath();
                                    ctx.moveTo(250 - 4, 250 - (outsideRadius + 5));
                                    ctx.lineTo(250 + 4, 250 - (outsideRadius + 5));
                                    ctx.lineTo(250 + 4, 250 - (outsideRadius - 5));
                                    ctx.lineTo(250 + 9, 250 - (outsideRadius - 5));
                                    ctx.lineTo(250 + 0, 250 - (outsideRadius - 13));
                                    ctx.lineTo(250 - 9, 250 - (outsideRadius - 5));
                                    ctx.lineTo(250 - 4, 250 - (outsideRadius - 5));
                                    ctx.lineTo(250 - 4, 250 - (outsideRadius + 5));
                                    ctx.fill();
                                }
                            }

                            function spin() {
                                spinAngleStart = Math.random() * 10 + 10;
                                spinTime = 0;
                                spinTimeTotal = Math.random() * 3 + 4 * 1000;
                                rotateWheel();
                            }

                            function rotateWheel() {
                                spinTime += 30;
                                if (spinTime >= spinTimeTotal) {
                                    stopRotateWheel();
                                    return;
                                }
                                var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
                                startAngle += (spinAngle * Math.PI / 180);
                                drawRouletteWheel();
                                spinTimeout = setTimeout('rotateWheel()', 30);
                                var audio = new Audio('tick.mp3');
                                audio.play();
                            }

                            function stopRotateWheel() {
                                clearTimeout(spinTimeout);
                                var degrees = startAngle * 180 / Math.PI + 90;
                                var arcd = arc * 180 / Math.PI;
                                var index = Math.floor((360 - degrees % 360) / arcd);
                                ctx.save();

                                //ctx.fillStyle = "rgba(255, 255, 255, 0.5)"; //blanco
                                //ctx.fillRect(0,0, 500, 500);
                                ctx.fillStyle = "rgba(240, 52, 52, 0.5)"; //rojo
                                var centerX = canvas.width / 2;
                                var centerY = canvas.height / 2;
                                var radius = 200;
                                var context = canvas.getContext('2d');

                                context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
                                context.fill();

                                ctx.font = 'bold 30px Helvetica, Arial';
                                ctx.fillStyle = "white";
                                ctx.shadowColor = "black";
                                ctx.shadowBlur = 6;
                                ctx.lineWidth = 6;
                                var text = options[index]
                                ctx.fillText(text, 250 - ctx.measureText(text).width / 2, 250 + 10);
                                ctx.restore();

                                var winnerSound = new Audio('winner.mp3');
                                winnerSound.play();
                                console.log("Ganador: " + text);
                                setCookie("emailCookie", text, 1);
                                makeRequest('ruleta.php', text);
                                Location.reload();


                                <?php
                                if (isset($_COOKIE['emailCookie'])) {
                                    $cookie = $_COOKIE['emailCookie'];
                                    $queryCookie = mysqli_query($conn, "UPDATE usuarios SET ganador='2' WHERE email='$cookie'");
                                }
                                ?>



                            }


                            function makeRequest(url, text) {
                                httpRequest.open('POST', url);
                                httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                httpRequest.send('name=' + encodeURIComponent(text));
                            }

                            function setCookie(name, value, days) {
                                var expires = "";
                                if (days) {
                                    var date = new Date();
                                    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                                    expires = "; expires=" + date.toUTCString();
                                }
                                document.cookie = name + "=" + (value || "") + expires + "; path=/";
                            }

                            function easeOut(t, b, c, d) {
                                var ts = (t /= d) * t;
                                var tc = ts * t;
                                return b + c * (tc + -3 * ts + 3 * t);
                            }

                            drawRouletteWheel();
                        </script>

So the problem is in the line:

ctx.fillText(text, -ctx.measureText(text).width / 4, 0);

overflows the canvas

You know how i can set in canvas the ... or at least cutoff the text? thanks

The roulette show the emails like this:

enter image description here

Slvr
  • 40
  • 8
  • 1
    It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 21 '20 at 21:28
  • Could you post a minimal example of your problem - just enough css/html/js to show a text overflow problem? – A Haworth Oct 21 '20 at 22:17
  • @AHaworth ive added the image it is how is working now with the email overflow – Slvr Oct 21 '20 at 22:19
  • 1
    Ah, so it's not overflowing the canvas (which is a rectangle), it's overflowing the circular part that you have drawn so you need to do some masking or calculate the text length and cut it off before you draw it on the canvas. – A Haworth Oct 21 '20 at 22:40

1 Answers1

0

The solution was obvious, thanks for let me see @AHaworth.

It is:

if (text.length > 10) {
    text = text.substring(0, 18) + "...";
}

Canvas doesnt provide that but with js is easy. Thank you guys.

Slvr
  • 40
  • 8