我需要在Javascript中减慢轮盘赌的速度

时间:2016-01-31 14:29:11

标签: javascript html5 html5-canvas

我有一个功课,我必须在没有插件的javascript中制作轮盘旋转轮代码,我需要一些条件才能使用此代码 clearTimeout(spinTimeout);
但我不知道如何编码轮盘旋转的减速速度。 最后一个问题,我需要在轮盘中随机颜色的颜色

这是我的代码:

<h1>
<p></p>
<p>
	
  <input type="button" value="spin" onmousedown="spin();" onmouseup="stopRotateWheel();" style="float: left;" />

  <canvas id="wheelcanvas" width="500" height="500"></canvas>
  <script type="application/javascript">
 
  var prize = ["prize1", "prize2", "prize3", "prize4",
                     "prize5", "prize6", "prize7", "prize8",
                     "prize9", "prize10", "Article11", "Article12"];
  
  var startAngle = 0;
  var arc = Math.PI / 6;
  var spinTimeout =null;
  
  var spinArcStart = 10;
  var spinTime = 0;
  
  var ctx;
  var x = 100;
  var i = 30;

  spinpower=false;
  function Random_Color()
	{
		var colors = ["#C137BD", "#D74C76", "#BD3B47", "#DC4345", "#F69939",
		"#FCC334", "#D3DC5D", "#5DDF67", "#3ABF32", "#3DA053",
		"#3DA0A0", "#5A7CC3", "#5792EE", "#6E4ECE", "#B53CB1"
	];
	
		var random_color = colors[Math.floor(Math.random() * colors.length)];
		return random_color;
	}
  function draw() {
    drawRouletteWheel();
  }
  
  function drawRouletteWheel() {
    var canvas = document.getElementById("wheelcanvas");
    if (canvas.getContext) {
      var outsideRadius = 200;
      var textRadius = 160;
      var insideRadius = 125;
      
      ctx = canvas.getContext("2d");
      ctx.clearRect(0,0,500,500);
      
      
      ctx.strokeStyle = "black";
      ctx.lineWidth = 2;
      
      ctx.font = 'bold 15px sans-serif';
      
      for(var i = 0; i < 12; i++) {
        var angle = startAngle + i * arc;
        ctx.fillStyle = Random_Color();
        
        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.shadowOffsetX = -1;
        ctx.shadowOffsetY = -1;
        ctx.shadowBlur    = 0;
        ctx.shadowColor   = "rgb(220,220,220)";
        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 / 2);
        var text = prize[i];
        ctx.fillText(text, -ctx.measureText(text).width / 2, 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() {
	var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
      startAngle += (spinAngle * Math.PI / 180);
    drawRouletteWheel()
    
	x--;
	spinTimeout = setTimeout('rotateWheel()',x);
    if (x<=-10){
     document.getElementById('welcomeDiv').style.display = "block";
   }
   
  }
 

  function stopRotateWheel() {
	x=100;
	document.getElementById('welcomeDiv').style.display = "none";
	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 12px sans-serif';
    var text = prize[index]
    ctx.fillText(text, 250 - ctx.measureText(text).width / 2, 250 + 10);
	window.location = "navto://"+prize[index]+"_stack";
	
    ctx.restore();
  }
  
  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>
<p>&nbsp;</p>
  <div id="welcomeDiv"  style="font-size:20;display:none;"  > MAX SPIN</div>

1 个答案:

答案 0 :(得分:0)

听起来像你已经用Google搜索了一个缓和功能,并且不知道如何使用它。

Robert Penner提供了一套很好的缓动功能,可以在这里找到:

https://github.com/danro/jquery-easing/blob/master/jquery.easing.js

以下是使用Penner缓动功能的快速课程

以下是Penner缓动函数的参数:

// d: total duration (==# steps desired to complete the animation)
// t: current step# inside duration, 
// b: beginning value,
// c: total change from beginning value,

您可以设置如下参数:

// b == the beginning value
// start your wheel at 0 angle
var b=0; 

// c == the total change that will occur to the beginning value
// spin the wheel one full rotation (PI*2) plus a random final angle (Math.random()*PI*2)
var PI2=Math.PI*2;
var c=PI2+Math.random()*PI2;

// d == duration == the number of loops desired to complete the animaton
// have the spin take 120 animation loops to complete
// If each animation loop is 1/60th second then the full animation takes 2 seconds
var d=120;

// t == the current step of the animation
var t=0; // start at step#0
// each loop will advance "t" towards "d"
t++;  // make t advance towards d 

要制作动画:

  1. 创建一个动画循环(最好是requestAnimationFrame,但如果必须,则为setTimeout
  2. 计算当前值:var currentAngle=someEasing(t,b,c,d)
  3. 绘制在currentAngle
  4. 处旋转的滚轮
  5. 通过循环增加下一次的步数:t++
  6. 由于您处于学习模式......

    ...我将实际编码留给你。