<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#button{
width: 60px;
height: 20px;
}
</style> </head>
<body>
<canvas id="canvas" width="600px" height="400px" style="background: #DBDDDF; "></canvas>
<input type="button" value="撤销" id="button"> <script type="text/javascript">
var canvas=document.getElementById('canvas');
var cext=canvas.getContext('2d'); cext.strokeStyle="black";
cext.lineWidth=2;
cext.fillStyle="black"; function getStyles(obj){//兼容FF,IE10; IE9及以下未测试
return document.defaultView.getComputedStyle(obj);
}
function getCanvasPos(canvas,e)
{//获取鼠标在canvas上的坐标
var rect = canvas.getBoundingClientRect();
var leftB = parseInt(getStyles(canvas).borderLeftWidth);//获取的是样式,需要转换为数值
var topB = parseInt(getStyles(canvas).borderTopWidth);
return {
x: (e.clientX - rect.left) - leftB,
y: (e.clientY - rect.top) - topB
};
} var restorePoint = {};
var shuzu=[];
var h=0;
var x1,x2,newx,newy; canvas.onmousedown=function (e) {
restorePoint = cext.getImageData(0, 0, canvas.width, canvas.height);
x1=getCanvasPos(canvas,e).x;
x2=getCanvasPos(canvas,e).y;
canvas.onmousemove=function(e){
cext.putImageData(restorePoint, 0, 0);
newx=getCanvasPos(canvas,e).x;
newy=getCanvasPos(canvas,e).y;
cext.beginPath();
cext.moveTo(x1,x2);
cext.lineTo(newx,newy);
cext.stroke();
cext.closePath();
}
} canvas.onmouseup=function(){
shuzu[h] = restorePoint;
h++;
canvas.onmousemove=null; } document.getElementById("button").onmousedown=function(){
restorePoint = shuzu[h-1];
cext.putImageData(restorePoint, 0, 0);
h--;
}
</script>
</body>
</html>

JS canvas 画板 撤销的更多相关文章

  1. [JS,Canvas]日历时钟

    [JS,Canvas]日历时钟 Html: <!doctype html> <html> <head> <meta charset="UTF-8&q ...

  2. 利用js+canvas实现的时钟效果图

    canvas+js时钟特效 运用js+canvas方面的知识完成一个时钟的效果图,再利用for循环实现指针的转动效果: <!--网页文档的声明--> <!doctype html&g ...

  3. 转 原生js canvas实现苹果电脑mac OS窗口最小化效果

    http://www.17sucai.com/pins/demo-show?id=2459 http://www.17sucai.com/pins/demo-show?id=2458  很多资料 ,前 ...

  4. js canvas游戏初级demo-躲避障碍物

    在线演示地址 http://200ok.fun:3100/html/game_demo.html 继上次js canvas游戏初级demo-上下左右移动(https://www.cnblogs.com ...

  5. QML学习笔记(二)-纯qml画图实现canvas画板-鼠标画图

    作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载请获取授权和联系作者 用纯qml实现canvas画板功能,用鼠标进行画图,可以画直线,画圆,画矩形,画弧线. 由于canvas画图会有延迟和 ...

  6. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理

    [微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...

  7. chart.js & canvas

    chart.js & canvas https://www.chartjs.org/samples/latest/ https://www.chartjs.org/samples/latest ...

  8. Atitit js canvas的图像处理类库attilax总结与事业

    Atitit js canvas的图像处理类库attilax总结与事业 1.1. 脸部识别JavaScript类库Tracking.js1 1.2. AlloyImage特性1 1.3. CamanJ ...

  9. js canvas captcha

    js canvas captcha https://thejackalofjavascript.com/building-a-captcha-using-html5-canvas/ https://a ...

随机推荐

  1. Food Delivery ZOJ - 3469(区间dp)

    题目传送门 题目翻译:当我们专注于解决问题时,我们通常宁愿呆在电脑前而不是外出吃午饭.在这个时候,我们可能会要求提供食物. 假设有N个人生活在一条直线的街道上,它只是位于X坐标轴上.第i个人的坐标是X ...

  2. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  3. Atomikos和GTS-Fescar和TCC-Transaction和TX-LCN分布式事物的比较

    什么是分布式事物 分布式系统中保证不同节点之间的数据一致性的事物,叫做分布式事物. 为什么要用分布式事物 微服务,SOA等服务架构模式,一个是service产生多个节点,另一个是resource产生多 ...

  4. 泛型List去除重复指定字段

    泛型List去除重复指定字段ID var list=listTemp.Distinct(new IDComparer ()).ToList(); 重写比较的方法: public class IDCom ...

  5. VBA读写ini 配置文件

    Option Explicit 'read Private Declare Function GetPrivateProfileString Lib "kernel32" Alia ...

  6. angular 键盘事件绑定与过滤

    方便的angular按钮绑定 如 <input (keyup.enter)="keyUpSearch($event)" value="按下回车键触发"&g ...

  7. MSSQL死锁进程查看及关闭

    select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...

  8. python学习——用dictionary实现通过地区查询邮编

    刚刚学习了python的基本语法,对自己学习的内容进行实践下. dictionary字典(类似map) 总结:1.dictionary比list读取速度快,但是占用内存大,适合存放不需修改,经常查询的 ...

  9. [Presto]Presto章1 Presto 咋用

    Presto 的平均查询性能是 Hive 的 10 倍! 由于 Presto 的数据源具有完全解耦.高性能,以及对 ANSI SQL 的支持等特性,使得 Presto 在 ETL.实时数据计算. Ad ...

  10. java上传文件常见几种方式

    1.ServletFileUpload 表单提交中当提交数据类型是multipare/form-data类型的时候,如果我们用servlet去做处理的话,该http请求就会被servlet容器,包装成 ...