首页
网页制作
网络编程
脚本专栏
数据库
网站运营
网络安全
平面设计
CMS教程
搜索
建站极客
网页制作
html5
正文
html5 canvas 简单画板实现代码
所属分类:
网页制作
/
html5
阅读数: 498
收藏 0
赞 0
分享
效果图:
注:下面的代码运行效果,请在支持html5浏览下执行,才能看到效果。
<!doctype html> <html> <head> <title>canvas简单画板</title> <style type="text/css"> #can{ width:600px; height:500px; border:1px solid #ccc; margin-top:0px; margin-left:20px;} </style> </head> <body> <h2 style="padding-left:20px">canvas简单画板</h2> <canvas id="can" width="600" height="500"></canvas> <script type="text/javascript"> function getBodyOffsetTop(el){ var top = 0; do{ top = top + el.offsetTop; }while(el = el.offsetParent); return top; } function getBodyOffsetLeft(el){ var left = 0; do{ left = left + el.offsetLeft; }while(el = el.offsetParent); return left; } function Drawing(canvas,options){ typeof canvas == 'string' && (canvas = document.getElementById(canvas)); if(!canvas || !canvas.getContext){ throw new Error(100,'do not support canvas!'); } this.option = { colors:['#000000','#ff0000','#00ff00','#0000ff','#00ffff','#7fef02','#4488bb'] }; this.setOption(options); this.init(canvas); } Drawing.prototype = { setOption:function(options){ typeof options == 'object' || (options = {}); for(var i in options){ switch(i){ case 'colors': this.option[i] = options[i]; break; } } }, init:function(canvas){ this.canvas = canvas; this.context = canvas.getContext('2d'); this.context.lineWidth = 1; this.context.lineJons = 'round'; this.context.lineCep = 'round'; this.isButtonDown = false; this.historyStroker = []; this.curStroker = {color:'#000000',path:[]}; this.lastX = 0; this.lastY = 0; this.curColor = '#000000'; this.toolbarspos ={}; this.bindEvent(); this.ResetDrawToolbar(); }, bindEvent:function(){ var self = this; this.canvas.addEventListener('mousemove',function(event){ var x = event.pageX-getBodyOffsetLeft(this), y = event.pageY-getBodyOffsetTop(this); self.onMouseMove({x:x,y:y}); },false); this.canvas.addEventListener('mousedown',function(event){ var x = event.pageX-getBodyOffsetLeft(this), y = event.pageY-getBodyOffsetTop(this); self.onMouseDown(event,{x:x,y:y}); },false); this.canvas.addEventListener('mouseup',function(event){ var x = event.pageX-getBodyOffsetLeft(this), y = event.pageY-getBodyOffsetTop(this); self.onMouseUp(event,{x:x,y:y}); },false); this.canvas.addEventListener('click',function(event){ var x = event.pageX-getBodyOffsetLeft(this), y = event.pageY-getBodyOffsetTop(this); self.onClick({x:x,y:y}); },false); }, onMouseMove:function(pos){ if(this.isButtonDown){ var p = this.toolbarspos; for(var i in p){ if(pos.x >= p[i].x && pos.x <= p[i].x+p[i].w && pos.y >= p[i].y && pos.y <= p[i].y+p[i].h){ return; } } this.context.lineTo(pos.x,pos.y); this.context.stroke(); this.lastX = pos.x; this.lastY = pos.y; this.curStroker.path.push(pos); } }, onMouseDown:function(event,pos){ if(event.button == 0){ var p = this.toolbarspos; for(var i in p){ if(pos.x >= p[i].x && pos.x <= p[i].x+p[i].w && pos.y >= p[i].y && pos.y <= p[i].y+p[i].h){ return; } } this.isButtonDown = true; this.lastX = pos.x; this.lastY = pos.y; this.context.beginPath(); this.context.moveTo(this.lastX,this.lastY); this.curStroker.color = this.curColor; this.curStroker.path.push(pos); } }, onMouseUp:function(event,pos){ if(event.button == 0){ var p = this.toolbarspos; for(var i in p){ if(pos.x >= p[i].x && pos.x <= p[i].x+p[i].w && pos.y >= p[i].y && pos.y <= p[i].y+p[i].h){ return; } } this.isButtonDown = false; this.historyStroker.push(this.curStroker); this.curStroker = {color:this.curColor,path:[]}; } }, ResetDrawAll:function(){ this.context.clearRect(0,0,500,500); this.ResetDrawCentre(); this.ResetDrawToolbar(); }, ResetDrawCentre:function(){ var p = this.historyStroker,p2, curColor = this.context.strokeStyle; for(var i=0; i< p.length;i++){ this.context.strokeStyle = p[i].color; this.context.beginPath(); for(var t=0; t<p[i].path.length;t++){ p2 = p[i].path[t]; if(t==0) this.context.moveTo(p2.x,p2.y); this.context.lineTo(p2.x,p2.y); this.context.stroke(); } this.context.beginPath(); } this.context.strokeStyle = curColor; }, ResetDrawToolbar:function(){ var curcolor = this.context.fillStyle; for(var i=0; i<this.option.colors.length;i++){ this.context.fillStyle = this.option.colors[i]; if(this.curColor == this.context.fillStyle){ this.context.fillRect(i*35+5,2,30,20); this.toolbarspos[i] ={x:i*35+5,y:2,w:30,h:20}; }else{ this.context.fillRect(i*35+5,5,30,20); this.toolbarspos[i] = {x:i*35+5,y:5,w:30,h:20}; } this.context.stroke(); } this.context.fillStyle = curcolor; }, onClick:function(pos){ var p = this.toolbarspos; for(var i in p){ if(pos.x >= p[i].x && pos.x <= p[i].x+p[i].w && pos.y >= p[i].y && pos.y <= p[i].y+p[i].h){ this.curColor = this.option.colors[i]; this.context.strokeStyle = this.curColor; this.ResetDrawAll(); } } } }; new Drawing('can'); </script></body> </html>
提示:您可以先修改部分代码再运行
更多精彩内容
其他人还在看
html5指南-4.使用Geolocation实现定位功能
今天我们要学习的是使用Geolocation实现定位功能。我们可以通过navigator.geolocation获取Geolocation对象,感兴趣的朋友可以了解下
评论 0
收藏 0
赞 0
分享
一张图片能隐含千言万语之隐藏你的程序代码
一个HTML5的视频智力游戏,开发的过程很有趣,我喜欢编程,但当实现了游戏逻辑后,我有了一个有趣的想法:为什么不想个办法把代码隐藏起来
评论 0
收藏 0
赞 0
分享
HTML4和HTML5之间除了相似以外的10个主要不同
HTML5是最新的HTML标准,重新开发一个HTML5的网站,要比把一个网站从HTML4迁移到HTML5上容易的多,这是因为这两个版本之间有很大不同之处
评论 0
收藏 0
赞 0
分享
HTML5 实现一个访问本地文件的实例
今天,我将向大家分享一个简单的应用,用来演示使用FileReader的方法, FileReader是HTML5里提供的一个文件操作API,需要的朋友可以了解下
评论 0
收藏 0
赞 0
分享
使用HTML5的链接预取功能(link prefetching)给网站提速
HTML5的链接预取功能(link prefetching)是一个埋在沙里的宝石,至今还很少人知道它的价值,需要的朋友可以了解下
评论 0
收藏 0
赞 0
分享
基于HTML5超酷摄像头(HTML5 webcam)拍照功能实现代码
基于HTML5实现的超酷摄像头(HTML5 webcam)拍照功能,需要了解的朋友可以参考下
评论 0
收藏 0
赞 0
分享
HTML5离线缓存在tomcat下部署可实现图片flash等离线浏览
打开一个网页,加载完后,如果突然断网了,那么你刷新后那页面就没了,怎么阻止这种局面的发生呢?html5的出现让我们豁然开朗,接下来将为您详细解读
评论 0
收藏 0
赞 0
分享
HTML5使用ApplicationCache接口实现离线缓存技术解决离线难题
离线访问对基于网络的应用而言越来越重要,虽然所有浏览器都有缓存机制,但它们并不可靠,HTML5 使用 ApplicationCache 接口解决了由离线带来的部分难题,需要的朋友可以参考下
评论 0
收藏 0
赞 0
分享
如何使用html5与css3完成google涂鸦动画
今天我们将介绍,如何使用css3完成google涂鸦动画。当你点击demo页面的【开始】按钮之后,页面中的骑手和马匹将会运动起来,需要的朋友可以了解下
评论 0
收藏 0
赞 0
分享
HTML5重塑Web世界它将如何改变互联网
即将成为新标准的HTML5到底会把我们带向哪里?下面收集了开发者、程序员以及设计师的一些看法,从中可以了解到HTML5如何改变互联网,需要的朋友可以了解下
评论 0
收藏 0
赞 0
分享
查看更多
网络赚钱
更多
从世界杯观赛看,“移动视频”就是个伪命题
站长故事实战:淘宝买关键词排名SEO服务攻略介绍
百度联盟点击率低的原因
网站赚钱就靠广告到底行不行?
浅谈一下个人站长领域的灰色地带有多么的暴利
光大银行网上银行西联汇款收汇方法分享
站长故事
更多
当iPhone6被上升成人民内部矛盾
站长故事:一个80后妈妈的微商之旅
一问易答:为何小米4/魅族MX4不支持NFC?
揭晓互联网三大巨头(百度,腾讯,阿里巴巴)和草根站长的往来
浅谈网站站长之现状
职场中12种经典实用的管理方法和工具