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: