當前位置:編程學習大全網 - 網站源碼 - 怎麽用html5實現 抽獎效果

怎麽用html5實現 抽獎效果

這個需要用到H5新標簽:canvas繪制圖形,利用js來實現抽獎效果,實現步驟如下:

var num = 6; // 獎品數量 var canvas = document.getElementById('canvas'); var

btn = document.getElementById('btn'); if(!canvas.getContext){

alert('抱歉!瀏覽器不支持。'); return; } // 獲取繪圖上下文 var ctx = canvas.getContext('2d'); for

(var i = 1; i <= num; i++) { // 保存當前狀態 ctx.save(); // 開始壹條新路徑

ctx.beginPath(); // 位移到圓心,下面需要圍繞圓心旋轉 ctx.translate(150, 150); // 從(0,

0)坐標開始定義壹條新的子路徑 ctx.moveTo(0, 0); // 旋轉弧度,需將角度轉換為弧度,使用 degrees * Math.PI/180

公式進行計算。 ctx.rotate(360 / num * i * Math.PI/180); // 繪制圓弧 ctx.arc(0, 0, 150, 0, 2

* Math.PI / num, false); if (i % 2 == 0) { ctx.fillStyle = '#ffb820'; }else{

ctx.fillStyle = '#ffcb3f'; } // 填充扇形 ctx.fill(); // 繪制邊框 ctx.lineWidth = 0.5;

ctx.strokeStyle = '#f48d24'; ctx.stroke(); // 恢復前壹個狀態 ctx.restore(); }

  • 上一篇:彩虹之上提單可以在網上使用嗎
  • 下一篇:JSP+AJAX開發在線聊天室
  • copyright 2024編程學習大全網