WeChat 복권, Taobao 복권, Xunlei Money Making 디스크 복권 등 구매 금액이 일정 금액에 도달하면 복권 활동과 같은 다양한 전자상거래 우대 활동에 자주 참여합니다. 이러한 복권 활동은 HTML5 Canvas에서 부분적으로 생성됩니다. 오늘은 HTML5 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>
위 내용은 이 글의 전체 내용입니다. 모든 분들의 공부에 도움이 되었으면 좋겠습니다.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

WebStorm Mac 버전
유용한 JavaScript 개발 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.
