Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net &引用;这";paper.js事件中的关键字转换为constractor_.net_Typescript_Paperjs - Fatal编程技术网

.net &引用;这";paper.js事件中的关键字转换为constractor

.net &引用;这";paper.js事件中的关键字转换为constractor,.net,typescript,paperjs,.net,Typescript,Paperjs,我尝试在paper.js和typescript之间进行交互 所以我想在PenTool的constractor中编写一些事件处理程序。 (因为我想在工具创建时使用DI并定义所有图纸事件) 我有下一段代码: export class PenTool implements BaseTool { name: string; toolType: Tools; tool: any; drawingContext: any; path: any; constructor(private paperServ

我尝试在paper.js和typescript之间进行交互

所以我想在
PenTool
的constractor中编写一些事件处理程序。 (因为我想在工具创建时使用DI并定义所有图纸事件)

我有下一段代码:

export class PenTool implements BaseTool {

name: string;
toolType: Tools;
tool: any;
drawingContext: any;
path: any;

constructor(private paperService: PaperService) {

    this.name = "Pen";
    this.toolType = Tools.Pen;
    this.drawingContext = this.paperService.getDrawingContext();

    this.tool = new this.drawingContext.Tool();

    this.tool.onMouseDown = function (event) {

        //!!!!!!!!!!!!!!!!!!!
        //I want to use my class property here (this.name) or
        //(this.drawingContext) etc, but I can't!

        this.path = new (paperService.getDrawingContext()).Path();
        //this.path = new this.drawingContext.Path();
        this.path.add(event.point, event.point);
        this.path.strokeColor = 'black';
    }

    this.tool.onMouseDrag = function (event) {

        console.log('pen -> mouse drag!');

        if (event.modifiers.shift) {
            this.path.lastSegment.point = event.point;
        } else {
            this.path.add(event.point);
        }
    }
}

}
paperService
给我纸张变量,创建新的paperScope等。 问题是我无法访问事件函数中的类属性

我做错了什么?
提前感谢。

使用箭头函数来保持相同的上下文

导出类PenTool实现BaseTool{
名称:字符串;
工具类型:工具;
工具:任何;
drawingContext:任何;
路径:任意;
建造商(私人文书服务:文书服务){
this.name=“Pen”;
this.toolType=Tools.Pen;
this.drawingContext=this.paperService.getDrawingContext();
this.tool=新建this.drawingContext.tool();
this.tool.onMouseDown=(事件)=>{
//!!!!!!!!!!!!!!!!!!!
//我想在这里使用我的类属性(this.name)或
//(这个.drawingContext)等等,但我不能!
this.path=new(paperService.getDrawingContext()).path();
//this.path=新建this.drawingContext.path();
this.path.add(event.point,event.point);
this.path.strokeColor='black';
}
this.tool.onMouseDrag=(事件)=>{
log('pen->mouse drag!');
if(event.modifiers.shift){
this.path.lastSegment.point=event.point;
}否则{
this.path.add(event.point);
}
}
}
}

这篇文章提供了更多关于这个如何在javascript中工作的信息。

它的可能副本正是我所需要的!非常感谢。