软件项目技术点(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()方法绘制图像.绘图环境提供了该方法的三个不同版本 ...
随机推荐
- flask_restful(转载)
flask插件系列之flask_restful设计API 前言 flask框架默认的路由和视图函数映射规则是通过在视图函数上直接添加路由装饰器来实现的,这使得路由和视图函数的对应关系变得清晰,但对于统 ...
- 基础篇:6)形位公差标注(GD&T标准)-总章
本章目的:理解GD&T概念,读懂和绘制GD&T图纸.本章是GD&T指引章节. 1.GD&T概念 GD&T 是 Geometric Dimensioning ...
- 【算法笔记】B1049 数列的片段和
1049 数列的片段和 (20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, ...
- FreeRTOS-01移植及任务创建和删除
根据正点原子FreeRTOS视频整理 单片机:STM32F207VC FreeRTOS源码版本:v10.0.1 任务创建和删除API函数: 工程列表: 1. main.c /**/ #include ...
- MSSQL远程连接操作(转)
--遠程連接操作 /****************************************************************************************** ...
- input 标签只能输入数字
$("input[name='contact']").keyup(function(){ $("input[name='contact']").attr(&qu ...
- 关于DES加密强制更新版(4.22)
数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的.通常,自动取款 ...
- Python学习 day11
一.装饰器 装饰器是在不改变函数调用方式的情况下,需要在函数前后新增功能.有些类似aop,不知道原理是否相同,表现是一样的. 装饰器严格遵守了“开放封闭原则” 1.基本装饰器 def wrapper( ...
- Centos7修改主机名称、DNS、网卡信息
1 hostnamectl set-hostname wangshuyi 2 vi /etc/hostname 3 vi /etc/resolv.conf 4 vi /etc/sysconfig/ne ...
- WPF的布局--DockPanel
1.DockPanel: 以上.下.左.右.中为基本结构的布局方式 类似于Java AWT布局中的BorderLayout. 但与BorderLayout不同的是,每一个区域可以同时放置多个控件,在同 ...