本来想的挺复杂实际操作了一下15分钟完成了,挺简单的。

预览地址:http://dtdxrk.github.io/game/blackboard/index.html

分享一下思路:

1.创建画布

2.添加按钮

3.设置事件

没啥好说的就这样吧。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>blackboard</title>
<style>
*{margin: 0;padding: 0;}
#wapper{margin:50px auto 0;display: block;}
#wapper:hover{cursor: pointer;}
.btn-list{width: 810px;margin: 5px auto;}
.btn{padding:5px 10px;margin-right: 10px;}
</style>
</head>
<body> <canvas id="wapper" width="800" height="400"></canvas>
<!-- btn -->
<div class="btn-list">chalkColor : <button class="btn" id="btn-white">white</button><button class="btn" id="btn-pink">pink</button><button class="btn" id="btn-green">green</button></div>
<div class="btn-list">lineWidth : <button class="btn" id="btn-5">5</button><button class="btn" id="btn-10">10</button><button class="btn" id="btn-15">15</button></div>
<div class="btn-list">clearAll : <button class="btn" id="btn-clearall">clearAll</button></div>
<!-- btn end-->
<script> var blackboard = {
color:'#fff',
lineWidth:5,
isDrawing:false,
x:0,
y:0,
init:function(){
this.wapper = document.getElementById('wapper');
this._2d = this.wapper.getContext('2d');
this.clear_blackboard();
this.init_font();
this.bind_btnEvent();
},
clear_blackboard:function(){
/*绘制背景颜色*/
this._2d.beginPath();
this._2d.fillStyle = '#000';/*设置颜色*/
this._2d.fillRect(0,0,800,600);/*fillRect(起点x,起点y,矩形长,矩形宽)*/
},
init_font:function(){
this._2d.beginPath();
this._2d.font='100px Arial';
this._2d.fillStyle = '#fff';
this._2d.textAlign = 'center';
this._2d.fillText('blackboard',400,200);/*strokeText(text,x,y,max width)*/
},
bind_btnEvent:function(){
var that = this; that.wapper.onmouseout = function(e){
that.isDrawing = false;
} that.wapper.onmousemove = function(e){
if(that.isDrawing){
that.drawing(e.offsetX, e.offsetY);
that.x = e.offsetX;
that.y = e.offsetY;
}
} that.wapper.onmouseup = function(e){
that.isDrawing = false;
return false;
} that.wapper.onmousedown = function(e){
that.x = e.offsetX;
that.y = e.offsetY;
that.isDrawing = true;
return false;
} /*btn-clearall*/
document.getElementById('btn-clearall').onclick = function(){
that.clear_blackboard();
} document.getElementById('btn-white').onclick = function(){
that.color = 'white';
} document.getElementById('btn-pink').onclick = function(){
that.color = 'pink';
} document.getElementById('btn-green').onclick = function(){
that.color = 'green';
} document.getElementById('btn-5').onclick = function(){
that.lineWidth = 5;
} document.getElementById('btn-10').onclick = function(){
that.lineWidth = 10;
} document.getElementById('btn-15').onclick = function(){
that.lineWidth = 15;
}
},
drawing:function(x,y){
this._2d.beginPath();/*创建新的路径*/
this._2d.lineWidth = this.lineWidth;/*设置线条宽度*/
this._2d.strokeStyle = this.color;/*设置线条颜色*/
this._2d.lineCap = 'round';/*向线条的每个末端添加圆形线帽。*/
this._2d.moveTo(this.x, this.y);/*将画笔光标移动到画布坐标10,10*/
this._2d.lineTo(x,y);/*画一条线*/
this._2d.stroke();/*绘制定义的路径*/
}
}
blackboard.init();
</script>
</body>
</html>

  

最新文章

  1. 【Bootstrap】Bootstrap-select多选下拉框实现
  2. 单元测试中如何配置log4net
  3. python 学习笔记十 rabbitmq(进阶篇)
  4. POJ3729 Facer’s string 后缀数组
  5. [SQL注入2]FROM SQL INJECTION TO SHELL: POSTGRESQL EDITION
  6. Activity Window View的关系
  7. NET工厂模式架构
  8. 我理解设计模式C++实现观察者模式Observer Pattern
  9. 《JS权威指南学习总结--8.8.4 记忆函数》
  10. struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)
  11. 深入浅出 Java Concurrency 锁机制 : AQS
  12. TreeSet集合如何保证元素唯一
  13. &lt;图论入门&gt;邻接矩阵+邻接表
  14. 数字特征值-python
  15. @ResponseBody 和 @RequestBody 的作用
  16. Mask RCNN 原理
  17. CF1082G Petya and Graph
  18. sqlserver2008设置定时任务
  19. 机器装多个版本php,并安装redis插件报错【已解决】
  20. BZOJ3926:[ZJOI2015]诸神眷顾的幻想乡(广义SAM)

热门文章

  1. Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库
  2. 比较好的Redux和React-Redux学习资料
  3. 读取和修改xml文件
  4. Delphi2010,DelphiXE 安装控件找不到DesignIntf 解决办法
  5. 冒泡排序 和 选择排序的 区别 python
  6. Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果
  7. 各个 C# 版本的主要特性、发布日期和发布方式(C# 1.0 - 7.3)
  8. 取消Jquery mobile自动省略的文本
  9. python3 操作注册表
  10. Folly: Facebook Open-source Library Readme.md 和 Overview.md(感觉包含的东西并不多,还是Boost更有用)