搜尋
首頁web前端js教程javascript HTML5 Canvas實作圓盤抽獎功能_javascript技巧

我們常參加各種電商優惠活動,例如購買達到一定金額進行抽獎活動,在例如微信抽獎,淘寶抽獎,迅雷賺錢寶圓盤抽獎活動等。這些抽獎活動部分就是由HTML5的Canvas來製作的,今天就為大家分享如何使用HTML5的Canvas來製作圓盤抽獎功能。老規矩,先看下效果圖:

再來看看Canvas的幾個主要api:

 

全部原始碼如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas圆盘抽奖应用DEMO演示</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<style>
*{padding: 0px;margin: 0px;font-size: 16px;font-family: "Microsoft YaHei";}
.xttblog_box{width: 300px;height: 300px;margin: 100px auto;position: relative; }
.xttblog_box canvas{position: absolute;}
#xttblog{background-color: white;border-radius: 100%;}
#xttblog01,#xttblog03{left: 50px;top: 50px;z-index: 30;}
#xttblog02{left: 75px;top: 75px;z-index: 20;}
#xttblog{-o-transform: transform 6s;-ms-transform: transform 6s;-moz-transform: transform 6s;  
-webkit-transform: transform 6s;transition: transform 6s;-o-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;-moz-transform-origin: 50% 50%;-webkit-transform-origin: 50% 50%;
transform-origin: 50% 50%;}
.taoge_btn{width: 60px;height: 60px;left: 120px;top: 120px;border-radius: 100%;
position: absolute;cursor: pointer;border: none;background: transparent;
outline: none;z-index: 40;}
</style>
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
 //旋转角度
 var angles;
 //可抽奖次数
 var clickNum = 5;
 //旋转次数
 var rotNum = 0;
 //中奖公告
 var notice = null;
 //转盘初始化
 var color = ["#626262","#787878","rgba(0,0,0,0.5)","#DCC722","white","#FF4350"];
 var info = ["谢谢参与"," 1000"," 10"," 500"," 100"," 4999"," 1"," 20"];
 var info1 = ['再接再厉','  元','  元',' 淘金币','  元',' 淘金币','  元',' 淘金币']
 canvasRun();
 $('#tupBtn').bind('click',function(){
  if (clickNum >= 1) {
   //可抽奖次数减一
   clickNum = clickNum-1;
   //转盘旋转
   runCup();
   //转盘旋转过程“开始抽奖”按钮无法点击
   $('#tupBtn').attr("disabled", true);
   //旋转次数加一
   rotNum = rotNum + 1;
   //“开始抽奖”按钮无法点击恢复点击
   setTimeout(function(){
    alert(notice);
    $('#tupBtn').removeAttr("disabled", true);
   },6000);
  }
  else{
   alert("亲,抽奖次数已用光!");
  }
 });
 
 //转盘旋转
 function runCup(){
  probability();
  var degValue = 'rotate('+angles+'deg'+')';
  $('#xttblog').css('-o-transform',degValue);   //Opera
  $('#xttblog').css('-ms-transform',degValue);   //IE浏览器
  $('#xttblog').css('-moz-transform',degValue);   //Firefox
  $('#xttblog').css('-webkit-transform',degValue);  //Chrome和Safari
  $('#xttblog').css('transform',degValue);
 }
 
 //各奖项对应的旋转角度及中奖公告内容
 function probability(){
  //获取随机数
  var num = parseInt(Math.random()*(7 - 0 + 0) + 0);
  //概率
  if ( num == 0 ) {
   angles = 2160 * rotNum + 1800;
   notice = info[0] + info1[0];
  }
  //概率
  else if ( num == 1 ) {
   angles = 2160 * rotNum + 1845;
   notice = info[7] + info1[7];
  }
  //概率
  else if ( num == 2 ) {
   angles = 2160 * rotNum + 1890;
   notice = info[6] + info1[6];
  }
  //概率
  else if ( num == 3 ) {
   angles = 2160 * rotNum + 1935;
   notice = info[5] + info1[5];
  }
  //概率
  else if ( num == 4 ) {
   angles = 2160 * rotNum + 1980;
   notice = info[4] + info1[4];
  }
  //概率
  else if ( num == 5 ) {
   angles = 2160 * rotNum + 2025;
   notice = info[3] + info1[3];
  }
  //概率
  else if ( num == 6 ) {
   angles = 2160 * rotNum + 2070;
   notice = info[2] + info1[2];
  }
  //概率
  else if ( num == 7 ) {
   angles = 2160 * rotNum + 2115;
   notice = info[1] + info1[1];
  }
 }
 
 //绘制转盘
 function canvasRun(){
  var canvas=document.getElementById('xttblog');
  var canvas01=document.getElementById('xttblog01');
  var canvas03=document.getElementById('xttblog03');
  var canvas02=document.getElementById('xttblog02');
  var ctx=canvas.getContext('2d');
  var ctx1=canvas01.getContext('2d');
  var ctx3=canvas03.getContext('2d');
  var ctx2=canvas02.getContext('2d');
  createCircle();
  createCirText();
  initPoint();
  
  //外圆
  function createCircle(){
   var startAngle = 0;//扇形的开始弧度
   var endAngle = 0;//扇形的终止弧度
   //画一个8等份扇形组成的圆形
   for (var i = 0; i< 8; i++){
    startAngle = Math.PI*(i/4-1/8);
    endAngle = startAngle+Math.PI*(1/4);
    ctx.save();
    ctx.beginPath();
    ctx.arc(150,150,100, startAngle, endAngle, false);
    ctx.lineWidth = 120;
    if (i%2 == 0) {
     ctx.strokeStyle = color[0];
    }else{
     ctx.strokeStyle = color[1];
    }
    ctx.stroke();
    ctx.restore();
   }
  }
 
  //各奖项
  function createCirText(){ 
   ctx.textAlign='start';
   ctx.textBaseline='middle';
   ctx.fillStyle = color[3];
   var step = 2*Math.PI/8;
   for ( var i = 0; i < 8; i++) {
    ctx.save();
    ctx.beginPath();
    ctx.translate(150,150);
    ctx.rotate(i*step);
    ctx.font = " 20px Microsoft YaHei";
    ctx.fillStyle = color[3];
    ctx.fillText(info[i],-30,-115,60);
    ctx.font = " 14px Microsoft YaHei";
    ctx.fillText(info1[i],-30,-95,60);
    ctx.closePath();
    ctx.restore();
   }
  }
 
  function initPoint(){
   //箭头指针
   ctx1.beginPath();
   ctx1.moveTo(100,24);
   ctx1.lineTo(90,62);
   ctx1.lineTo(110,62);
   ctx1.lineTo(100,24);
   ctx1.fillStyle = color[5];
   ctx1.fill();
   ctx1.closePath();
   //中间小圆
   ctx3.beginPath();
   ctx3.arc(100,100,40,0,Math.PI*2,false);
   ctx3.fillStyle = color[5];
   ctx3.fill();
   ctx3.closePath();
   //小圆文字
   ctx3.font = "Bold 20px Microsoft YaHei";
   ctx3.textAlign='start';
   ctx3.textBaseline='middle';
   ctx3.fillStyle = color[4];
   ctx3.beginPath();
   ctx3.fillText('开始',80,90,40);
   ctx3.fillText('抽奖',80,110,40);
   ctx3.fill();
   ctx3.closePath();
   //中间圆圈
   ctx2.beginPath();
   ctx2.arc(75,75,75,0,Math.PI*2,false);
   ctx2.fillStyle = color[2];
   ctx2.fill();
   ctx2.closePath();
  }
 }
});
</script>
</head>
<body>
<div class="xttblog_box">
 <canvas id="xttblog" width="300px" height="300px">抱歉!浏览器不支持。</canvas>
 <canvas id="xttblog01" width="200px" height="200px">抱歉!浏览器不支持。</canvas>
 <canvas id="xttblog03" width="200px" height="200px">抱歉!浏览器不支持。</canvas>
 <canvas id="xttblog02" width="150px" height="150px">抱歉!浏览器不支持。</canvas>
 <button id="tupBtn" class="taoge_btn"></button>
