How to add an event for shapes in Two.js?












0















I have a group of two.js shapes. When a user hovers over a shape, I want that one shape to spin.



http://merhoo.github.io/secrets.html



Issue with css animations: if I apply a :hover animation to the svg shapes, it affects the position of the shape somehow, moving it to the top left corner.



So I'm trying to bind a mouseover event to each child for them to spin.



var shapes = makeFlowers();

for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
...
var shape = pickFlower();
shape.translation.set(hi * two.width, vi * two.height);
two.add(shape);
}
}
two.update();

for (var f in flowers) {
var flower = flowers[f];
$(flower._renderer.elem)
.click(function(e) {
flower.fill = "blue";
})
}


So how do you apply hover/mouseover animations to shapes in two.js?










share|improve this question





























    0















    I have a group of two.js shapes. When a user hovers over a shape, I want that one shape to spin.



    http://merhoo.github.io/secrets.html



    Issue with css animations: if I apply a :hover animation to the svg shapes, it affects the position of the shape somehow, moving it to the top left corner.



    So I'm trying to bind a mouseover event to each child for them to spin.



    var shapes = makeFlowers();

    for (var r = 0; r < rows; r++) {
    for (var c = 0; c < cols; c++) {
    ...
    var shape = pickFlower();
    shape.translation.set(hi * two.width, vi * two.height);
    two.add(shape);
    }
    }
    two.update();

    for (var f in flowers) {
    var flower = flowers[f];
    $(flower._renderer.elem)
    .click(function(e) {
    flower.fill = "blue";
    })
    }


    So how do you apply hover/mouseover animations to shapes in two.js?










    share|improve this question



























      0












      0








      0


      0






      I have a group of two.js shapes. When a user hovers over a shape, I want that one shape to spin.



      http://merhoo.github.io/secrets.html



      Issue with css animations: if I apply a :hover animation to the svg shapes, it affects the position of the shape somehow, moving it to the top left corner.



      So I'm trying to bind a mouseover event to each child for them to spin.



      var shapes = makeFlowers();

      for (var r = 0; r < rows; r++) {
      for (var c = 0; c < cols; c++) {
      ...
      var shape = pickFlower();
      shape.translation.set(hi * two.width, vi * two.height);
      two.add(shape);
      }
      }
      two.update();

      for (var f in flowers) {
      var flower = flowers[f];
      $(flower._renderer.elem)
      .click(function(e) {
      flower.fill = "blue";
      })
      }


      So how do you apply hover/mouseover animations to shapes in two.js?










      share|improve this question
















      I have a group of two.js shapes. When a user hovers over a shape, I want that one shape to spin.



      http://merhoo.github.io/secrets.html



      Issue with css animations: if I apply a :hover animation to the svg shapes, it affects the position of the shape somehow, moving it to the top left corner.



      So I'm trying to bind a mouseover event to each child for them to spin.



      var shapes = makeFlowers();

      for (var r = 0; r < rows; r++) {
      for (var c = 0; c < cols; c++) {
      ...
      var shape = pickFlower();
      shape.translation.set(hi * two.width, vi * two.height);
      two.add(shape);
      }
      }
      two.update();

      for (var f in flowers) {
      var flower = flowers[f];
      $(flower._renderer.elem)
      .click(function(e) {
      flower.fill = "blue";
      })
      }


      So how do you apply hover/mouseover animations to shapes in two.js?







      javascript animation onclicklistener two.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 4:28







      merhoo

















      asked Jan 2 at 18:28









      merhoomerhoo

      7211




      7211
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Two issues (haha) with adding individual event handlers/animations to two.js objects.




          1. The first I don't understand still: it will only add the event handler to the last shape in the loop. Maybe because it is rendered in the html as one svg, the onclick is applied to either one or all the paths. Seems like a bug on Two.js' part.

          2. As mentioned in this post, transforming the path of an SVG via an animation is very heavy for the browser. If you apply the animation to the container of the SVG, it won't be applying a transformation to each point in the path.


          Solution:





          1. Make a div for each row, and fill it with a div for each cell



            for (var r = 0; r < rows; r++) {
            var rowId = "row" + r;
            var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
            for (var c = 0; c < cols; c++) {
            var cellId = "cell" + ((r * rows) + c);
            $(row).append('<div class="cell" id="' + cellId + '"></div>');
            }
            }



          2. For each cell, make an instance of Two, add the shape to that



            $(".cell").each(function (index, object) {
            var two = new Two({
            width: size + padding,
            height:size + padding
            }).appendTo(object);
            var shape = pickFlower(shapes);
            shape.translation.set(two.width / 2, two.height / 2);
            two.add(shape);
            two.update();
            });



          3. Add the hover animation to the divs with CSS



            .cell:hover {
            animation: loaderAnim 5s ease-in-out infinite normal forwards;
            padding: 0;
            margin: 0;
            }


            @keyframes loaderAnim {
            0% {
            transform:rotate(0deg);
            }
            50% {
            transform:rotate(90deg);
            }
            100% {
            transform:rotate(0deg);
            }
            }



          Notes:




          • Don't try to combine making the div and adding Two to the div. The div may not be added to the html, and it will then add multiple two instances to a div, and other funky things






          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%2f54011369%2fhow-to-add-an-event-for-shapes-in-two-js%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









            0














            Two issues (haha) with adding individual event handlers/animations to two.js objects.




            1. The first I don't understand still: it will only add the event handler to the last shape in the loop. Maybe because it is rendered in the html as one svg, the onclick is applied to either one or all the paths. Seems like a bug on Two.js' part.

            2. As mentioned in this post, transforming the path of an SVG via an animation is very heavy for the browser. If you apply the animation to the container of the SVG, it won't be applying a transformation to each point in the path.


            Solution:





            1. Make a div for each row, and fill it with a div for each cell



              for (var r = 0; r < rows; r++) {
              var rowId = "row" + r;
              var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
              for (var c = 0; c < cols; c++) {
              var cellId = "cell" + ((r * rows) + c);
              $(row).append('<div class="cell" id="' + cellId + '"></div>');
              }
              }



            2. For each cell, make an instance of Two, add the shape to that



              $(".cell").each(function (index, object) {
              var two = new Two({
              width: size + padding,
              height:size + padding
              }).appendTo(object);
              var shape = pickFlower(shapes);
              shape.translation.set(two.width / 2, two.height / 2);
              two.add(shape);
              two.update();
              });



            3. Add the hover animation to the divs with CSS



              .cell:hover {
              animation: loaderAnim 5s ease-in-out infinite normal forwards;
              padding: 0;
              margin: 0;
              }


              @keyframes loaderAnim {
              0% {
              transform:rotate(0deg);
              }
              50% {
              transform:rotate(90deg);
              }
              100% {
              transform:rotate(0deg);
              }
              }



            Notes:




            • Don't try to combine making the div and adding Two to the div. The div may not be added to the html, and it will then add multiple two instances to a div, and other funky things






            share|improve this answer




























              0














              Two issues (haha) with adding individual event handlers/animations to two.js objects.




              1. The first I don't understand still: it will only add the event handler to the last shape in the loop. Maybe because it is rendered in the html as one svg, the onclick is applied to either one or all the paths. Seems like a bug on Two.js' part.

              2. As mentioned in this post, transforming the path of an SVG via an animation is very heavy for the browser. If you apply the animation to the container of the SVG, it won't be applying a transformation to each point in the path.


              Solution:





              1. Make a div for each row, and fill it with a div for each cell



                for (var r = 0; r < rows; r++) {
                var rowId = "row" + r;
                var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
                for (var c = 0; c < cols; c++) {
                var cellId = "cell" + ((r * rows) + c);
                $(row).append('<div class="cell" id="' + cellId + '"></div>');
                }
                }



              2. For each cell, make an instance of Two, add the shape to that



                $(".cell").each(function (index, object) {
                var two = new Two({
                width: size + padding,
                height:size + padding
                }).appendTo(object);
                var shape = pickFlower(shapes);
                shape.translation.set(two.width / 2, two.height / 2);
                two.add(shape);
                two.update();
                });



              3. Add the hover animation to the divs with CSS



                .cell:hover {
                animation: loaderAnim 5s ease-in-out infinite normal forwards;
                padding: 0;
                margin: 0;
                }


                @keyframes loaderAnim {
                0% {
                transform:rotate(0deg);
                }
                50% {
                transform:rotate(90deg);
                }
                100% {
                transform:rotate(0deg);
                }
                }



              Notes:




              • Don't try to combine making the div and adding Two to the div. The div may not be added to the html, and it will then add multiple two instances to a div, and other funky things






              share|improve this answer


























                0












                0








                0







                Two issues (haha) with adding individual event handlers/animations to two.js objects.




                1. The first I don't understand still: it will only add the event handler to the last shape in the loop. Maybe because it is rendered in the html as one svg, the onclick is applied to either one or all the paths. Seems like a bug on Two.js' part.

                2. As mentioned in this post, transforming the path of an SVG via an animation is very heavy for the browser. If you apply the animation to the container of the SVG, it won't be applying a transformation to each point in the path.


                Solution:





                1. Make a div for each row, and fill it with a div for each cell



                  for (var r = 0; r < rows; r++) {
                  var rowId = "row" + r;
                  var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
                  for (var c = 0; c < cols; c++) {
                  var cellId = "cell" + ((r * rows) + c);
                  $(row).append('<div class="cell" id="' + cellId + '"></div>');
                  }
                  }



                2. For each cell, make an instance of Two, add the shape to that



                  $(".cell").each(function (index, object) {
                  var two = new Two({
                  width: size + padding,
                  height:size + padding
                  }).appendTo(object);
                  var shape = pickFlower(shapes);
                  shape.translation.set(two.width / 2, two.height / 2);
                  two.add(shape);
                  two.update();
                  });



                3. Add the hover animation to the divs with CSS



                  .cell:hover {
                  animation: loaderAnim 5s ease-in-out infinite normal forwards;
                  padding: 0;
                  margin: 0;
                  }


                  @keyframes loaderAnim {
                  0% {
                  transform:rotate(0deg);
                  }
                  50% {
                  transform:rotate(90deg);
                  }
                  100% {
                  transform:rotate(0deg);
                  }
                  }



                Notes:




                • Don't try to combine making the div and adding Two to the div. The div may not be added to the html, and it will then add multiple two instances to a div, and other funky things






                share|improve this answer













                Two issues (haha) with adding individual event handlers/animations to two.js objects.




                1. The first I don't understand still: it will only add the event handler to the last shape in the loop. Maybe because it is rendered in the html as one svg, the onclick is applied to either one or all the paths. Seems like a bug on Two.js' part.

                2. As mentioned in this post, transforming the path of an SVG via an animation is very heavy for the browser. If you apply the animation to the container of the SVG, it won't be applying a transformation to each point in the path.


                Solution:





                1. Make a div for each row, and fill it with a div for each cell



                  for (var r = 0; r < rows; r++) {
                  var rowId = "row" + r;
                  var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
                  for (var c = 0; c < cols; c++) {
                  var cellId = "cell" + ((r * rows) + c);
                  $(row).append('<div class="cell" id="' + cellId + '"></div>');
                  }
                  }



                2. For each cell, make an instance of Two, add the shape to that



                  $(".cell").each(function (index, object) {
                  var two = new Two({
                  width: size + padding,
                  height:size + padding
                  }).appendTo(object);
                  var shape = pickFlower(shapes);
                  shape.translation.set(two.width / 2, two.height / 2);
                  two.add(shape);
                  two.update();
                  });



                3. Add the hover animation to the divs with CSS



                  .cell:hover {
                  animation: loaderAnim 5s ease-in-out infinite normal forwards;
                  padding: 0;
                  margin: 0;
                  }


                  @keyframes loaderAnim {
                  0% {
                  transform:rotate(0deg);
                  }
                  50% {
                  transform:rotate(90deg);
                  }
                  100% {
                  transform:rotate(0deg);
                  }
                  }



                Notes:




                • Don't try to combine making the div and adding Two to the div. The div may not be added to the html, and it will then add multiple two instances to a div, and other funky things







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 12 at 20:54









                merhoomerhoo

                7211




                7211
































                    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%2f54011369%2fhow-to-add-an-event-for-shapes-in-two-js%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith