软件项目技术点(6)——结合鼠标操作绘制动态canvas画布
AxeSlide软件项目梳理 canvas绘图系列知识点整理
我们创建一个类封装了所有鼠标需要处理的事件。
export class MouseEventInfo {
el: HTMLElement;
onmouseDown: Function;
ondragMove: Function;
ondragUp: Function;
onmouseMove: Function;
onmouseUp: Function;
mouseWheel: Function;
onContextMenu: Function;
constructor(ele: any, mouseDown: Function, dragMove: Function, dragUp: Function, mouseMove: Function, mouseUp: Function, mouseWheel: Function, contexMenu: Function) {
this.el = ele;
this.onmouseDown = mouseDown || null;
this.ondragMove = dragMove || null;
this.ondragUp = dragUp || null;
this.onmouseMove = mouseMove || null;
this.onmouseUp = mouseUp || null;
this.mouseWheel = mouseWheel || null
this.onContextMenu = contexMenu;
}
}
MouseEventClass就是绑定事件的过程,我们这里只列出部分代码,未列出的事件绑定的部分同理。
export class MouseEventClass {
x: number;
y: number;
down: boolean;
dragging: boolean;
scroll: boolean;
lastX: number;
lastY: number;
startX: number;
startY: number;
moveCount: number;
eventInfo: MouseEventInfo;
constructor(eInfo: MouseEventInfo) {
this.moveCount = 0;
this.x = 0;
this.y = 0;
this.lastX = 0;
this.lastY = 0;
this.startX = 0;
this.startY = 0;
this.down = false;
this.eventInfo = eInfo;
this.dragging = false;
this.scroll = false;
var on = impressEvent.on;
var self = this;
if (this.eventInfo.el) {
on(this.eventInfo.el, "mousedown", function (e) {
if (e.button == 1 || e.button == 2) {
e.preventDefault();
return false;
}
self.mousedownHandler(e);
});
(this.eventInfo.ondragMove || this.eventInfo.onmouseMove) && on(this.eventInfo.el, "mousemove", function (e) {
if (e.button == 1 || e.button == 2) {
e.preventDefault();
return false;
}
self.mousemoveHandler(e);
});
(this.eventInfo.ondragUp || this.eventInfo.onmouseUp) && on(this.eventInfo.el, "mouseup", function (e) {
if (e.button == 1 || e.button == 2) {
e.preventDefault();
return false;
}
self.mouseupHandler(e);
});
this.eventInfo.mouseWheel && on(this.eventInfo.el, "mousewheel", function (e) {
if (e.button == 1 || e.button == 2) {
e.preventDefault();
return false;
}
self.mouseWheelHandler(e);
});
this.eventInfo.onContextMenu && on(this.eventInfo.el, "contextmenu", function (e) {
if (e.button == 1) {
e.preventDefault();
return false;
}
e.preventDefault();
self.contextMenuHandler(e);
})
};
}
mousedownHandler = function (evt: MouseEvent) {
this.moveCount = 1;
this.down = true;
this.startX = evt.pageX;
this.startY = evt.pageY;
this.dragging = false;
this.eventInfo.el && this.eventInfo.onmouseDown && (this.eventInfo.onmouseDown({
evt: evt,
target: this.eventInfo.el,
mouseX: this.startX - leftWidth,
mouseY: this.startY - topHeight - topAnimaitonHeight,
buttonCode: evt.button
}));
this.lastX = evt.pageX;
this.lastY = evt.pageY;
};
mousemoveHandler = function (evt: MouseEvent) {
this.moveCount++;
this.x = evt.pageX;
this.y = evt.pageY;
if (this.down && (this.x - this.startX != 0 || this.y - this.startY != 0)) {
this.dragging = true;
}
if (this.dragging) {
this.eventInfo.ondragMove && this.eventInfo.ondragMove({
evt: evt,
isFirstMove: this.moveCount == 1 ? true : false,
mouseX: this.x - leftWidth,
mouseY: this.y - topHeight - topAnimaitonHeight,
downX: this.startX - leftWidth,
downY: this.startY - topHeight - topAnimaitonHeight,
lastX: this.lastX - leftWidth,
lastY: this.lastY - topHeight - topAnimaitonHeight,
noRoteDiffX: this.x - this.lastX,
noRoteDiffY: this.y - this.lastY,
totalX: this.x - this.startX,
totalY: this.y - this.startY
})
} else {
this.eventInfo.onmouseMove && this.eventInfo.onmouseMove({
evt: evt,
mouseX: this.x - leftWidth,
mouseY: this.y - topHeight - topAnimaitonHeight,
downX: this.startX - leftWidth,
downY: this.startY - topHeight - topAnimaitonHeight,
lastX: this.lastX - leftWidth,
lastY: this.lastY - topHeight - topAnimaitonHeight,
noRoteDiffX: this.x - this.lastX,
noRoteDiffY: this.y - this.lastY,
totalX: this.x - this.startX,
totalY: this.y - this.startY
})
}
this.lastX = evt.pageX;
this.lastY = evt.pageY;
};
如何使用上面我们创建的两个类呢
我们在使用的时候先将MouseEventInfo这个类初始化,然后再用MouseEventClass绑定事件。
this.mouseEventInfo = new Common.MouseEventInfo(document, this.mouseDown, this.dragMove, this.dragUp, this.mouseMove, this.mouseUp, this.mouseWheel, this.onContextMenu); new Common.MouseEventClass(this.mouseEventInfo);
我们绑定事件的元素是document,操作软件的所有事件都会走我们绑定的以下函数:
当然最重要的操做画布的响应也依赖于我们的事件处理逻辑:
this.mouseDown:鼠标按下的事件处理逻辑,例如存储当前的鼠标值
this.dragMove:鼠标拖拽移动,例如移动某个元素或者移动画布
this.dragUp:鼠标松开(拖拽后松开鼠标),例如停止移动
this.mouseMove:鼠标移动(不是按住状态),例如让元素显示hover状态
this.mouseUp:鼠标松开(mousedown后马上松开),例如触发某个元素的编辑状态
this.mouseWheel:鼠标滚动,例如缩放画布
this.onContextMenu:鼠标点击右键,例如显示右键菜单
软件项目技术点(6)——结合鼠标操作绘制动态canvas画布的更多相关文章
- 软件项目技术点(1)——d3.interpolateZoom-在两个点之间平滑地缩放平移
AxeSlide软件项目梳理 canvas绘图系列知识点整理 软件参考d3的知识点 我们在软件中主要用到d3.js的核心函数d3.interpolateZoom - 在两个点之间平滑地缩放平移.请 ...
- 软件项目技术点(5)——在canvas上绘制动态网格线
AxeSlide软件项目梳理 canvas绘图系列知识点整理 grid类的实现 当鼠标在画布上缩放时,网格能跟着我的鼠标滚动而相应的有放大缩小的效果. 下面是具体实现的代码,draw函数里计算出大 ...
- 软件项目技术点(2)——Canvas之平移translate、旋转rotate、缩放scale
AxeSlide软件项目梳理 canvas绘图系列知识点整理 画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transf ...
- 软件项目技术点(2)——Canvas之坐标系转换
AxeSlide软件项目梳理 canvas绘图系列知识点整理 默认坐标系与当前坐标系 canvas中的坐标是从左上角开始的,x轴沿着水平方向(按像素)向右延伸,y轴沿垂直方向向下延伸.左上角坐标为 ...
- 软件项目技术点(2)——Canvas之获取Canvas当前坐标系矩阵
AxeSlide软件项目梳理 canvas绘图系列知识点整理 前言 在我的另一篇博文 Canvas坐标系转换 中,我们知道了所有的平移缩放旋转操作都会影响到画布坐标系.那在我们对画布进行了一系列操 ...
- 软件项目技术点(1)——Tween算法及缓动效果
AxeSlide软件项目梳理 canvas绘图系列知识点整理 Tween算法及缓动效果 软件里在切换步序时需要有过渡动画效果,从当前位置的画面缓动到目标位置的画面.动画效果可重新查看文章系列第一篇 ...
- 软件项目技术点(7)——在canvas上绘制自定义图形
AxeSlide软件项目梳理 canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...
- 软件项目技术点(9)——如何将gif动态图拆分绘制
AxeSlide软件项目梳理 canvas绘图系列知识点整理 背景介绍 我们的软件支持插入gif图片,并且展示在软件里是动态的,例如插入下面这张gif图. 在软件里显示的同样是这样的动态效果: 那 ...
- 软件项目技术点(8)—— canvas调用drawImage绘制图片
AxeSlide软件项目梳理 canvas绘图系列知识点整理 html5中标签canvas,函数drawImage(): 使用drawImage()方法绘制图像.绘图环境提供了该方法的三个不同版本 ...
随机推荐
- iOS开发中UILocalNotification本地通知实现简单的提醒功能
这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...
- 1.Bootstrap简介
Bootstrap简介 BootstrapAPI: https://v3.bootcss.com Bootstrap优点: 响应式布局,一个框架,多种设备适用 Bootstrap 是最受欢迎的 HTM ...
- 动手玩转Docker(二)
CentOS7下安装docker: 通过命令uname -r 查看linux内核版本,版本低的话不支持docker. [tim@num root]$ uname -r 3.10.0-514.el7.x ...
- Maximum call stack size exceeded
写vue时报了如下错误 Maximum call stack size exceeded 栈溢出,因为在调用函数时使用了递归调用,而且没有写跳出条件,导致了该错误
- WebFrom页面绑定数据过于冗长的处理方法
嘛 这个是当时写完东西之后 功能没什么问题 但是由于页面绑定的数据太长 破坏了整体的样式(对于本人来说 样式就是浮云....) 所以测试就跟我说必须弄好看点 于是乎 我就找到了下面这种方法 因为我这 ...
- OPPO R11s在哪里打开Usb调试模式的简单方法
就在我们使用pc链接安卓手机的时候,如果手机没有开启usb调试模式,pc则不能够成功读到我们的手机,如果遇到此情况我们需要找方法将手机的usb调试模式打开,本文我们记录一下OPPO R11s如何开启u ...
- Python用于http/https接口自动化
本接口自动化框架主要用到的类: 1. unittest:组织测试用例 2. requests:http/https请求 3. HTMLTestRunner:生成测试报告 4. Dingtalkchat ...
- Session.Abandon和Session.Clear有何不同 (转)
Session.Clear()就是把Session对象中的所有项目都删除了, Session对象里面啥都没有.但是Session对象还保留.Session.Abandon()就是把当前Session对 ...
- spring项目中aop的使用
AOP:是一种面向切面的编程范式,是一种编程思想,旨在通过分离横切关注点,提高模块化,可以跨越对象关注点.Aop的典型应用即spring的事务机制,日志记录.利用AOP可以对业务逻辑的各个部分进行隔离 ...
- unittest之断言
在测试用例中,执行完测试用例后,最后一步是判断测试结果是 pass 还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert).用 unittest 组件测试用例的时候,断言 ...