【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') ) ...
随机推荐
- Swift下CoreData的使用
我之前的随笔中有写过一些iOS持久化存储的方法,包含了sqlite.解归档.沙盒存放等等.这些方式中,能够大规模存储,并保持性能的只有使用sqlite了.而这里将记录下Cocoa自身继承的数据库的存储 ...
- ckplayer 插件在线视频播放
1. CKplayer 它是一款用于网页上播放视频的插件,支持的格式有:http 协议上的 flv, f4v, mp4格式,同时支持 rtmp 视频流格式播放.使用非常简单. <html> ...
- jQuery Mobile移动开发
1.在<head>元素中包括JavaScript文件是传统的方法.然而,依据Yahoo!"80%的终于用户响应时间在前端上"的说法,这些事件大部分花在资产的下载上,比如 ...
- xml报错(xsd):Failed to read schema document
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...
- PCI GXL学习之安装篇
作者:朱金灿 来源:http://blog.csdn.net/clever101 上周简单学习了PCI gxl的使用和二次开发.说实话gxl没有我想象中那么自动化,我原以为把一个数据处理作业扔给gxl ...
- Oracle 学习笔记 18 -- 存储函数和存储过程(PL/SQL子程序)
PL/SQL子程序 它包含了函数和过程.此功能是指用户定义的函数.和系统功能是不同的.子程序通常完成特定的功能PL/SQL座.,能够被不同的应用程序多次调用.Oracle提供能够把PL/SQL程序存储 ...
- 数据科学(data science)概览
0. 硬件平台设计 一种分层的体系结构: 自下到上依次是: 硬件层 分布式系统层 分布式管理层 分布式处理层 应用层: 1. 总论
- MyEclipse和Eclipse区别与联系
Eclipse:IBM花了4千万美金来开发这个IDE(Integrated Development Environment).第一版1.0在2001年11月释出,随后逐渐受到欢迎.Eclipse已经成 ...
- 简明Python3教程 2.序言
Python也许是为数不多的既简单又强大的编程语言.这有利于新手甚至于专家,更重要的是用它编程所带来的乐趣. 这本书的目的是帮助您了解这种神奇的语言,展示如何快速而轻松地完成事情——事实上”编程问题的 ...
- JQuery在一个简单的表单验证的例子
html代码例如以下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...