Dragging a Konva layer from empty areas












0














I'd like to drag a Konva layer even if empty. I see that I can drag the layer only where objects are present (the code shows that you can drag where the rectangle is but nowhere else).



How can I drag the layer dragging empty parts?



Moreover, I see that also the mouseenter and mousleave aren't respected if you are out of the objects of the layer.






<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
<body>
<script src="js/modulePattern.js"></script>
<script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



<div id="container"></div>
<script>

function demo() {

var stage = new Konva.Stage({
container: 'container',
width: 100,
height: 100
});

var layer = new Konva.Layer({draggable: true});
stage.add(layer);

layer.on("dragmove", function() {
console.log(this.x(), this.y())
});

layer.on("mouseenter", function() {
stage.container().style.cursor = 'pointer';
});

layer.on("mouseleave", function() {
stage.container().style.cursor = 'default';
});

var rect = new Konva.Rect({
width: 50,
height: 50,
fill: 'red',
stroke: 'black',
strokeWidth: 5
});

layer.add(rect);

stage.draw();
}

demo();
</script>

</body>
</html>












share|improve this question





























    0














    I'd like to drag a Konva layer even if empty. I see that I can drag the layer only where objects are present (the code shows that you can drag where the rectangle is but nowhere else).



    How can I drag the layer dragging empty parts?



    Moreover, I see that also the mouseenter and mousleave aren't respected if you are out of the objects of the layer.






    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    </head>
    <body>
    <script src="js/modulePattern.js"></script>
    <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



    <div id="container"></div>
    <script>

    function demo() {

    var stage = new Konva.Stage({
    container: 'container',
    width: 100,
    height: 100
    });

    var layer = new Konva.Layer({draggable: true});
    stage.add(layer);

    layer.on("dragmove", function() {
    console.log(this.x(), this.y())
    });

    layer.on("mouseenter", function() {
    stage.container().style.cursor = 'pointer';
    });

    layer.on("mouseleave", function() {
    stage.container().style.cursor = 'default';
    });

    var rect = new Konva.Rect({
    width: 50,
    height: 50,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 5
    });

    layer.add(rect);

    stage.draw();
    }

    demo();
    </script>

    </body>
    </html>












    share|improve this question



























      0












      0








      0







      I'd like to drag a Konva layer even if empty. I see that I can drag the layer only where objects are present (the code shows that you can drag where the rectangle is but nowhere else).



      How can I drag the layer dragging empty parts?



      Moreover, I see that also the mouseenter and mousleave aren't respected if you are out of the objects of the layer.






      <!DOCTYPE html>
      <html>
      <head lang="en">
      <meta charset="UTF-8">
      </head>
      <body>
      <script src="js/modulePattern.js"></script>
      <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



      <div id="container"></div>
      <script>

      function demo() {

      var stage = new Konva.Stage({
      container: 'container',
      width: 100,
      height: 100
      });

      var layer = new Konva.Layer({draggable: true});
      stage.add(layer);

      layer.on("dragmove", function() {
      console.log(this.x(), this.y())
      });

      layer.on("mouseenter", function() {
      stage.container().style.cursor = 'pointer';
      });

      layer.on("mouseleave", function() {
      stage.container().style.cursor = 'default';
      });

      var rect = new Konva.Rect({
      width: 50,
      height: 50,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 5
      });

      layer.add(rect);

      stage.draw();
      }

      demo();
      </script>

      </body>
      </html>












      share|improve this question















      I'd like to drag a Konva layer even if empty. I see that I can drag the layer only where objects are present (the code shows that you can drag where the rectangle is but nowhere else).



      How can I drag the layer dragging empty parts?



      Moreover, I see that also the mouseenter and mousleave aren't respected if you are out of the objects of the layer.






      <!DOCTYPE html>
      <html>
      <head lang="en">
      <meta charset="UTF-8">
      </head>
      <body>
      <script src="js/modulePattern.js"></script>
      <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



      <div id="container"></div>
      <script>

      function demo() {

      var stage = new Konva.Stage({
      container: 'container',
      width: 100,
      height: 100
      });

      var layer = new Konva.Layer({draggable: true});
      stage.add(layer);

      layer.on("dragmove", function() {
      console.log(this.x(), this.y())
      });

      layer.on("mouseenter", function() {
      stage.container().style.cursor = 'pointer';
      });

      layer.on("mouseleave", function() {
      stage.container().style.cursor = 'default';
      });

      var rect = new Konva.Rect({
      width: 50,
      height: 50,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 5
      });

      layer.add(rect);

      stage.draw();
      }

      demo();
      </script>

      </body>
      </html>








      <!DOCTYPE html>
      <html>
      <head lang="en">
      <meta charset="UTF-8">
      </head>
      <body>
      <script src="js/modulePattern.js"></script>
      <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



      <div id="container"></div>
      <script>

      function demo() {

      var stage = new Konva.Stage({
      container: 'container',
      width: 100,
      height: 100
      });

      var layer = new Konva.Layer({draggable: true});
      stage.add(layer);

      layer.on("dragmove", function() {
      console.log(this.x(), this.y())
      });

      layer.on("mouseenter", function() {
      stage.container().style.cursor = 'pointer';
      });

      layer.on("mouseleave", function() {
      stage.container().style.cursor = 'default';
      });

      var rect = new Konva.Rect({
      width: 50,
      height: 50,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 5
      });

      layer.add(rect);

      stage.draw();
      }

      demo();
      </script>

      </body>
      </html>





      <!DOCTYPE html>
      <html>
      <head lang="en">
      <meta charset="UTF-8">
      </head>
      <body>
      <script src="js/modulePattern.js"></script>
      <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



      <div id="container"></div>
      <script>

      function demo() {

      var stage = new Konva.Stage({
      container: 'container',
      width: 100,
      height: 100
      });

      var layer = new Konva.Layer({draggable: true});
      stage.add(layer);

      layer.on("dragmove", function() {
      console.log(this.x(), this.y())
      });

      layer.on("mouseenter", function() {
      stage.container().style.cursor = 'pointer';
      });

      layer.on("mouseleave", function() {
      stage.container().style.cursor = 'default';
      });

      var rect = new Konva.Rect({
      width: 50,
      height: 50,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 5
      });

      layer.add(rect);

      stage.draw();
      }

      demo();
      </script>

      </body>
      </html>






      javascript konvajs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 23:31







      cdarwin

















      asked Nov 12 '18 at 22:14









      cdarwincdarwin

      1,45872952




      1,45872952
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Konva does not detect events on empty space of layers. And it is unclear how can it do it if you have several layers on the stage.



          If you still need to listen on empty layers and make a layer draggable at any space you can:




          1. Make stage draggable instead and listen events on it (click will work on empty space too).


          2. Add a transparent rectangle as a background, so it will be used to listen to events:






          <!DOCTYPE html>
          <html>
          <head lang="en">
          <meta charset="UTF-8">
          </head>
          <body>
          <script src="js/modulePattern.js"></script>
          <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



          <div id="container"></div>
          <script>

          function demo() {

          var stage = new Konva.Stage({
          container: 'container',
          width: 300,
          height: 300
          });

          var layer = new Konva.Layer({draggable: true});
          stage.add(layer);


          layer.on("mouseenter", function() {
          stage.container().style.cursor = 'pointer';
          });

          layer.on("mouseleave", function() {
          stage.container().style.cursor = 'default';
          });

          var rect = new Konva.Rect({
          width: 50,
          height: 50,
          fill: 'red',
          stroke: 'black',
          strokeWidth: 5
          });

          var back = new Konva.Rect({
          fill: 'red',
          width: stage.width(),
          height: stage.height()
          })
          layer.add(back);

          // move rectangle so it always stay on the screen, no matter where layer it
          layer.on('dragend', function() {
          back.setAbsolutePosition({x: 0, y: 0});
          layer.draw();
          });



          layer.add(rect);

          stage.draw();
          }

          demo();
          </script>

          </body>
          </html>








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270863%2fdragging-a-konva-layer-from-empty-areas%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Konva does not detect events on empty space of layers. And it is unclear how can it do it if you have several layers on the stage.



            If you still need to listen on empty layers and make a layer draggable at any space you can:




            1. Make stage draggable instead and listen events on it (click will work on empty space too).


            2. Add a transparent rectangle as a background, so it will be used to listen to events:






            <!DOCTYPE html>
            <html>
            <head lang="en">
            <meta charset="UTF-8">
            </head>
            <body>
            <script src="js/modulePattern.js"></script>
            <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



            <div id="container"></div>
            <script>

            function demo() {

            var stage = new Konva.Stage({
            container: 'container',
            width: 300,
            height: 300
            });

            var layer = new Konva.Layer({draggable: true});
            stage.add(layer);


            layer.on("mouseenter", function() {
            stage.container().style.cursor = 'pointer';
            });

            layer.on("mouseleave", function() {
            stage.container().style.cursor = 'default';
            });

            var rect = new Konva.Rect({
            width: 50,
            height: 50,
            fill: 'red',
            stroke: 'black',
            strokeWidth: 5
            });

            var back = new Konva.Rect({
            fill: 'red',
            width: stage.width(),
            height: stage.height()
            })
            layer.add(back);

            // move rectangle so it always stay on the screen, no matter where layer it
            layer.on('dragend', function() {
            back.setAbsolutePosition({x: 0, y: 0});
            layer.draw();
            });



            layer.add(rect);

            stage.draw();
            }

            demo();
            </script>

            </body>
            </html>








            share|improve this answer


























              1














              Konva does not detect events on empty space of layers. And it is unclear how can it do it if you have several layers on the stage.



              If you still need to listen on empty layers and make a layer draggable at any space you can:




              1. Make stage draggable instead and listen events on it (click will work on empty space too).


              2. Add a transparent rectangle as a background, so it will be used to listen to events:






              <!DOCTYPE html>
              <html>
              <head lang="en">
              <meta charset="UTF-8">
              </head>
              <body>
              <script src="js/modulePattern.js"></script>
              <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



              <div id="container"></div>
              <script>

              function demo() {

              var stage = new Konva.Stage({
              container: 'container',
              width: 300,
              height: 300
              });

              var layer = new Konva.Layer({draggable: true});
              stage.add(layer);


              layer.on("mouseenter", function() {
              stage.container().style.cursor = 'pointer';
              });

              layer.on("mouseleave", function() {
              stage.container().style.cursor = 'default';
              });

              var rect = new Konva.Rect({
              width: 50,
              height: 50,
              fill: 'red',
              stroke: 'black',
              strokeWidth: 5
              });

              var back = new Konva.Rect({
              fill: 'red',
              width: stage.width(),
              height: stage.height()
              })
              layer.add(back);

              // move rectangle so it always stay on the screen, no matter where layer it
              layer.on('dragend', function() {
              back.setAbsolutePosition({x: 0, y: 0});
              layer.draw();
              });



              layer.add(rect);

              stage.draw();
              }

              demo();
              </script>

              </body>
              </html>








              share|improve this answer
























                1












                1








                1






                Konva does not detect events on empty space of layers. And it is unclear how can it do it if you have several layers on the stage.



                If you still need to listen on empty layers and make a layer draggable at any space you can:




                1. Make stage draggable instead and listen events on it (click will work on empty space too).


                2. Add a transparent rectangle as a background, so it will be used to listen to events:






                <!DOCTYPE html>
                <html>
                <head lang="en">
                <meta charset="UTF-8">
                </head>
                <body>
                <script src="js/modulePattern.js"></script>
                <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



                <div id="container"></div>
                <script>

                function demo() {

                var stage = new Konva.Stage({
                container: 'container',
                width: 300,
                height: 300
                });

                var layer = new Konva.Layer({draggable: true});
                stage.add(layer);


                layer.on("mouseenter", function() {
                stage.container().style.cursor = 'pointer';
                });

                layer.on("mouseleave", function() {
                stage.container().style.cursor = 'default';
                });

                var rect = new Konva.Rect({
                width: 50,
                height: 50,
                fill: 'red',
                stroke: 'black',
                strokeWidth: 5
                });

                var back = new Konva.Rect({
                fill: 'red',
                width: stage.width(),
                height: stage.height()
                })
                layer.add(back);

                // move rectangle so it always stay on the screen, no matter where layer it
                layer.on('dragend', function() {
                back.setAbsolutePosition({x: 0, y: 0});
                layer.draw();
                });



                layer.add(rect);

                stage.draw();
                }

                demo();
                </script>

                </body>
                </html>








                share|improve this answer












                Konva does not detect events on empty space of layers. And it is unclear how can it do it if you have several layers on the stage.



                If you still need to listen on empty layers and make a layer draggable at any space you can:




                1. Make stage draggable instead and listen events on it (click will work on empty space too).


                2. Add a transparent rectangle as a background, so it will be used to listen to events:






                <!DOCTYPE html>
                <html>
                <head lang="en">
                <meta charset="UTF-8">
                </head>
                <body>
                <script src="js/modulePattern.js"></script>
                <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



                <div id="container"></div>
                <script>

                function demo() {

                var stage = new Konva.Stage({
                container: 'container',
                width: 300,
                height: 300
                });

                var layer = new Konva.Layer({draggable: true});
                stage.add(layer);


                layer.on("mouseenter", function() {
                stage.container().style.cursor = 'pointer';
                });

                layer.on("mouseleave", function() {
                stage.container().style.cursor = 'default';
                });

                var rect = new Konva.Rect({
                width: 50,
                height: 50,
                fill: 'red',
                stroke: 'black',
                strokeWidth: 5
                });

                var back = new Konva.Rect({
                fill: 'red',
                width: stage.width(),
                height: stage.height()
                })
                layer.add(back);

                // move rectangle so it always stay on the screen, no matter where layer it
                layer.on('dragend', function() {
                back.setAbsolutePosition({x: 0, y: 0});
                layer.draw();
                });



                layer.add(rect);

                stage.draw();
                }

                demo();
                </script>

                </body>
                </html>








                <!DOCTYPE html>
                <html>
                <head lang="en">
                <meta charset="UTF-8">
                </head>
                <body>
                <script src="js/modulePattern.js"></script>
                <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



                <div id="container"></div>
                <script>

                function demo() {

                var stage = new Konva.Stage({
                container: 'container',
                width: 300,
                height: 300
                });

                var layer = new Konva.Layer({draggable: true});
                stage.add(layer);


                layer.on("mouseenter", function() {
                stage.container().style.cursor = 'pointer';
                });

                layer.on("mouseleave", function() {
                stage.container().style.cursor = 'default';
                });

                var rect = new Konva.Rect({
                width: 50,
                height: 50,
                fill: 'red',
                stroke: 'black',
                strokeWidth: 5
                });

                var back = new Konva.Rect({
                fill: 'red',
                width: stage.width(),
                height: stage.height()
                })
                layer.add(back);

                // move rectangle so it always stay on the screen, no matter where layer it
                layer.on('dragend', function() {
                back.setAbsolutePosition({x: 0, y: 0});
                layer.draw();
                });



                layer.add(rect);

                stage.draw();
                }

                demo();
                </script>

                </body>
                </html>





                <!DOCTYPE html>
                <html>
                <head lang="en">
                <meta charset="UTF-8">
                </head>
                <body>
                <script src="js/modulePattern.js"></script>
                <script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>



                <div id="container"></div>
                <script>

                function demo() {

                var stage = new Konva.Stage({
                container: 'container',
                width: 300,
                height: 300
                });

                var layer = new Konva.Layer({draggable: true});
                stage.add(layer);


                layer.on("mouseenter", function() {
                stage.container().style.cursor = 'pointer';
                });

                layer.on("mouseleave", function() {
                stage.container().style.cursor = 'default';
                });

                var rect = new Konva.Rect({
                width: 50,
                height: 50,
                fill: 'red',
                stroke: 'black',
                strokeWidth: 5
                });

                var back = new Konva.Rect({
                fill: 'red',
                width: stage.width(),
                height: stage.height()
                })
                layer.add(back);

                // move rectangle so it always stay on the screen, no matter where layer it
                layer.on('dragend', function() {
                back.setAbsolutePosition({x: 0, y: 0});
                layer.draw();
                });



                layer.add(rect);

                stage.draw();
                }

                demo();
                </script>

                </body>
                </html>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 13:17









                lavrtonlavrton

                7,24221231




                7,24221231






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270863%2fdragging-a-konva-layer-from-empty-areas%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma