简体   繁体   English

Animate CC HTML5 easylJS stage.X stage.Y鼠标位置在chrome和firefox之间不一致

[英]Animate CC HTML5 easelJS stage.X stage.Y mouse position not consistent between chrome and and firefox

I have an application where I need an information card to follow the position of the mouse. 我有一个需要信息卡来跟踪鼠标位置的应用程序。

I have used the following code: 我使用了以下代码:

stage.addEventListener("tick", fl_CustomMouseCursor.bind(this));

function fl_CustomMouseCursor() {

    this.NameTag.x = stage.mouseX;
    this.NameTag.y = stage.mouseY;

}

It works perfectly in Firefox but in Chrome and Safari the further away the mouse gets from 0,0 on the canvas the larger the distance between NameTag and the mouse (by a factor of 2). 它在Firefox中可完美运行,但在Chrome和Safari中,鼠标距画布上的0,0越远,NameTag与鼠标之间的距离就越大(系数为2)。

I look forward to any comments. 我期待任何评论。

I see the issue in Chrome as well as Firefox - I don't believe it is a browser issue. 我在Chrome和Firefox中都看到了该问题-我认为这不是浏览器问题。

The problem is that the stage itself is being scaled. 问题在于舞台本身正在缩放。 This means that the coordinates are multiplied by that value. 这意味着坐标乘以该值。 You can get around it by using globalToLocal on the stage coordinates, which brings them into the coordinate space of the exportRoot ( this in your function). 您可以通过在舞台坐标上使用globalToLocal解决该问题,这会将它们带入exportRoot的坐标空间( this函数在函数中)。 I answered a similar question here , which was caused by Animate's responsive layout support. 在这里回答了类似的问题 ,这是由Animate的响应式布局支持引起的。

Here is a modified function: 这是修改后的函数:

function fl_CustomMouseCursor() {
    var p = this.globalToLocal(stage.mouseX, stage.mouseY);
    this.NameTag.x = p.x;
    this.NameTag.y = p.y;
}

You can also clean up the code using the "stagemousemove" event (which is fired only on mouse move events on the stage), and the on() method, which can do function binding for you (among other things): 您还可以使用“ stagemousemove”事件(仅在舞台上的鼠标移动事件上触发)和on()方法清理代码,该方法可以为您执行功能绑定(除其他外):

stage.on("stagemousemove", function(event) {
    var p = this.globalToLocal(stage.mouseX, stage.mouseY);
    this.NameTag.x = p.x;
    this.NameTag.y = p.y;
}, this);

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM