简体   繁体   English

穿插 KineticJS 和 jQuery 事件

[英]Interspersing KineticJS and jQuery events

I am looking to build a simple photo collage using KineticJS and Canvas.我正在寻找使用 KineticJS 和 Canvas 构建一个简单的照片拼贴。 In the canvas container, I have pre-defined image areas.在画布容器中,我有预定义的图像区域。 I also have (in the DOM and outside of the canvas container), a strip of images which I want to drag and drop to the image wells on the canvas.我也有(在 DOM 和画布容器外部),我想拖放到画布上的图像井中的一条图像。 I am able to drag an image and snap it to a (hard-coded) image well but while the image is moving on canvas I'm unable to get the x and y co-ordinates.我能够拖动图像并将其很好地捕捉到(硬编码)图像,但是当图像在画布上移动时,我无法获得 x 和 y 坐标。 I've tried using jQuery's drag/drag event but then it is blind to the canvas and I've tried to use use the KineticJS's dragover function but then I can't get the DOM image's x and y.我试过使用 jQuery 的拖动/拖动事件,但它对画布视而不见,我试过使用 KineticJS 的拖动功能,但我无法获得 DOM 图像的 x 和 y。 The idea is that knowing the x,y of the image being dragged I can write some logic to figure out which image location it should get snapped to - as opposed to hard coding it's target.这个想法是知道被拖动的图像的 x,y 我可以编写一些逻辑来确定它应该被捕捉到哪个图像位置 - 而不是硬编码它的目标。

Is it even possible to do this with KineticJS+jQuery - drag images from a DOM onto a canvas and have them snap into pre-defined image areas?甚至可以使用 KineticJS+jQuery 做到这一点 - 将图像从 DOM 拖到画布上并让它们卡入预定义的图像区域? Or, is there a simpler way?或者,有没有更简单的方法?

as far as I understand your problem, I tried to reproduce it, but not seeing any difficulties据我了解你的问题,我试图重现它,但没有看到任何困难

    <div width="600" height="500" style="background-color:green;padding-left:60px" id="wrap">
    <canvas id="a" width="300" height="225" style="background-color:yellow"></canvas>
</div>
<img id="dragMe" src="http://www.linuxmint.com/img/logo.png"/>
<div id="log">
    <span class="a"></span>
    <span class="b"></span>
    <span class="c"></span>
        <span class="d"></span>
</div>

$("#wrap").mousemove(function(e){
  var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
  var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
  $("#log > span.a").text("( e.pageX, e.pageY ) : " + pageCoords);
  $("#log > span.b").text("( e.clientX, e.clientY ) : " + clientCoords);
});
var p = $('#a').position();
$("#dragMe").draggable({
    drag: function(event, ui) {
        $("#log > span.c").text(  ui.offset.left+':' +ui.offset.top);
        $("#log > span.d").text( ( ui.offset.top-p.top)+':' +(ui.offset.left - p.left));
    }
});

http://jsfiddle.net/agj3N/1/ http://jsfiddle.net/agj3N/1/

When I asked you if you had a reason you had the strip of images outside of the KineticJS stage, you answered:当我问你是否有理由在 KineticJS 阶段之外拥有图像条时,你回答说:

Only because when I toJSON it (for saving and calling up for a future edit), I just want the images on my photo collage and not the photos on the strip.只是因为当我使用它(用于保存和调用以供将来编辑)时,我只想要照片拼贴上的图像,而不是条带上的照片。

My answer to this is:我对此的回答是:

You don't have to use toJSON on the entire stage.您不必在整个阶段都使用 toJSON。

If you separate the Photo Strip (previously outside of the canvas) into a different layer from the Collage (previously inside the canvas, then you can have two layers and use toJSON on only one of those layers!如果您将照片带(以前在画布外部)与拼贴画(以前在画布内部)分开到不同的层中,那么您可以有两个层,并且仅在其中一个层上使用toJSON

The result will be that toJSON will only serialize the objects from that given layer, which sounds like what you need.结果将是toJSON只会序列化来自给定层的对象,这听起来像您所需要的。

jsfiddle - here is an example to illustrate what I mean, excuse my poor logic for snapping the image to the group. jsfiddle - 这是一个示例来说明我的意思,请原谅我将图像捕捉到组的糟糕逻辑。 I have 2 layers: photoStripLayer and collageLayer我有 2 层: photoStripLayercollageLayer

  1. Click on the Collage toJSON button.单击拼贴 toJSON 按钮。 Notice the console.log output does not include any of the images.请注意,console.log 输出不包括任何图像。 This is because collageLayer only has a group inside it that has a child rectangle.这是因为collageLayer内部只有一个具有子矩形的组。

  2. Drag the first yoda (top left, the rest don't work this is just an example) into the red box.将第一个尤达(左上角,其余不起作用,这只是一个示例)拖到红色框中。 Sorry you'll have to play around with this to get it to snap properly (I'm assuming you have your "snap" code already)抱歉,您必须尝试使用​​它才能正确捕捉(我假设您已经有了“捕捉”代码)

  3. On dragend if the Yoda node is inside the red box, it will use KineticJS' moveTo function to move the yoda from the photoStripLayer --> collageLayer .dragend如果 Yoda 节点在红框内,它将使用 KineticJS 的moveTo函数从photoStripLayer --> collageLayer移动 yoda。 You'll know it worked when the Yoda snaps to position (0,0) relative to the new group.当 Yoda 捕捉到相对于新组的位置 (0,0) 时,您就会知道它起作用了。

  4. Now Click on the Collage toJSON button.现在单击 Collage toJSON 按钮。 Notice that the yoda image is now a part of the toJSON console.log output.请注意,yoda 图像现在是 toJSON console.log 输出的一部分。 The yoda was moved to the new group, which is a child of collageLayer . yoda 被移动到新组,它是collageLayer的孩子。 Now part of the collageLayer, Yoda is .现在是 collageLayer 的一部分,Yoda 是.

Here's the dragend code:这是dragend代码:

             node.on('dragend', function () {
                var pos = stage.getMousePosition();
                var X = pos.x;
                var Y = pos.y;
                var minX = snap.getX();
                var maxX = snap.getX() + snap.getWidth();
                var minY = snap.getY();
                var maxY = snap.getY() + snap.getHeight();
                if (node.getX() < minX) {
                    node.moveTo(snap);
                    node.setX(0);
                    node.setY(0);
                }
                if (node.getX() > maxX) {
                    node.moveTo(snap);
                    node.setX(0);
                    node.setY(0);
                }
                if (node.getY() < minY) {
                    node.moveTo(snap);
                    node.setX(0);
                    node.setY(0);
                }
                if (node.getY() > maxY) {
                    node.moveTo(snap);
                    node.setX(0);
                    node.setY(0);
                }
                photoStripLayer.draw();
                collageLayer.draw();
            });

And the button click code to illustrate using toJSON:和按钮单击代码来说明使用 toJSON:

            function collageToJSON() {
                var cjson = collageLayer.toJSON();
                console.log(cjson);
                /* To illustrate, you can also call toDataURL here. But in JSFiddle I think it throws a Security Error.
                collageLayer.toDataURL({
                    callback: function(dataUrl) {
                        window.open(dataUrl);
                    }
                });
                */
            }

            document.getElementById('CtoJSON').addEventListener('click', function () {
                collageToJSON();
            });

And there you have it!你有它! The issue is solved by letting KineticJS handle the entire process.通过让KineticJS处理整个过程来解决这个问题。

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

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