javascript HTML5 Canvas實(shí)現(xiàn)圓盤抽獎(jiǎng)功能
我們經(jīng)常參加各種電商優(yōu)惠活動(dòng),比如購買達(dá)到一定數(shù)額進(jìn)行抽獎(jiǎng)活動(dòng),在比如微信抽獎(jiǎng),淘寶抽獎(jiǎng),迅雷賺錢寶圓盤抽獎(jiǎng)活動(dòng)等。這些抽獎(jiǎng)活動(dòng)部分就是由HTML5的Canvas來制作的,今天就為大家分享一下如何使用HTML5的Canvas來制作圓盤抽獎(jiǎng)功能。老規(guī)矩,先看下效果圖吧:
再來看看Canvas的幾個(gè)主要api:
全部源代碼如下:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>HTML5 Canvas圓盤抽獎(jiǎng)應(yīng)用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(){ //旋轉(zhuǎn)角度 var angles; //可抽獎(jiǎng)次數(shù) var clickNum = 5; //旋轉(zhuǎn)次數(shù) var rotNum = 0; //中獎(jiǎng)公告 var notice = null; //轉(zhuǎn)盤初始化 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) { //可抽獎(jiǎng)次數(shù)減一 clickNum = clickNum-1; //轉(zhuǎn)盤旋轉(zhuǎn) runCup(); //轉(zhuǎn)盤旋轉(zhuǎn)過程“開始抽獎(jiǎng)”按鈕無法點(diǎn)擊 $('#tupBtn').attr("disabled", true); //旋轉(zhuǎn)次數(shù)加一 rotNum = rotNum + 1; //“開始抽獎(jiǎng)”按鈕無法點(diǎn)擊恢復(fù)點(diǎn)擊 setTimeout(function(){ alert(notice); $('#tupBtn').removeAttr("disabled", true); },6000); } else{ alert("親,抽獎(jiǎng)次數(shù)已用光!"); } }); //轉(zhuǎn)盤旋轉(zhuǎn) 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); } //各獎(jiǎng)項(xiàng)對(duì)應(yīng)的旋轉(zhuǎn)角度及中獎(jiǎng)公告內(nèi)容 function probability(){ //獲取隨機(jī)數(shù) 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]; } } //繪制轉(zhuǎn)盤 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;//扇形的終止弧度 //畫一個(gè)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(); } } //各獎(jiǎng)項(xiàng) 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('抽獎(jiǎng)',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> <!-- 更改系統(tǒng)默認(rèn)彈窗 --> <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\">[中獎(jiǎng)提醒]</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
詳解JavaScript設(shè)計(jì)模式中的享元模式
享元模式是一種用于性能優(yōu)化的模式。享元模式的核心是運(yùn)用共享技術(shù)來有效支持大量細(xì)粒度的對(duì)象.如果系統(tǒng)中創(chuàng)建了大量類似的對(duì)象而導(dǎo)致內(nèi)存占用過高,本文通過介紹書中文件上傳的優(yōu)化案例來說明享元模式的使用方式和作用,需要的朋友可以參考下2023-06-06利用ASP發(fā)送和接收XML數(shù)據(jù)的處理方法與代碼
利用ASP發(fā)送和接收XML數(shù)據(jù)的處理方法與代碼...2007-11-11js實(shí)現(xiàn)網(wǎng)頁多級(jí)級(jí)聯(lián)菜單代碼
這篇文章主要介紹了js實(shí)現(xiàn)網(wǎng)頁多級(jí)級(jí)聯(lián)菜單代碼,涉及javascript基于數(shù)組動(dòng)態(tài)構(gòu)造多級(jí)級(jí)聯(lián)菜單的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08Javascript 中的 call 和 apply使用介紹
JavaScript 中通過call或者apply用來代替另一個(gè)對(duì)象調(diào)用一個(gè)方法,將一個(gè)函數(shù)的對(duì)象上下文從初始的上下文改變?yōu)橛?thisObj 指定的新對(duì)象2012-02-02JS實(shí)現(xiàn)拖動(dòng)滾動(dòng)條評(píng)分的效果代碼分享
本文給大家基于js實(shí)現(xiàn)拖動(dòng)滾動(dòng)條評(píng)分效果,在項(xiàng)目開發(fā)中經(jīng)??梢杂玫降?,大家可以更加需要適當(dāng)?shù)奶砑有薷?,?duì)js評(píng)分效果感興趣的朋友一起看看吧2016-09-09javascript寫一個(gè)ajax自動(dòng)攔截并下載數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了javascript寫一個(gè)ajax自動(dòng)攔截并下載數(shù)據(jù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09