简体   繁体   English

Easeljs中的位图不适用于事件侦听器吗?

[英]Bitmap in Easeljs does not work with event listeners?

I'm trying to create a draggable background image in my canvas. 我正在尝试在画布中创建可拖动的背景图像。 Currently I have something like this: 目前我有这样的事情:

var stage = new createjs.Stage("canvas");
createjs.Ticker.addEventListener("tick", tick);

var dragContainer = new createjs.Container();
stage.addChild(dragContainer);

dragContainer.on("pressmove", function (event) {
    event.currentTarget.x = event.stageX;
    event.currentTarget.y = event.stageY;
    stage.update();
});

// Add a thing
var shape = new createjs.Shape();
shape.graphics.beginFill("#000");
shape.graphics.drawRect(0, 0, 50, 50);

dragContainer.addChild(shape);

// Update the stage
function tick(event) {
    stage.update();
}

This works perfectly fine, everything is draggable, etc. The problem is, when I replace the shape drawn with graphics with a bitmap (which is the functionality I want), it doesn't work. 这工作得很好,一切都可以拖动,等等。问题是,当我用位图替换图形绘制的形状(这是我想要的功能)时,它不起作用。

    //Add a thing, this doesn't work
    var shape = new createjs.Bitmap("http://www.graphic-design-employment.com/images/create-a-simple-shape.jpg")
    dragContainer.addChild(shape);

Here's a jsfiddle: http://jsfiddle.net/frozensoviet/5heLu2L1/ . 这是一个jsfiddle: http : //jsfiddle.net/frozensoviet/5heLu2L1/ Any ideas what I'm doing wrong? 有什么想法我做错了吗?

You are likely seeing cross-origin errors. 您可能会看到跨域错误。 Your console should be showing you the error. 您的控制台应该向您显示错误。 This happens because EaselJS must access the stage pixels to determine when the content is intersected by the mouse. 发生这种情况是因为EaselJS必须访问舞台像素才能确定鼠标何时将内容相交。

I modified your fiddle to load an image from a server that supports CORS. 我修改了您的小提琴以从支持CORS的服务器加载图像。 There is an htaccess file in the folder, which has the line: 文件夹中有一个htaccess文件,其中包含以下行:

Header set Access-Control-Allow-Origin "*"

Then, it is just a matter of flagging the content as crossOrigin. 然后,只需将内容标记为crossOrigin。 Note that the crossOrigin flag must be set BEFORE you load the source. 请注意,必须在加载源之前设置crossOrigin标志。

var img = new Image();
img.crossOrigin = "Anonymous";
img.src = "http://playpen.createjs.com/CORS/duck.png";
var shape = new createjs.Bitmap(img);

EaselJS doesn't have built-in support for CORS, so you have to specify it on your images before you load and pass them in. PreloadJS does have CORS support. EaselJS没有对CORS的内置支持,因此在加载和传递图像之前必须在图像上指定它。PreloadJS确实具有CORS支持。

If you can't support CORS on the server, there is another workaround. 如果您不能在服务器上支持CORS,则还有另一种解决方法。 Specify a hitArea on the image that is the same size, and it will draw that instead of the image when doing hit-detection. 在图像上指定相同大小的hitArea,当进行命中检测时,它将绘制图像而不是图像。 Here is an updated Fiddle: http://jsfiddle.net/lannymcnie/5heLu2L1/2/ 这是更新的小提琴: http : //jsfiddle.net/lannymcnie/5heLu2L1/2/

var shape = new createjs.Bitmap("http://playpen.createjs.com/CORS/duck.png");
shape.image.onload = function() {
    var hitArea = new createjs.Shape();
    hitArea.graphics.f("#000").dr(0,0,shape.image.width,shape.image.height);
    shape.hitArea = hitArea;
}

Hope that helps! 希望有帮助!

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

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