当前位置:Gxlcms > html代码 > canvas刮刮乐和画笔

canvas刮刮乐和画笔

时间:2021-07-01 10:21:17 帮助过:40人阅读

舍不得买2块钱的刮刮乐,就只能写个类似的功能过过彩票瘾了!

布局

  1. <div id="lottery" style="width:300px;height:500px;margin:10px;background-color:lightskyblue;border-radius:5px;float:left;">
  2. <div style="width:300px;height:100px;line-height:100px;text-align:center;font-size:33px;color:blueviolet;">NICK彩票</div>
  3. <div id="txt" style="width:300px;height:200px;font-size:40px;color:peachpuff;display:flex;justify-content:center;align-items:center;flex-direction:column;">
  4. <span>祝</span>
  5. <span>君</span>
  6. <span>中</span>
  7. <span>奖</span>
  8. </div>
  9. <div id="canvasArea" style="width:300px;height:200px;position:relative;">
  10. <div style="width:300px;height:200px;position:absolute;top:0;left:0;z-index:1;text-align:center;line-height:200px;font-weight:bold;font-size:56px;color:indianred;">一等奖</div>
  11. <canvas id="canvas" width="300px" height="200px" style="position:absolute;top:0;left:0;z-index:2;"></canvas>
  12. </div>
  13. </div>

这段html要注意的地方有2个:

flex布局,将‘祝君中奖’垂直居中,目前还有兼容问题,不过看我们大前端的发展趋势,应该很快就能搞定了;

canvas和‘一等奖’div的z-index问题,将canvas的z-index设置较高,使其置于一等奖div上面。

设置canvas画布

var canvas = document.getElementById("canvas");

var context = canvas.getContext("2d");

绘制刮奖区域

context.fillStyle='#A9AB9D';
context.fillRect(10,10,280,180);
context.fillStyle='#000';
context.font='50px Arial';
context.fillText('刮奖区',75,115);

填充颜色;

绘制实心矩形;

设置字体颜色;

设置字体大小类型;

绘制实心字体。

以上都是canvas基础api,看w3c就ok了。

为了好看,我将‘祝君中奖’加个字体变色

  1. setInterval(function(){
  2. document.getElementById('txt').style.color =
  3. document.getElementById('txt').style.color=='
  4. peachpuff' ? 'yellow' : 'peachpuff';
  5. },500);

刮奖功能函数

  1. var brush=function(){//刮奖
  2. context.clearRect(event.offsetX,event.offsetY,20,20);
  3. };

为canvas元素onmousedown和onmouseup事件

  1. canvas.onmousedown = function(){
  2. // 鼠标按下时 - 绑定鼠标跟随事件
  3. bindHandler(canvas,'mousemove',brush,false);
  4. }
  5. canvas.onmouseup = function(){
  6. // 停止刮奖功能 - 解绑鼠标跟随事件
  7. removeHandler(canvas,"mousemove",brush,false);
  8. }

这里的事件绑定与解绑我上篇博文有封装,最后完整代码也有!


刮刮乐happy到底结束!最后附上完整代码,再看看效果吧!


demo二:画笔

布局

  1. <div style="width:300px;height:500px;margin:10px;border-radius:10px;overflow:hidden;float:right;">
  2. <canvas id="canvas2" width="300px" height="500px" style="background-color:lightblue;"></canvas>
  3. </div>

设置canvas画布

  1. var canvas2 = document.getElementById("canvas2");
  2. var context2 = canvas2.getContext("2d");

画笔功能函数

  1. var draw=function(){
  2. context2.fillRect(event.offsetX,event.offsetY,10,10);
  3. };

为canvas元素onmousedown和onmouseup事件

  1. context2.font='20px Arial';
  2. context2.strokeText('NICK画笔',100,30);//写个头
  3. //1. 为canvas元素onmousedown和onmouseup事件
  4. canvas2.onmousedown = function(){
  5. // 启用画笔功能 - 绑定鼠标跟随事件
  6. bindHandler(canvas2,'mousemove',draw,false);
  7. }
  8. canvas2.onmouseup = function(){
  9. // 停止画笔功能 - 解绑鼠标跟随事件
  10. removeHandler(canvas2,"mousemove",draw,false);
  11. }

画图工具的画笔功能到底结束!


附上完整代码:

  1. <title>Canvas lottery brush nick</title>
  2. <meta charset="utf-8">
  3. <div style="width:640px;margin:auto;">
  4. <!--刮刮乐-->
  5. <div id="lottery" style="width:300px;height:500px;margin:10px;background-color:lightskyblue;border-radius:5px;float:left;">
  6. <div style="width:300px;height:100px;line-height:100px;text-align:center;font-size:33px;color:blueviolet;">NICK彩票</div>
  7. <div id="txt" style="width:300px;height:200px;font-size:40px;color:peachpuff;display:flex;justify-content:center;align-items:center;flex-direction:column;">
  8. <span>祝</span>
  9. <span>君</span>
  10. <span>中</span>
  11. <span>奖</span>
  12. </div>
  13. <div id="canvasArea" style="width:300px;height:200px;position:relative;">
  14. <div style="width:300px;height:200px;position:absolute;top:0;left:0;z-index:1;text-align:center;line-height:200px;font-weight:bold;font-size:56px;color:indianred;">一等奖</div>
  15. <canvas id="canvas" width="300px" height="200px" style="position:absolute;top:0;left:0;z-index:2;"></canvas>
  16. </div>
  17. </div>
  18. <!--画图工具画笔功能-->
  19. <div style="width:300px;height:500px;margin:10px;border-radius:10px;overflow:hidden;float:right;">
  20. <canvas id="canvas2" width="300px" height="500px" style="background-color:lightblue;"></canvas>
  21. </div>
  22. </div>
  23. <div style="text-align:center;">
  24. <p>刮刮乐:鼠标按住不放,拖动开始刮奖!</p>
  25. <p>画笔:鼠标按住不放,拖动画画!</p>
  26. </div>

人气教程排行