canvas 实现自由画线,变换颜色、画笔大小,撤销上一步等简单功能

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>画板</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <style>
body,div,canvas,h5,input,ul,li,p,button{
margin: 0px;
padding: 0px;
position: relative; }
#mycanvas{ margin: 5px;
}
#content{
margin: 5px auto;
width: 700px;
height: 510px;
border: 1px solid gray; }
#canvas_selector{
position: absolute;
margin-left: 505px;
margin-top: -512px; height: 500px;
width: 190px;
border:1px solid black;
}
.title{
text-align: center;
margin-bottom: 10px;
} ul li{ list-style-type: none;
margin: 10px 30px 10px 20px;
display: block;
float: left;
width: 40px;
height: 20px;
background:greenyellow;
cursor: pointer;
border: 1px solid gray;
} #canvas_color,#canvas_brush,#canvas_control,#canvasImage{ margin:50px 0 50px 0;
} #canvas_brush{ height: 80px;
margin:10px 10px 0px 20px; background:greenyellow;
text-align:center; }
#lineT{
width: 150px;
height: 30px;
background:bisque;
}
#canvas_control{
margin:10px 10px 20px 30px;
text-align:center;
} #canvasImage{
text-align: center; }
#imgDiv{
margin: 0 auto;
}
#line{
width: 40px;
height: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="content">
<canvas id="mycanvas" width="500" height="500" style="border: 1px solid red;"></canvas>
<div id="canvas_selector"> <div id="canvas_color"> <h5 class="title">颜色<input type="color" name="color" id="changeColor" /></h5>
</div>
<div id="canvas_brush">
<h5 class="title">画笔大小</h5>
<input type="range" id="lineT" min="1" max="100" value="2">
</div>
<div id="canvas_control">
<h5 class="title">操作</h5>
<span><button style="background:greenyellow" id="prev">上一步</button></span> <span><button style="background:greenyellow" id="cloth">橡皮擦</button></span>
<span><button style="background:#ffc200" id="clear">清除</button></span>
</div>
<div id="canvasImage">
<button id="createImg">生成图像</button>
</div>
</div> </div>
<div id="imgDiv"></div> </body>
</html>
<script>
var c=$("#mycanvas")[0];
var canvas= $("#mycanvas");
console.log(c);
var ctx=c.getContext("2d");//创建画布对象
var bool=false;
var left=$("#mycanvas").offset().left;//获取画布的left值
console.log("left",left);
var top=$("#mycanvas").offset().top;//获取画布的top值
console.log("top",top);
var canvasW=$("#mycanvas").width();//获取画布的宽度
console.log("canvasW",canvasW);
var canvasH=$("#mycanvas").height();//获取画布的高度
console.log("canvasH",canvasH);
var img = []; //用于存放画布图片截图的数组
var imgDiv=document.getElementById("imgDiv");
var content=document.getElementById("content") var color="#000"; ctx.lineCap="round";// 设置线条的结束端点样式
ctx.lineJion="round";//设置两条线相交时,所创建的拐角类型 //鼠标点击设置画布起点
/* $("#mycanvas") */canvas.mousedown(function(e){
bool=true;
console.log("mousedown",bool);
ctx.beginPath();//起始/重置一条路径
ctx.moveTo(e.clientX-left,e.clientY-top); //把路径移动到画布中的指定点,不创建线条
var pic=ctx.getImageData(0,0,canvasW,canvasH);//获取当前画布的图像
img.push(pic);//将当前图像存入数组
});
//当bool=ture时鼠标移动画线
/* $("#mycanvas") */canvas.mousemove(function(e){
console.log("mousemove",bool);
if(bool){ //通过bool值控制画线的连续性,如果bool=true,画线
console.log("if(bool)",bool);
ctx.lineTo(e.clientX-left,e.clientY-10);//添加一个新点,在画布中创建从该点到最后指定点的线条
ctx.stroke();//画线
}
});
//鼠标移出画布或者抬起时,退出当前画线,并新建画线,实现画线断续
/* $("#mycanvas") */canvas.mouseout(function(e){
ctx.closePath();//当鼠标移出画布区域时,创建从当前点回到起始点的路径
bool=false;
console.log("mouseout",bool);
});
/* $("#mycanvas") */canvas.mouseup(function(e){
ctx.closePath();//当鼠标抬起时,创建从当前点回到起始点的路径
bool=false;
console.log("mouseup",bool);
}); //清除画布
$("#clear").click(function(){
//alert("Are you sure clear the canvas?");
ctx.clearRect(0,0,canvasW, canvasH);//创建矩形清空
});
//擦除画布
$("#cloth").click(function(){
ctx.strokeStyle="#fff";//利用画线为白色实现橡皮擦功能
});
//上一步
$("#prev").click(function(){
if(img.length>=0){
console.log("img.length",img.length);
var newImgLength=img.length;
console.log("newImgLength",newImgLength);
ctx.putImageData(img.pop(),0,0); }
});
//改变颜色
$("#changeColor").change(function(){
ctx.strokeStyle=this.value;//改变颜色
});
//改变画笔大小
$("#lineT").change(function(){
ctx.lineWidth=this.value;
}); //生成图片
$("#createImg").click(function(){
var url=c.toDataURL('image/png');
var newImg=new Image();//创建一个Image对象
newImg.src=url;
imgDiv.appendChild(newImg);
imgDiv.style.width="500px";
imgDiv.style.height="500px";
imgDiv.style.background="#ccc"; }); </script>

  

