Html5 canvas 简单画布画板例子源码
直接复制到编译器上面运行!
<html> <head> <title>淘拜网</title> </head> <body> <canvas width="800" height="450" style="border:1px solid black"></canvas> <div id="info">-</div> <script> var canvas = document.getElementsByTagName('canvas')[0]; canvas.addEventListener('mousemove', onMouseMove, false); canvas.addEventListener('mousedown', onMouseDown, false); canvas.addEventListener('mouseup', onMouseUp, false); var info = document.getElementById('info'); var context = canvas.getContext('2d'); var linex = new Array(); var liney = new Array(); var linen = new Array(); var lastX = -1; var lastY = -1; var hue = 0; var flag = 0; function onMouseMove(evt) { if (flag == 1) { linex.push(evt.layerX); liney.push(evt.layerY); linen.push(1); context.save(); context.translate(context.canvas.width/2, context.canvas.height/2); context.translate(-context.canvas.width/2, -context.canvas.height/2); context.beginPath(); context.lineWidth = 5 + Math.random() * 10; for (var i=1;i<linex.length;i++) { lastX = linex[i]; lastY = liney[i]; if (linen[i] == 0) { context.moveTo(lastX,lastY); } else { context.lineTo(lastX,lastY); } } huehue = hue + 10 * Math.random(); context.strokeStyle = 'hsl(' + hue + ', 50%, 50%)'; context.shadowColor = 'red'; context.shadowBlur = 1; context.stroke(); context.restore(); } info.firstChild.nodeValue ='x = ' + evt.layerX + ' y = ' + evt.layerY; } function onMouseDown(evt) { flag = 1; linex.push(evt.layerX); liney.push(evt.layerY); linen.push(0); } function onMouseUp(evt) { flag = 0; linex.push(evt.layerX); liney.push(evt.layerY); linen.push(0); } </script> </body> </html>
正在加载评论...
栏目列表
推荐阅读
- js控制滚动条到最底端(置底)和最顶端(置顶)
- js将图片转化为二进制流
- bootstrap导航菜单,手机和PC端自适应
- javascript/jquery/js获取地址栏url传递的参数值的方法
- js辨别访问浏览器判断是手机,android,微信,pc还是ios系统
- IE浏览器下Ajax发送Post请求时提交中文乱码
- localStorage与sessionStorage封装可设置过期时间的缓存
- js 中获取当前系统时间
- 使用JS获取当前页面的URL,端口,协议,IP(网址信息)
- jquery.lightTreeview.js树形菜单插件例子
- js获取当前域名
- XMLHttpRequest封装同步请求的例子
- ztree加载树形菜单控件事列
- JS获取屏幕分辨率,可见区域,可用工作区等大小
- jquery.js和jquery-1.4.2.min.js的区别
- js利用Date.prototype.Format对时间格式化处理,yyyy-MM-dd HH:mm:ss:SS
- jquery追加元素的几种方法append(),prepend(),after(),before(),insertAfter(),insertBefore()
- 引入script地址时,并携带参数,js获取script标签上的属性数据值
- javascript怎么禁用浏览器后退按钮
- jquery和javascript判断页面元素是否存在