《TypeScript图形渲染实战:2D架构设计与实现》 —3.2.8 将DOM Event事件转换为CanvasInpu
3.2.8 将DOM Event事件转换为CanvasInputEvent事件
使用了自己定义的CanvasInputEvent事件体系,因此需要将DOM Event事件转换为CanvasInputEvent对应的子类。具体代码如下:
// 将DOM Event对象信息转换为自己定义的CanvasMouseEvent事件
private _toCanvasMouseEvent ( evt : Event ) : CanvasMouseEvent {
// 向下转型,将Event转换为MouseEvent
let event : MouseEvent = evt as MouseEvent ;
// 将客户区的鼠标pos变换到Canvas坐标系中表示
let mousePosition : vec2 = this . _viewportToCanvasCoordinate ( event ) ;
// 将Event一些要用到的信息传递给CanvasMouseEvent并返回
let canvasMouseEvent : CanvasMouseEvent = new CanvasMouseEvent
( mousePosition , event . button , event . altKey , event . ctrlKey ,
event .shiftKey ) ;
return canvasMouseEvent ;
}
// 将DOM Event对象信息转换为自己定义的Keyboard事件
private _toCanvasKeyBoardEvent ( evt : Event ) : CanvasKeyBoardEvent {
let event : KeyboardEvent = evt as KeyboardEvent ;
// 将Event一些要用到的信息传递给CanvasKeyBoardEvent并返回
let canvasKeyboardEvent : CanvasKeyBoardEvent = new CanvasKeyBoardEvent
( event . key , event . keyCode , event . repeat , event . altKey , event .
ctrlKey , event . shiftKey ) ;
return canvasKeyboardEvent ;
}
- 点赞
- 收藏
- 关注作者
评论(0)