1

我有一些 html5 和 javascript 代码来创建一个旋转的轮盘赌。它分为 14 个结果。我的问题是,我怎样才能获得一些独特的 HTML 代码来显示每个轮子的结果。例如,如果轮子落在 Business 上,则一些文本会出现在轮子下方,其中包含有关 Business 的信息。

这是我的 HTML 代码,下面是一个工作示例的链接:

<!--[if IE]><script type="text/javascript" src="/sites/default/files/1010/source/excanvas.js"></script><![endif]-->
<input type="button" value="spin" onclick="spin();" style="float: left;" />
<canvas id="wheelcanvas" width="800" height="750"></canvas>
<script type="application/javascript">
  var colors = ["#B8D430", "#3AB745", "#029990", "#3501CB",
               "#2E2C75", "#673A7E", "#CC0071", "#F80120",
               "#F35B20", "#FB9A00", "#FFCC00", "#FEF200", "#B2DF00", "#5C8300"];
  var classes = ["Business", "Office Education", "Continuing Care Assistant", "Practical Nursing",
                     "Primary Care Paramedic", "Early Childhood Education", "Cooking", "Electrician",
                     "Heavy Equipment Operator", "Industrial Mechanic - Millwright", "Plumbing & Pipefitting", "Truck Driver Training", "Welding", "Power Engineering"];

  var startAngle = 0;
  var arc = Math.PI / 7;
  var spinTimeout = null;

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

  var ctx;

  function draw() {
    drawRouletteWheel();
  }

  function drawRouletteWheel() {
    var canvas = document.getElementById("wheelcanvas");
    if (canvas.getContext) {
      var outsideRadius = 300;
      var textRadius = 260;
      var insideRadius = 100;

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


      ctx.strokeStyle = "black";
      ctx.lineWidth = 2;

      ctx.font = 'bold 10px sans-serif';

      for(var i = 0; i < 14; i++) {
        var angle = startAngle + i * arc;
        ctx.fillStyle = colors[i];

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

        ctx.save();
        ctx.shadowOffsetX = -1;
        ctx.shadowOffsetY = -1;
        ctx.shadowBlur    = 0;
        ctx.shadowColor   = "rgb(220,220,220)";
        ctx.fillStyle = "black";
        ctx.translate(400 + Math.cos(angle + arc / 2) * textRadius, 400 + Math.sin(angle + arc / 2) * textRadius);
        ctx.rotate(angle + arc / 2 + Math.PI / 2);
        var text = classes[i];
        ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
        ctx.restore();
      } 

      //Arrow
      ctx.fillStyle = "black";
      ctx.beginPath();
      ctx.moveTo(400 - 4, 400 - (outsideRadius + 5));
      ctx.lineTo(400 + 4, 400 - (outsideRadius + 5));
      ctx.lineTo(400 + 4, 400 - (outsideRadius - 5));
      ctx.lineTo(400 + 9, 400 - (outsideRadius - 5));
      ctx.lineTo(400 + 0, 400 - (outsideRadius - 13));
      ctx.lineTo(400 - 9, 400 - (outsideRadius - 5));
      ctx.lineTo(400 - 4, 400 - (outsideRadius - 5));
      ctx.lineTo(400 - 4, 400 - (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);
  }

  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.font = 'bold 30px sans-serif';
    var text = classes[index]
    ctx.fillText(text, 400 - ctx.measureText(text).width / 2, 400 + 10);
    ctx.restore();
    document.getElementById('wheelResult').innerHTML = 'Test' + text;
  }

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

  draw();
</script>

<p id="wheelResult"></p>

http://www.ctrc.sk.ca/wheel.html

4

1 回答 1

0

有几种方法可以做到这一点,但这是我要做的方式。您可以使用 display:none 的 css 样式创建一个 div 标签,然后使用所需的信息对其进行设置,并根据需要隐藏和显示它。

例子:

<script>
    var htmlData = ['<h1>Business</h1><br><span>What is Business?</span><br>...', ...];
    ....
    function spin()
    {
        //Hide description box.
        document.getElementById('idDescriptionBox').style.display = 'none';

        spinAngleStart = Math.random() * 10 + 10;
        ....
    }
    ....
    function stopRotateWheel()
    {
        ....
        //Set description and display it.
        var descriptionBox = document.getElementById('idDescriptionBox');
        descriptionBox.innerHTML = htmlData[index];
        descriptionBox.style.display = 'block';
    }
    ....
</script>
<div id="idDescriptionBox" style="display:none;">
    <!--Description info in here-->
    ....
</div>
于 2013-04-03T17:18:59.203 回答