【canvas】blackboard 黑板
本来想的挺复杂实际操作了一下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>
【canvas】blackboard 黑板的更多相关文章
- 《Note --- Unreal 4 --- behavior tree》
Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...
- 使用Map List 封装json数据
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...
- 英语进阶系列-A03-英语升级练习一
古诗背诵 要求:根据诗句,先翻译成现代文,然后绘制图像. 词汇系列 要求:认真朗读单词,然后通过该单词联想2个词汇,然后给每个单词造句. 例子:class班级,联想到了classroom教室,clas ...
- 笔记:Sublime Text 3
http://www.sublimetext.com/3 Sublime Text官网 http://www.sublimetextcn.com/3/ Sublime Text中文官网 http:// ...
- 树莓派进阶之路 (018) - raspberryPi摄像头命令行软件raspistill帮助文档
文档原文:https://www.raspberrypi.org/app/uploads/2013/07/RaspiCam-Documentation.pdf 作用 在指定的时间运行摄像头,而且如果需 ...
- English trip -- VC(情景课)2 A At school
xu言: Sometimes, I feel very confused. However, there will always be a weak light in a corner to ligh ...
- 《今天你买到票了吗?——从铁道部12306.cn站点漫谈电子商务站点的“海量事务快速处理”系统》
<今天你买到票了吗?--从铁道部12306.cn站点漫谈电子商务站点的"海量事务快速处理"系统> 首发地址: http://bbs.hpx-party.org/thre ...
- [UE4]装饰器:Blackboard(装饰器的一种,不是黑板)
装饰器Blackboard可以检查黑板的值是否满足期望的条件: 添加“Blackboard装饰器”:在组合或者任务节点上右键“添加装饰器...”,跟普通装饰器一样. Notify Observer:通 ...
- canvas常用属性方法由浅下沉
首先引入<canvas></canvas>标签就不必说了. 其次就是得到canvas的2d环境了( var ctx = canvasDom.getContext('2d') ) ...
随机推荐
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- struts2和velocity整合问题
以下是我第一次使用velocity的时候写的 2012-03-12 话说struts真够懒的,都把velocity-1.6.4.jar放到他自己的lib里边了,就不给放全了,搞得新手太郁闷了.stru ...
- H∞一般控制问题的鲁棒叙述性说明
Robust Control System:反馈控制有承受一定类不确定能力的影响,这一直保持在这种不确定的条件(制)稳定.动态特性(灵敏度)和稳态特性(逐步调整)的能力. 非结构不确定性(Unstru ...
- 设置Oracle tnslsnr监听器口令
绿盟扫描提示引用程序脆弱账号 Oracle tnslsnr 监听器,加密主要为了防止监听被恶意远程关闭.关于这个安全问题的详细说明参见文字结尾转载的说明<Oracle的监听口令及监听器安全&g ...
- 我的MBTI职业性格测试
背景 最近在看<程序员的思维训练--开发人之前能的九堂课>,其中讲到了 MBTI 职业性格测试的指导意义.记起来两年多以前在面试 ASES 的时候有做过这个测试,只可惜当时的测试结果在好几 ...
- PCI GXL学习之二次开发篇
作者:朱金灿 来源:http://blog.csdn.net/clever101 gxl的二次开发分初级和高级之分.初级是gxl提供了几百个模块供你编排成不同的作业.高级就是你可以编写你的算法模块,然 ...
- .NET开发人员的瓶颈和职业发展
现在社会比前几年浮躁了,越来越多的人抱怨薪水低,高薪工作不好找; 诚然这有CPI的压力,可是也有很多人没有认清自己的职业发展. 很多.net程序员个各种纠结,想拿高薪又拿不到,想提高又不知道怎么能提高 ...
- JS简单验证password强度
<input type="password" id="password" value=""/><button id=&qu ...
- Oracle 一些实用的DBA语句
--查询LOB的大小和所在表空间 SELECT A.TABLE_NAME, A.COLUMN_NAME, B.SEGMENT_NAME, B.SEGMENT_TYPE, B.TABLESPACE_NA ...
- uwp - 解决“Microsoft.EntityFrameworkCore.Tools –Pre因为在此系统上禁止运行脚本”
在uwp使用ef时,需要安装“Microsoft.EntityFrameworkCore.Tools –Pre” ,如果安装失败提示:“无法加载文件 \.nuget\packages\Microsof ...