We often participate in various e-commerce promotional activities, such as purchasing to a certain amount to conduct lottery activities, such as WeChat lottery, Taobao lottery, Thunder Money-making Bao disk lottery activities, etc. These raffles are partly made by HTML5 Canvas. Today I will share with you how to use HTML5 Canvas to create a disc raffle function. As usual, let’s look at the renderings first:
Let’s take a look at several of Canvas’ main APIs:
All source codes are as follows:
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8"><title>HTML5 Canvas Disc Lottery Application DEMO 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(){ //Rotation angle var angles; //Number of draws var clickNum = 5; //Number of rotation var rotNum = 0; //Winning announcement var notice = null; //Turn initialization var color = ["#626262","#787878","rgba(0,0,0,0.5)","#DCC722","white","#FF4350"]; var info = ["Thank you for your participation"," 1000"," 10"," 500"," 100"," 4999"," 1"," 20"]; var info1 = ['Still work hard',' Yuan',' Yuan',' Gold Coins',' Yuan',' Gold Coins'] canvasRun(); $('#tupBtn').bind('click',function(){ if (clickNum >= 1) { //The number of draws can be reduced by one clickNum = clickNum-1; //The turntable rotate runCup(); //The turntable rotated process "Start draw" button cannot be clicked $('#tupBtn').attr("disabled", true); //The number of rotations add one rotNum = rotNum + 1; //The "Start draw" button cannot be clicked to restore click setTimeout(function(){ alert(Notice); $('#tupBtn').removeAttr("disabled", true); },6000); } else{ alert("Dear, the number of draws has been used up!"); } }); //The turntable rotate function runCup(){ probability(); var degValue = 'rotate('+angles+'deg'+')'; $('#xttblog').css('-o-transform',degValue); //Opera $('#xttblog').css('-ms-transform',degValue); //IE browser $('#xttblog').css('-moz-transform',degValue); //Firefox $('#xttblog').css('-webkit-transform',degValue); //Chrome and Safari $('#xttblog').css('transform',degValue); } //The rotation angle corresponding to each prize and the content of the winning announcement function probability(){ //Get the random number var num = parseInt(Math.random()*(7 - 0 + 0) + 0); //Probability if ( num == 0 ) { angles = 2160 * rotNum + 1800; notice = info[0] + info1[0]; } //Probability else if ( num == 1 ) { angles = 2160 * rotNum + 1845; notice = info[7] + info1[7]; } //Probability else if ( num == 2 ) { angles = 2160 * rotNum + 1890; notice = info[6] + info1[6]; } //Probability else if ( num == 3 ) { angles = 2160 * rotNum + 1935; notice = info[5] + info1[5]; } //Probability else if ( num == 4 ) { angles = 2160 * rotNum + 1980; notice = info[4] + info1[4]; } //Probability else if ( num == 5 ) { angles = 2160 * rotNum + 2025; notice = info[3] + info1[3]; } //Probability else if ( num == 5 ) { angles = 2160 * rotNum + 2025; notice = info[3] + info1[3]; } //Probability else if ( num == 6 ) { angles = 2160 * rotNum + 2070; notice = info[2] + info1[2]; } //Probability else if ( num == 7 ) { angles = 2160 * rotNum + 2115; notice = info[1] + info1[1]; } } //Draw the turntable 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(); //External circle function createCircle(){ var startAngle = 0;//The start radian of the fan var endAngle = 0;//The end radian of the fan//Draw a circle composed of 8 equal parts of the fan 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(); } } //Previous awards 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(){ //arrow pointer 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(); //The middle circle ctx3.beginPath(); ctx3.arc(100,100,40,0,Math.PI*2,false); ctx3.fillStyle = color[5]; ctx3.fill(); ctx3.closePath(); //The small circle text ctx3.font = "Bold 20px Microsoft YaHei"; ctx3.textAlign='start'; ctx3.textBaseline='middle'; ctx3.fillStyle = color[4]; ctx3.beginPath(); ctx3.fillText('Start',80,90,40); ctx3.fillText('Release',80,110,40); ctx3.fill(); ctx3.closePath(); //Middle circle ctx2.beginPath(); ctx2.arc(75,75,75,0,Math.PI*2,false); ctx2.fillStyle = color[2]; ctx2.fill(); ctx2.closePath(); } }});</script></head><body><div> <canvas id="xttblog">Sorry! The browser does not support it. </canvas> <canvas id="xttblog01">Sorry! The browser does not support it. </canvas> <canvas id="xttblog03">Sorry! The browser does not support it. </canvas> <canvas id="xttblog02">Sorry! The browser does not support it. </canvas> <button id="tupBtn"></button></div><!-- Change the system default pop-up window--><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/">[winning reminder]</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=/" OK/" 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>The above is all about this article, I hope it will be helpful to everyone's learning.
Original address: http://www.xttblog.com/?p=399