龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

Jscex和HTML 5制作的抽奖小程序(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
代码部分: !DOCTYPEhtmlPUBLIC-//W3C//DTDXHTML1.0Transitional//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html lang = 'zh-CN' xml:lang = 'zh-CN' xmlns = 'http://www.w3.org/1999/xhtm

代码部分:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
  5. <meta http-equiv="Content-Language" content="zh-CN"/> 
  6. <meta name="robots" content="index, follow" /> 
  7. <title>开源中国 - 抽奖小程序</title> 
  8. <script type="text/javascript" src="http://www.oschina.net/js/2012/jquery-1.7.1.min.js"></script> 
  9. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex.js"></script> 
  10. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex-parser.js"></script> 
  11. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex-jit.js"></script> 
  12. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex-builderbase.js"></script> 
  13. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex-async.js"></script> 
  14. <script type="text/javascript" src="http://repository.jscex.info/master/src/jscex-async-powerpack.js"></script> 
  15. <style> 
  16. * {font-size:12pt;font-weight:bold;}  
  17. body {margin: 5px;}  
  18. form {margin:0 0 20px 0;}  
  19. input {text-align:center;}  
  20. </style> 
  21. </head> 
  22. <body> 
  23. <form> 
  24. 共有 100 个会员,抽取 <input type='text' id='c_count' name='count' value='10' size='4'/> 名幸运会员  
  25. <input type='button' id='btn_begin' value='开始抽奖' onclick='begin_chose()'/> 
  26. </form> 
  27. <canvas id="myCanvas">Fallback content, in case the browser does not support Canvas.</canvas> 
  28. </body> 
  29.  
  30. <script type='text/javascript'> 
  31. var PERSON_COUNT = 100;  
  32. var brick_width = 40;  
  33. var brick_height = 22;  
  34. var col_count = 0;  
  35. var results=new Array();  
  36. var canvas = document.getElementById('myCanvas');  
  37. var ctx = canvas.getContext('2d');  
  38. var sound1,sound2,sound3;  
  39. resize_canvas();  
  40. draw_bricks();  
  41. load_songs();  
  42.  
  43. function resize_canvas(){  
  44.     var cWidth = document.body.clientWidth - 10;  
  45.     var cHeight = (PERSON_COUNT / (cWidth / (brick_width+10))) * (brick_height+10) + 50;  
  46.     if (canvas.width  < cWidth)  
  47.         canvas.width  = cWidth;      
  48.     if (canvas.height < cHeight)  
  49.         canvas.height = cHeight;     
  50. }  
  51.  
  52. function load_songs(){  
  53.     sound1 = new Audio("http://www.oschina.net/uploads/e.wav");  
  54.     sound1.load();  
  55.     sound2 = new Audio("http://www.oschina.net/uploads/e2.wav");  
  56.     sound2.load();  
  57.     sound3 = new Audio("http://www.oschina.net/uploads/e3.wav");  
  58.     sound3.load();  
  59. }  
  60.  
  61. function draw_bricks(){  
  62.     var x = 0,y = 0;  
  63.     var tmp_c_count = 0;  
  64.     for(c = 0; c < PERSON_COUNT; c++){  
  65.         ctx.fillStyle="#40AA53";  
  66.         ctx.fillRect(x, y, brick_width, brick_height);        
  67.         ctx.fillStyle="#fff";  
  68.         ctx.fillText(c+1,x+10,y+brick_height-7);  
  69.         x += (brick_width + 10);  
  70.         tmp_c_count ++;  
  71.         if((x + brick_width) > canvas.width){  
  72.             y += (brick_height + 10);  
  73.             x = 0;  
  74.             if(col_count == 0)  
  75.                 col_count = tmp_c_count;  
  76.         }  
  77.     }  
  78. }  
  79.  
  80. function begin_chose(){  
  81.     $('#btn_begin').attr('disabled',true);  
  82.     var nc = parseInt($('#c_count').val());  
  83.     beginAsync(nc).addEventListener("success", function () {  
  84.         sound3.play();  
  85.         $('#btn_begin').removeAttr("disabled");       
  86.     }).start();  
  87. }  
  88.  
  89. var beginAsync = eval(Jscex.compile("async", function(num_count){  
  90.     do{  
  91.         var thisV = $await(drawAsync(20));  
  92.         if(jQuery.inArray(thisV, results)<0){  
  93.             results.push(thisV);  
  94.             sound2.play();    
  95.             if(results.length >= num_count)  
  96.                 break;  
  97.         }  
  98.     }while(true);  
  99. }));  
  100.  
  101. var drawAsync = eval(Jscex.compile("async", function (ccount) {  
  102.     var last = -1;  
  103.     var r;  
  104.     for(oc=0;oc<ccount;oc++){  
  105.         r = Math.floor(Math.random() * PERSON_COUNT) + 1;//0-PERSON_COUNT         
  106.         if(jQuery.inArray(r, results)>=0)  
  107.             continue;  
  108.         var x = ((r - 1) % col_count) * (brick_width + 10);  
  109.         var y = (Math.ceil(r / col_count)-1) * (brick_height + 10);  
  110.         //alert("r="+r+",x="+x+",y="+y);  
  111.         //绘制高亮显示          
  112.         ctx.fillStyle="#F00";  
  113.         ctx.fillRect(x, y, brick_width, brick_height);        
  114.         ctx.fillStyle="#fff";  
  115.         ctx.fillText(r,x+10,y+brick_height-7);  
  116.         //擦除之前的高亮显示  
  117.         if(last > 0){  
  118.             var x = ((last - 1) % col_count) * (brick_width + 10);  
  119.             var y = (Math.ceil(last / col_count)-1) * (brick_height + 10);  
  120.             //绘制高亮显示          
  121.             ctx.fillStyle="#40AA53";  
  122.             ctx.fillRect(x, y, brick_width, brick_height);        
  123.             ctx.fillStyle="#fff";  
  124.             ctx.fillText(last,x+10,y+brick_height-7);  
  125.         }  
  126.         last = r;  
  127.         sound1.play();  
  128.         $await(Jscex.Async.sleep(100)); // 暂停10毫秒  
  129.     }  
  130.     return r;  
  131. }));  
  132. </script> 
  133. </html> 

lottery.html ~ 4KB 下载

http://www.oschina.net/code/snippet_12_11478

  1. JavaScript+CSS3写的Web版纸牌
精彩图集

赞助商链接