</div>
<!-- 更改系统默认弹窗 -->
<script type="text/javascript">
window.alert = function(str)
{
 var shield = document.createElement("DIV");
 shield.id = "shield";
 shield.style.position = "absolute";
 shield.style.left = "50%";
 shield.style.top = "50%";
 shield.style.width = "280px";
 shield.style.height = "150px";
 shield.style.marginLeft = "-140px";
 shield.style.marginTop = "-110px";
 shield.style.zIndex = "25";
 var alertFram = document.createElement("DIV");
 alertFram.id="alertFram";
 alertFram.style.position = "absolute";
 alertFram.style.width = "280px";
 alertFram.style.height = "150px";
 alertFram.style.left = "50%";
 alertFram.style.top = "50%";
 alertFram.style.marginLeft = "-140px";
 alertFram.style.marginTop = "-110px";
 alertFram.style.textAlign = "center";
 alertFram.style.lineHeight = "150px";
 alertFram.style.zIndex = "300";
 strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
 strHtml += " <li style=\"background:#626262;text-align:left;padding-left:20px;font-size:14px;font-weight:bold;height:25px;line-height:25px;border:1px solid #F9CADE;color:white\">[中奖提醒]</li>\n";
 strHtml += " <li style=\"background:#787878;text-align:center;font-size:12px;height:95px;line-height:95px;border-left:1px solid #F9CADE;border-right:1px solid #F9CADE;color:#DCC722\">"+str+"</li>\n";
 strHtml += " <li style=\"background:#626262;text-align:center;font-weight:bold;height:30px;line-height:25px; border:1px solid #F9CADE;\"><input type=\"button\" value=\"确 定\" onclick=\"doOk()\" style=\"width:80px;height:20px;background:#626262;color:white;border:1px solid white;font-size:14px;line-height:20px;outline:none;margin-top: 4px\"/></li>\n";
 strHtml += "</ul>\n";
 alertFram.innerHTML = strHtml;
 document.body.appendChild(alertFram);
 document.body.appendChild(shield);
 this.doOk = function(){
  alertFram.style.display = "none";
  shield.style.display = "none";
 }
 alertFram.focus();
 document.body.onselectstart = function(){return false;};
}
</script>
</body>
</html>

以上就是本文的全部內容,希望對大家的學習有所幫助。

原文網址:http://www.xttblog.com/?p=399

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
JavaScript數據類型:瀏覽器和nodejs之間是否有區別?JavaScript數據類型:瀏覽器和nodejs之間是否有區別?May 14, 2025 am 12:15 AM

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScript評論:使用//和 / * * / * / * /JavaScript評論:使用//和 / * * / * / * /May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript:開發人員的比較分析Python vs. JavaScript:開發人員的比較分析May 09, 2025 am 12:22 AM

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

Python vs. JavaScript:選擇合適的工具Python vs. JavaScript:選擇合適的工具May 08, 2025 am 12:10 AM

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript:了解每個的優勢Python和JavaScript:了解每個的優勢May 06, 2025 am 12:15 AM

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

JavaScript的核心:它是在C還是C上構建的?JavaScript的核心:它是在C還是C上構建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript應用程序:從前端到後端JavaScript應用程序:從前端到後端May 04, 2025 am 12:12 AM

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

Python vs. JavaScript:您應該學到哪種語言?Python vs. JavaScript:您應該學到哪種語言?May 03, 2025 am 12:10 AM

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用