这周有点迷茫,不知道干嘛了,一天天就过去了!我在博客右侧公告栏加了qq交流,各位有好的主题,或者有趣的技术,欢迎交流!今天突发奇想,就写了2个h5 canvas的demo玩玩!

demo一:刮刮乐

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

布局

<div id="lottery" style="width:300px;height:500px;margin:10px;border-radius:5px;float:left;">
<div style="width:300px;height:100px;line-height:100px;text-align:center;font-size:33px;color:blueviolet;">NICK彩票</div>
<div id="txt" style="width:300px;height:200px;font-size:40px;color:peachpuff;display:flex;justify-content:center;align-items:center;flex-direction:column;">
<span>祝</span>
<span>君</span>
<span>中</span>
<span>奖</span>
</div>
<div id="canvasArea" style="width:300px;height:200px;position:relative;">
<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>
<canvas id="canvas" width="300px" height="200px" style="position:absolute;top:0;left:0;z-index:2;"></canvas>
</div>
</div>

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

  1. flex布局,将‘祝君中奖’垂直居中,目前还有兼容问题,不过看我们大前端的发展趋势,应该很快就能搞定了;
  2. 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);
  1. 填充颜色;
  2. 绘制实心矩形;
  3. 设置字体颜色;
  4. 设置字体大小类型;
  5. 绘制实心字体。

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

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

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

刮奖功能函数

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

为canvas元素onmousedown和onmouseup事件

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

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

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

demo二:画笔

布局

     <div style="width:300px;height:500px;margin:10px;border-radius:10px;overflow:hidden;float:right;">
<canvas id="canvas2" width="300px" height="500px" style=""></canvas>
</div>

设置canvas画布

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

画笔功能函数

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

为canvas元素onmousedown和onmouseup事件

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

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

附上完整代码:

 

  代码写完了,我也想说点其他的:

  上面js代码中,有不少注释,我将其分为几个区域:插件方法封装区、命名区、功能实现区、刮刮乐区以及画笔区等,我感觉这样写加上一些注释,能使代码能加简洁,便于以后的维护!当然这只是个人观点,欢迎各位点击我博客右边公告栏的qq交流交流!

最后附上:

上篇博文:事件绑定与解绑!(只是一个简单的封装)

来看看效果,玩玩吧!

最新文章

  1. gitlab使用有感之坚持
  2. Fedora20安装fcitx输入法
  3. IT客学院《构建高转化率的着陆页-PS+HTML+网络营销》共25节【价值199元】无水印版
  4. liunx下tomcat启动报错
  5. 给jdk写注释系列之jdk1.6容器(5)-LinkedHashMap源码解析
  6. web2py--------------用web2py写 django的例子 --------建立一个投票应用(2)
  7. param
  8. 组合数处理(逆元求解)...Orz
  9. MMA8451重力加速度计通过写内部校准寄存器进行校准
  10. Oracle 查询重复索引列
  11. 新版Ubuntu安装日文输入法
  12. linux服务管理 服务启动和自启动
  13. Linux 第十二天
  14. Linux内核读书笔记第四周
  15. TP5.1:request请求对象(使用四种方式获取)
  16. 高性能JavaScript(字符串和正则表达式)
  17. ctcms Nginx 伪静态
  18. vue总结07 常用插件
  19. 神之编辑器emacs
  20. linux 版本查询

热门文章

  1. MATLAB符号运算
  2. leetcode 94 Binary Tree Inorder Traversal ----- java
  3. HDU 1231 最大连续子序列 --- 入门DP
  4. POJ3308 Paratroopers(网络流)(最小割)
  5. 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach
  6. linux如何查看磁盘剩余空间
  7. 归档-ios
  8. Linux 用户态和内核态
  9. ABBYY是怎么自定义主窗口的
  10. Mozilla Brick:一个Web组件Polyfill库