function palette(canvas,ctx){   //初始化画布内部元素默认样式
this.strokeColor = 'red'; //默认选中红色触发颜色
this.fillColor = 'green'; //默认选中绿色填充色
this.style = 'tablet'; //默认选中直线形状
this.type = 'stroke'; //默认的绘制方式是划
this.ctx = ctx; //默认为绘图环境
this.canvas = canvas; //默认画布
this.canvasW = canvas.width; //默认画布宽
this.canvasH = canvas.height; //默认画布高
this.history = []; //默认的历史记录为数组,
this.edges = ''; //默认的边数为3
} 在切换绘图状态是都要对history 进行操作,不然将是两个不同画布的状态。
// 书写绘画函数
palette.prototype.drawing = function(){
//移动
if(move){
var that = this;
this.canvas.onmousedown = function(e) { //鼠标移动画布的函数
moving=true;
var point = getLocation(e.clientX,e.clientY);
//var s = getnear(point.x, point.y);
//console.log("+++++++++++"+s);
$.each(draws, function(n, v) {
restdraw(draws[n]);
if(ctx.isPointInPath(point.x, point.y)){
console.log("======================"+n);
// if(s==n){
moveItem = n;
console.log(n);
ctx.strokeStyle = "green";
//}
} else {
ctx.strokeStyle = "red";
}
ctx.stroke();
})
}
//获取鼠标移动时的坐标
canvas.onmousemove = function(e){
if( moving==false && moveItem < ) {
return;
}
p.ctx.clearRect(, , p.canvasW, p.canvasH);
var point = getLocation(e.clientX,e.clientY);
$.each(draws, function(n, v) {
if(n !== moveItem ){
ctx.strokeStyle = "red";
} else {
ctx.strokeStyle = "green";
draws[n].l1=point.x-draws[n].l3/;
draws[n].l2=point.y-draws[n].l4/;
}
restdraw(draws[n]);
ctx.stroke();
})
}
canvas.onmouseup = function(e){
moving = false;
moveItem =-;
ctx.save();
that.history.push(that.ctx.getImageData(,,that.canvasW,that.canvasH));
}
}else{ //绘图
var that = this;
// console.log(this);
this.canvas.onmousedown = function(e){ //鼠标移动画布的函数
// * 获取鼠标起始位置
var sx = e.offsetX;
// 获取鼠标到时间源的宽度
// console.log(sx);
var sy = e.offsetY;
// 获取鼠标到时间源的高度
// console.log(sy);
that.init(); //初始化样式
if(that.style=="tablet"){
that.ctx.beginPath();
// beginPath() 方法开始一条路径,或重置当前的路径
that.ctx.moveTo(sx,sy);
// moveTo() 方法可把窗口的左上角移动到一个指定的坐标。
}
// 获取鼠标移动时的坐标
canvas.onmousemove = function(e){
var mx = e.offsetX;
// console.log(mx);
var my = e.offsetY;
// console.log(my);
if (that.style!= "eraser") {
that.ctx.clearRect(, , that.canvasW, that.canvasH);
// console.log(that.canvasW + ',' + that.canvasH);
// 清除鼠标在画布移动的填充色
if(that.history.length>){ //注:只能是that.history数组的长度大于0,才可以putImageData()
that.ctx.putImageData(that.history[that.history.length-],,);
// putImageData() 方法将图像数据(从指定的 ImageData 对象)放回画布上。
}
}
// console.log(that.history.length);
that[that.style](sx,sy,mx,my);
}
// 获取鼠标移走的坐标
canvas.onmouseup = function(){
that.history.push(that.ctx.getImageData(,,that.canvasW,that.canvasH));
// getImageData() 方法返回 ImageData 对象,该对象拷贝了画布指定矩形的像素数据。
this.onmousemove=null;
// 清空鼠标移动事件
this.onmouseup=null;
// 清空鼠标移出事件
}
}
} }

canvas绘图history妙用的更多相关文章

  1. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

  2. HTML5 学习总结(四)——canvas绘图、WebGL、SVG

    一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...

  3. HTML5学习总结——canvas绘制象棋(canvas绘图)

    一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...

  4. 伙伴们休息啦canvas绘图夜空小屋

    HTML5 canvas绘图夜空小屋 伙伴们园友们,夜深了,休息啦,好人好梦... 查看效果:http://hovertree.com/texiao/html5/28/ 效果图如下: 代码如下: &l ...

  5. HTML5_03之Canvas绘图

    1.Canvas绘图--JS绘图: <canvas id='c1' width='' height=''></canvas> * Canvas尺寸不能用CSS设置: c1.he ...

  6. javascript的canvas绘图的基本用法

    <canvas>是HTML里面非常强大的元素,利用它结合js可以实现很多动画效果,大大增强交互性.下面,我想用图文并茂的方式阐述一下canvas的绘图机制的基础内容,话不多说,先上代码: ...

  7. canvas绘图、WebGL、SVG

    目录 一.Canvas 1.1.创建canvas元素 1.2.画线 1.3.绘制矩形 1.4.绘制圆弧 1.5.绘制图像 1.6.绘制文字 1.7.随机颜色与简单动画 二.WebGL 2.1.HTML ...

  8. HTML5 学习笔记(四)——canvas绘图、WebGL、SVG

    一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...

  9. Canvas绘图基础(一)

    简单图形绘制 矩形:描边与填充 Canvas的API提供了三个方法,分别用于矩形的清除.描边及填充 clearRect(double x, double y, double w, double h) ...

随机推荐

  1. 基于Thinkphp5.0 小程序登录插件应用

    资源连接: wulongtao/think-wxminihelper 具体怎么安装,不介绍了,有不懂再问我吧: 主要重点如下: wepy:index.wpy this.$parent.getUserI ...

  2. SpringMVC 拦截器使用说明

    spring-content.xml <!-- 配置用于session验证的拦截器 --> <!-- 如果有多个拦截器满足拦截处理的要求,则依据配置的先后顺序来执行 --> & ...

  3. linux环境node服务器配置流程

    一. 安装node Node 官网已经把 linux 下载版本更改为已编译好的版本了,我们可以直接下载解压后使用: # wget https://nodejs.org/dist/v10.9.0/nod ...

  4. JSON数组形式字符串转换为List<Map<String,String>>的几种方法

    package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArr ...

  5. FLAG区

    以下是一些flag(倒了我也不会怎么样): 更博客(对不起 您呼叫的flag是空号 请稍后再拨) CTS/APIO2019 Cu+ NOI2019 Ag+

  6. 2018-2019 ACM-ICPC Pacific Northwest Regional Contest C Contest Setting(DP)

    比赛链接:Contest Setting C题 题意:$n$道题目,每道题目难度为$ai$,选择$k$道难度不同的题目,有多少种选择方案.$1<=k<=n<=1000,1<=a ...

  7. (二)flask-sqlalchemy基本操作

    对数据库基本操作 在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 db. ...

  8. Python中字符串、列表、元组、字典、集合常用方法总结

  9. Educational Codeforces Round 46 C - Covered Points Count

    C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...

  10. gradle配置统一管理

    配置全局参数 根目录build.gradle文件: 主要关注ext部分 // Top-level build file where you can add configuration options ...