canvas 实现简单的画板功能 1.0的更多相关文章

  1. canvas 实现简单的画板功能添加手机端效果 1.01

    在上次的基础上,加了一些代码,手机端可操作 访问网址:https://chandler712.github.io/Item/ <!-- 简单版画板 --> <!DOCTYPE htm ...

  2. iOS实现白板、画板功能,有趣的涂鸦工具,已封装,简单快捷使用

    一.效果图: 二.选择颜色: 分[固定颜色模式]和[自由取模式].  三.操作栏功能: 1.撤销:撤销上一步操作,可一直往上进行,直到全部清空. 2.清空:直接清除所有绘画. 3.橡皮擦:去除不要的绘 ...

  3. 在iOS中实现一个简单的画板App

    在这个随笔中,我们要为iPhone实现一个简单的画板App. 首先需要指出的是,这个demo中使用QuarzCore进行绘画,而不是OpenGL.这两个都可以实现类似的功能,区别是OpenGL更快,但 ...

  4. Python3使用PyQt5制作简单的画板/手写板

    0.目录 1.前言 2.简单的画板1.0 在定点和移动中的鼠标所在处画一条线 3.简单的画板2.0 在定点和移动中的鼠标所在处画一条线 并将画过的线都保留在窗体上 4.简单的画板3.0 将按住鼠标后移 ...

  5. Unity UGUI 实现简单拖拽功能

    说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标),这两个坐标是不一样的,所以拖拽就需要转换. 因 ...

  6. 一步一步学Silverlight 2系列(5):实现简单的拖放功能

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  7. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  8. ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...

  9. Web---创建Servlet的3种方式、简单的用户注册功能

    说明: 创建Servlet的方式,在上篇博客中,已经用了方式1(实现Servlet接口),接下来本节讲的是另外2种方式. 上篇博客地址:http://blog.csdn.net/qq_26525215 ...

随机推荐

  1. jwt三种方式

    package library.book.demo.config.loginconfig; import com.alibaba.fastjson.JSON; import com.sun.org.a ...

  2. http请求包含哪几个部分(请求行、请求头、请求体)

    http协议报文     1.请求报文(请求行/请求头/请求数据/空行)         请求行             求方法字段.URL字段和HTTP协议版本             例如:GET ...

  3. Linux·命令收藏

    时间:2018-11-20 记录:byzqy 标题:Linux命令大全(手册) 地址:http://man.linuxde.net/ 标题:Linux script命令 -- 终端里的记录器 地址:h ...

  4. 面试官:如何实现LRU?你学会了吗?

    面试官:来了,老弟,LRU缓存实现一下? 我:直接LinkedHashMap就好了. 面试官:不要用现有的实现,自己实现一个. 我:..... 面试官:回去等消息吧.... 大家好,我是程序员学长,今 ...

  5. leetcode 位运算异或

    1. 只出现一次的数字(136) 异或的性质总结: 相异为1,相同为0: a ^ a = 0; 0 ^ a = a; 如果 a ^ b = c 成立,那么a ^ c = b 与 b ^ c = a 均 ...

  6. tornado2.2安装教程

    最近要用到vxworks 系统,所以避免不了要安装tornado 软件,进行相关的开发. 自己在安装过程中遇到了点点问题,这里记录下来,免得以后再次安装时遇到同样的问题. 1.   首先提供一个tor ...

  7. pyRevit开发:如何创建轴网

    必看部分: Document获取: 必看文章 如何添加基本项目引用 基础部分: 创建轴网 基本思路: 首先添加引用 获取当前项目文档 创建轴网定位线 创建轴网 设置轴网名称 实现代码: import ...

  8. aes加解密前后端-前端

    一.ajax请求前端 f12请求和响应参数效果: 1.在前端封装ajax的公共Util的js中,封装ajax请求的地方,在beforeSend方法和成功之后的回调函数success方法中: var p ...

  9. Vue设置全局js/css样式

    ''' 配置全局js mian.js: import settings from '@/assets/js/settings' Vue.prototype.$settings = settings; ...

  10. 妙用 background 实现花式文字效果

    本文将讲解如何利用 background 系列属性,巧妙的实现一些花式的文字效果.通过本文,你将可以学到: 通过 background-size 与 background-position 实现酷炫的 ...