canvas - 简单画板
截图:
Demo:Demo
上代码:.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{ margin:0;padding:0; }
body{ }
canvas{ background-color: #fff;border:1px solid #ccc;}
ul,li{ padding:0;margin:0; list-style:none;}
.fl{ float: left;}
.warp{ height:600px;}
.canvas{ float: left;}
.left{ margin-left:620px;}
.show-img{ float:left;margin-left:10px;width:600px;height:600px; border:1px solid #ccc;}
.detail-main{ padding:20px;}
.detail-main p{margin:10px 0;}
.detail-main input{ margin-top:10px; }
.color-ul{ height: 32px; clear: both;}
.color-ul li{ float: left; width:80px;height:30px; border:1px solid #ccc; margin-right: 10px; cursor:pointer;}
.color-ul li:nth-child(1){ background-color:blue; }
.color-ul li:nth-child(2){ background-color:red;}
.color-ul li:nth-child(3){ background-color:#000; }
.color-ul li:nth-child(4){ background-color:pink; }
.size-ul{ height: 32px; clear: both;}
.size-ul li{ float: left; border-radius: 50%; margin-right: 10px; background-color: #000; vertical-align: middle; cursor:pointer;}
.size-ul li:nth-child(1){ width:10px;height:10px; margin-top: 10px; }
.size-ul li:nth-child(2){ width:20px;height:20px; margin-top: 5px; }
.size-ul li:nth-child(3){ width:30px;height:30px; }
</style>
<script>
window.onload = function(){ var oBtn = document.getElementById('btn');
var oAllClearBtn = document.getElementById('allClearBtn'); var oClearBtn = document.getElementById('clearBtn');
var oShowImg = document.getElementById('show-img'); var oBrush = document.getElementById('brush'); var oColorUl = document.getElementById('color-ul');
var aColorLis = oColorUl.getElementsByTagName('li'); var oSizeUl = document.getElementById('size-ul');
var aSizeLis = oSizeUl.getElementsByTagName('li'); var oCanvas = document.getElementById('canvas');
var oCtx = oCanvas.getContext('2d'); oCanvas.setAttribute('width','600');
oCanvas.setAttribute('height','600'); var COLORS = '#000';
var BORDER = 2; var maxBorder = 2;
var CLEAR = false;
var BRUSH = true; for( var i=0,len=aColorLis.length; i<len;i++ ){ aColorLis[i].onclick = function(){ var color = this.getAttribute('color');
COLORS = color; } } for( var i=0,len=aSizeLis.length; i<len;i++ ){ aSizeLis[i].onclick = function(){
var size = this.getAttribute('size');
BORDER = parseInt(size);
maxBorder = BORDER + 2;
} } oBtn.onclick = function(){ var src = oCanvas.toDataURL("image/png");
var html='<img width="600" height="600" src="'+src+'" alt="">';
oShowImg.innerHTML = html; } oClearBtn.onclick = function(){ CLEAR = true; } oAllClearBtn.onclick = function(){
oCtx.clearRect(0,0,600,600);
} oBrush.onclick = function(){ CLEAR = false; } oCanvas.onmousedown = function( event ){ oCtx.beginPath();
var ev = window.event || event;
var diX = ev.clientX;
var diY = ev.clientY; document.onmousemove = function( event ){ oCtx.save();
var ev = window.event || event; var x = ev.clientX;
var y = ev.clientY; if( CLEAR ){ oCtx.clearRect(x,y,maxBorder,maxBorder);
return false;
} if( BRUSH ){
oCtx.lineWidth = BORDER;
oCtx.lineTo(x, y);
oCtx.strokeStyle = COLORS;
oCtx.stroke();//画线
return false;
} oCtx.restore(); return false; }
document.onmouseup = function( event ){
document.onmousemove = null; }
} }
</script>
</head>
<body> <div class="warp">
<canvas id="canvas" class="canvas"></canvas>
<div class="left">
<span class="fl">生成图片</span>
<div id="show-img" class="show-img"> </div>
</div>
</div> <div class="detail-main">
<p>
颜色:
</p>
<ul class="color-ul" id="color-ul">
<li color="blue"></li>
<li color="red"></li>
<li color="#000"></li>
<li color="pink"></li>
</ul>
<p>
画笔大小:
</p>
<ul class="size-ul" id="size-ul">
<li size="2"></li>
<li size="4"></li>
<li size="8"></li>
</ul>
<input type="button" value="画笔" id="brush">
<input type="button" value="橡皮擦" id="clearBtn">
<input type="button" value="清楚全部" id="allClearBtn">
<input type="button" value="生成图片" id="btn">
</div> </body>
</html>
有问题Demo : Demo
有问题的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{ margin:0;padding:0; }
body{ }
canvas{ background-color: #fff;border:1px solid #ccc;}
ul,li{ padding:0;margin:0; list-style:none;}
.fl{ float: left;}
.warp{ height:600px;}
.canvas{ float: left;}
.left{ margin-left:620px;}
.show-img{ float:left;margin-left:10px;width:600px;height:600px; border:1px solid #ccc;}
.detail-main{ padding:20px;}
.detail-main p{margin:10px 0;}
.detail-main input{ margin-top:10px; }
.color-ul{ height: 32px; clear: both;}
.color-ul li{ float: left; width:80px;height:30px; border:1px solid #ccc; margin-right: 10px; cursor:pointer;}
.color-ul li:nth-child(1){ background-color:blue; }
.color-ul li:nth-child(2){ background-color:red;}
.color-ul li:nth-child(3){ background-color:#000; }
.color-ul li:nth-child(4){ background-color:pink; }
.size-ul{ height: 32px; clear: both;}
.size-ul li{ float: left; border-radius: 50%; margin-right: 10px; background-color: #000; vertical-align: middle; cursor:pointer;}
.size-ul li:nth-child(1){ width:10px;height:10px; margin-top: 10px; }
.size-ul li:nth-child(2){ width:20px;height:20px; margin-top: 5px; }
.size-ul li:nth-child(3){ width:30px;height:30px; }
</style>
<script>
window.onload = function(){ var oBtn = document.getElementById('btn');
var oAllClearBtn = document.getElementById('allClearBtn');
var oClearBtn = document.getElementById('clearBtn');
var oShowImg = document.getElementById('show-img'); var oSquareBtn = document.getElementById('squareBtn');
var oBrush = document.getElementById('brush'); var oColorUl = document.getElementById('color-ul');
var aColorLis = oColorUl.getElementsByTagName('li'); var oSizeUl = document.getElementById('size-ul');
var aSizeLis = oSizeUl.getElementsByTagName('li'); var oCanvas = document.getElementById('canvas');
var oCtx = oCanvas.getContext('2d'); oCanvas.setAttribute('width','600');
oCanvas.setAttribute('height','600'); var COLORS = '#000';
var BORDER = 2; var maxBorder = 2;
var CLEAR = false;
var SQUARE = false;
var BRUSH = true; for( var i=0,len=aColorLis.length; i<len;i++ ){
aColorLis[i].onclick = function(){ var color = this.getAttribute('color');
COLORS = color;
}
} for( var i=0,len=aSizeLis.length; i<len;i++ ){
aSizeLis[i].onclick = function(){
var size = this.getAttribute('size');
BORDER = parseInt(size);
maxBorder = BORDER + 2;
}
} oBtn.onclick = function(){ var src = oCanvas.toDataURL("image/png");
var html='<img width="600" height="600" src="'+src+'" alt="">';
oShowImg.innerHTML = html; } oClearBtn.onclick = function(){
SQUARE = BRUSH = false;
CLEAR = true;
} oAllClearBtn.onclick = function(){
oCtx.clearRect(0,0,600,600);
} oSquareBtn.onclick = function(){
CLEAR = BRUSH = false;
SQUARE = true; } oBrush.onclick = function(){
CLEAR = SQUARE = false;
BRUSH = true; } oCanvas.onmousedown = function( event ){
oCtx.beginPath();
var ev = window.event || event;
var diX = ev.clientX;
var diY = ev.clientY; document.onmousemove = function( event ){ oCtx.save();
var ev = window.event || event; var x = ev.clientX;
var y = ev.clientY; if( CLEAR ){ oCtx.clearRect(x,y,maxBorder,maxBorder);
return false;
} if( SQUARE ){ oCtx.strokeStyle = COLORS; //边框颜色
oCtx.linewidth = BORDER; //边框宽
oCtx.clearRect(diX,diY,x-diX,y-diY);
oCtx.strokeRect(diX,diY,x-diX,y-diY); //填充边框 x y坐标 宽 高
oCtx.clearRect(diX,diY,x-diX,y-diY); return false;
} if( BRUSH ){
oCtx.lineWidth = BORDER;
oCtx.lineTo(x, y);
oCtx.strokeStyle = COLORS;
oCtx.stroke();//画线
return false;
} oCtx.restore(); return false; }
document.onmouseup = function( event ){
document.onmousemove = null; }
} }
</script>
</head>
<body>
<div class="warp">
<canvas id="canvas" class="canvas"></canvas>
<div class="left">
<span class="fl">生成图片</span>
<div id="show-img" class="show-img"> </div>
</div>
</div> <div class="detail-main">
<p>
颜色:
</p>
<ul class="color-ul" id="color-ul">
<li color="blue"></li>
<li color="red"></li>
<li color="#000"></li>
<li color="pink"></li>
</ul>
<p>
画笔大小:
</p>
<ul class="size-ul" id="size-ul">
<li size="2"></li>
<li size="4"></li>
<li size="8"></li>
</ul>
<input type="button" value="画笔" id="brush">
<input type="button" value="画正方形" id="squareBtn">
<input type="button" value="橡皮擦" id="clearBtn">
<input type="button" value="清楚全部" id="allClearBtn">
<input type="button" value="生成图片" id="btn">
</div> </body>
</html>
后记:
这应该算是 canvas 最简单的demo了,网上也有很多但是本身canvas是能画方块和圆的但是我最后这个有问题的demo没完全做出来,在做方块的时候就要清除之前的这一区域了所以看起来像一层一层叠上去的,包括如果从大方块往小了托尤其快速会留下很多边框也就是没清除的,如果哪位大神有比较的好的方法请告知,向您学习。
canvas - 简单画板的更多相关文章
- canvas实现画板
canvas实现画板 主要用到知识点: 鼠标事件onmousedown() onmousemove() onmouseup() onmouseleave() 事件委托 canvas的一些方法 如:绘制 ...
- 一款基于HTML5 Canvas的画板涂鸦动画
今天给各网友分享一款基于HTML5 Canvas的画板涂鸦动画.记得之前我们分享过一款HTML5 Canvas画板工具,可以切换不同的笔刷,功能十分强大.本文今天要再来分享一款基于HTML5 Canv ...
- canvas小画板——(2)荧光笔效果
我们在上一篇文章中讲了如何绘制平滑曲线 canvas小画板——(1)平滑曲线. 透明度实现荧光笔 现在我们需要加另外一种画笔效果,带透明度的荧光笔.那可能会觉得绘制画笔的时候加上透明度就可以了.我们来 ...
- 【分享】用Canvas实现画板功能
前言 PC端测试:QQ浏览器全屏绘画完成.缩小时内容会被清空,切换背景颜色内容会被重置,其他暂无发现: 手机端测试:微信内置浏览器不通过:Safari 浏览器使用画笔时没固定页面会有抖动效果,使用橡皮 ...
- html5 canvas 涂鸦画板
html5 canvas 的涂鸦画板,可以加载图片进行涂鸦,也可以下载服务器使用的php上传的图片不能超过1M,只能是jpg或者png 格式的演示地址的服务器网速不怎么样,读取文件可能很慢,到达100 ...
- 基于canvas的画板
最近重新在看Html5&CSS3的知识,看到canvas的时候,想到了以前在学校学计算机图形学时做过的画图实验,于是想,可以基于html5和css3来做一款画板,经过1天的努力,完成了画板的一 ...
- canvas小画板--(1)平滑曲线
功能需求 项目需求:需要实现一个可以自由书写的小画板 简单实现 对于熟悉canvas的同学来说,这个需求很简单,短短几十行代码就能实现: <!doctype html> <html& ...
- canvas简单图片处理(灰色处理)
反色处理写的比较简单,灰色处理写了一些注释 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- canvas简单处理图片(反色处理)
用canvas可以简单地处理图像,比如切割 灰色处理等,今天记下的是图像的反色处理. <!DOCTYPE html> <html> <head> <meta ...
随机推荐
- .net面式题
.Net httphandler与httpmodule区别 动态控件在postback能否保存下来(不能) 序列化(对象到其他格式(xml/json/byte...)JavaScriptSeriali ...
- Linux学习笔记:【000】Linux系统入门
什么是Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX(可移植操作系统接口 Portable Operating System Interface of UN ...
- mysql创建用户与pymsql模块
mysql 创建用户及增删改查 创建mysql 用户 with grant option 表示用户不存在自主创建 grant [ select ……,insert ……| all ] on 库名.表名 ...
- vue cli 解决跨域 线上 nginx 反向代理配置
前后分离 axios 接 api 跨域问题如图: 解决办法: 1. npm start 本地开发环境解决: 在webpack配置文件 /config/index.js 里找到 proxyTable 开 ...
- luogu 4377 Talent show 01分数规划+背包dp
01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...
- 【python小练】0020
第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年1 ...
- springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
创建5个项目: 1.服务注册中心 2.服务提供者1 3.服务提供者2(与服务提供者1的代码实现一样,这是是为了模拟负载均衡) 4.ribbon客户端项目 5.feign客户端项目 如图: 一.注册中心 ...
- JavaScript面试技巧(一):基础知识
1.变量类型和计算 变量类型:值类型.引用类型.typeof运算符. 变量计算:字符串拼接.==运算符.if语句.逻辑运算符 2.原型和原型链 构造函数 5个原型规则 3.作用域和闭包-执行上下文 4 ...
- 第26月第29天 ffmpeg yasm
1. brew install automake fdk-aac git lame libass libtool libvorbis libvpx \ opus sdl shtool texi2htm ...
- IDEA对新建java线程池的建议
1 代码片段 ExecutorService pool = Executors.newCachedThreadPool(); 2 建议的三种模板 A 第一种,采用Apache的common.lang3 ...