Phaser js: is it possible to add css to a sprite?











up vote
-1
down vote

favorite












I want to take a sprite from my Phaser game and add to it some CSS. Is it possible? I made researches but found nothing. I would like to add to my sprite this CSS:



img {
webkit-filter: drop-shadow(1px 1px 0 black)
drop-shadow(-1px -1px 0 black);
filter: drop-shadow(1px 1px 0 black)
drop-shadow(-1px -1px 0 black);
background-color: lightcoral;
}









share|improve this question




























    up vote
    -1
    down vote

    favorite












    I want to take a sprite from my Phaser game and add to it some CSS. Is it possible? I made researches but found nothing. I would like to add to my sprite this CSS:



    img {
    webkit-filter: drop-shadow(1px 1px 0 black)
    drop-shadow(-1px -1px 0 black);
    filter: drop-shadow(1px 1px 0 black)
    drop-shadow(-1px -1px 0 black);
    background-color: lightcoral;
    }









    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I want to take a sprite from my Phaser game and add to it some CSS. Is it possible? I made researches but found nothing. I would like to add to my sprite this CSS:



      img {
      webkit-filter: drop-shadow(1px 1px 0 black)
      drop-shadow(-1px -1px 0 black);
      filter: drop-shadow(1px 1px 0 black)
      drop-shadow(-1px -1px 0 black);
      background-color: lightcoral;
      }









      share|improve this question















      I want to take a sprite from my Phaser game and add to it some CSS. Is it possible? I made researches but found nothing. I would like to add to my sprite this CSS:



      img {
      webkit-filter: drop-shadow(1px 1px 0 black)
      drop-shadow(-1px -1px 0 black);
      filter: drop-shadow(1px 1px 0 black)
      drop-shadow(-1px -1px 0 black);
      background-color: lightcoral;
      }






      javascript css canvas phaser-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 4:42









      Eray Balkanli

      3,85741943




      3,85741943










      asked Nov 11 at 4:39









      JuZDePeche

      186




      186
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          No, you cannot style a Phaser sprite with CSS.



          However, if you're using an image for the sprite you could add the element to the page as an img element and then apply CSS styling to it. But, it could not then be used within the game with that styling.



          If you wanted to do this in Phaser one option would be to make a duplicate of the sprite and tint it and offset it from the original sprite so that it looks like a shadow.



          There's a Sprite Shadow example for Phaser 2 that shows this in action.



          var sprite;
          var shadow;
          var offset = new Phaser.Point(10, 8);

          function create() {
          shadow = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
          shadow.anchor.set(0.5);
          shadow.tint = 0x000000;
          shadow.alpha = 0.6;

          sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
          sprite.anchor.set(0.5);

          game.input.addMoveCallback(move, this);
          }

          function move(pointer, x, y) {
          sprite.x = x;
          sprite.y = y;

          shadow.x = sprite.x + offset.x;
          shadow.y = sprite.y + offset.y;
          }


          Alternatively, you could update your asset to include the drop shadow, if it should always be present. That would generally be more performant than adding it programmatically.






          share|improve this answer





















          • Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
            – JuZDePeche
            Nov 11 at 6:45


















          up vote
          0
          down vote













          No, style can't be applied to a phaser sprite as Phaser adds the sprite to a Canvas.
          However, you can add the drop-shadow effect to a sprite using Phaser.



          Please note that CSS doesn't works on the objects which are drawn over a Canvas (but you can still add CSS to a canvas or parent div)






          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',
            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%2f53245898%2fphaser-js-is-it-possible-to-add-css-to-a-sprite%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            No, you cannot style a Phaser sprite with CSS.



            However, if you're using an image for the sprite you could add the element to the page as an img element and then apply CSS styling to it. But, it could not then be used within the game with that styling.



            If you wanted to do this in Phaser one option would be to make a duplicate of the sprite and tint it and offset it from the original sprite so that it looks like a shadow.



            There's a Sprite Shadow example for Phaser 2 that shows this in action.



            var sprite;
            var shadow;
            var offset = new Phaser.Point(10, 8);

            function create() {
            shadow = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            shadow.anchor.set(0.5);
            shadow.tint = 0x000000;
            shadow.alpha = 0.6;

            sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            sprite.anchor.set(0.5);

            game.input.addMoveCallback(move, this);
            }

            function move(pointer, x, y) {
            sprite.x = x;
            sprite.y = y;

            shadow.x = sprite.x + offset.x;
            shadow.y = sprite.y + offset.y;
            }


            Alternatively, you could update your asset to include the drop shadow, if it should always be present. That would generally be more performant than adding it programmatically.






            share|improve this answer





















            • Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
              – JuZDePeche
              Nov 11 at 6:45















            up vote
            0
            down vote



            accepted










            No, you cannot style a Phaser sprite with CSS.



            However, if you're using an image for the sprite you could add the element to the page as an img element and then apply CSS styling to it. But, it could not then be used within the game with that styling.



            If you wanted to do this in Phaser one option would be to make a duplicate of the sprite and tint it and offset it from the original sprite so that it looks like a shadow.



            There's a Sprite Shadow example for Phaser 2 that shows this in action.



            var sprite;
            var shadow;
            var offset = new Phaser.Point(10, 8);

            function create() {
            shadow = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            shadow.anchor.set(0.5);
            shadow.tint = 0x000000;
            shadow.alpha = 0.6;

            sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            sprite.anchor.set(0.5);

            game.input.addMoveCallback(move, this);
            }

            function move(pointer, x, y) {
            sprite.x = x;
            sprite.y = y;

            shadow.x = sprite.x + offset.x;
            shadow.y = sprite.y + offset.y;
            }


            Alternatively, you could update your asset to include the drop shadow, if it should always be present. That would generally be more performant than adding it programmatically.






            share|improve this answer





















            • Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
              – JuZDePeche
              Nov 11 at 6:45













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            No, you cannot style a Phaser sprite with CSS.



            However, if you're using an image for the sprite you could add the element to the page as an img element and then apply CSS styling to it. But, it could not then be used within the game with that styling.



            If you wanted to do this in Phaser one option would be to make a duplicate of the sprite and tint it and offset it from the original sprite so that it looks like a shadow.



            There's a Sprite Shadow example for Phaser 2 that shows this in action.



            var sprite;
            var shadow;
            var offset = new Phaser.Point(10, 8);

            function create() {
            shadow = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            shadow.anchor.set(0.5);
            shadow.tint = 0x000000;
            shadow.alpha = 0.6;

            sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            sprite.anchor.set(0.5);

            game.input.addMoveCallback(move, this);
            }

            function move(pointer, x, y) {
            sprite.x = x;
            sprite.y = y;

            shadow.x = sprite.x + offset.x;
            shadow.y = sprite.y + offset.y;
            }


            Alternatively, you could update your asset to include the drop shadow, if it should always be present. That would generally be more performant than adding it programmatically.






            share|improve this answer












            No, you cannot style a Phaser sprite with CSS.



            However, if you're using an image for the sprite you could add the element to the page as an img element and then apply CSS styling to it. But, it could not then be used within the game with that styling.



            If you wanted to do this in Phaser one option would be to make a duplicate of the sprite and tint it and offset it from the original sprite so that it looks like a shadow.



            There's a Sprite Shadow example for Phaser 2 that shows this in action.



            var sprite;
            var shadow;
            var offset = new Phaser.Point(10, 8);

            function create() {
            shadow = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            shadow.anchor.set(0.5);
            shadow.tint = 0x000000;
            shadow.alpha = 0.6;

            sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'hotdog');
            sprite.anchor.set(0.5);

            game.input.addMoveCallback(move, this);
            }

            function move(pointer, x, y) {
            sprite.x = x;
            sprite.y = y;

            shadow.x = sprite.x + offset.x;
            shadow.y = sprite.y + offset.y;
            }


            Alternatively, you could update your asset to include the drop shadow, if it should always be present. That would generally be more performant than adding it programmatically.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 5:53









            James Skemp

            4,81284775




            4,81284775












            • Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
              – JuZDePeche
              Nov 11 at 6:45


















            • Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
              – JuZDePeche
              Nov 11 at 6:45
















            Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
            – JuZDePeche
            Nov 11 at 6:45




            Didn't use this way. I decided to use sprite.tint instead of creating a new sprite for each of mine. Thanks!
            – JuZDePeche
            Nov 11 at 6:45












            up vote
            0
            down vote













            No, style can't be applied to a phaser sprite as Phaser adds the sprite to a Canvas.
            However, you can add the drop-shadow effect to a sprite using Phaser.



            Please note that CSS doesn't works on the objects which are drawn over a Canvas (but you can still add CSS to a canvas or parent div)






            share|improve this answer

























              up vote
              0
              down vote













              No, style can't be applied to a phaser sprite as Phaser adds the sprite to a Canvas.
              However, you can add the drop-shadow effect to a sprite using Phaser.



              Please note that CSS doesn't works on the objects which are drawn over a Canvas (but you can still add CSS to a canvas or parent div)






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                No, style can't be applied to a phaser sprite as Phaser adds the sprite to a Canvas.
                However, you can add the drop-shadow effect to a sprite using Phaser.



                Please note that CSS doesn't works on the objects which are drawn over a Canvas (but you can still add CSS to a canvas or parent div)






                share|improve this answer












                No, style can't be applied to a phaser sprite as Phaser adds the sprite to a Canvas.
                However, you can add the drop-shadow effect to a sprite using Phaser.



                Please note that CSS doesn't works on the objects which are drawn over a Canvas (but you can still add CSS to a canvas or parent div)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 at 10:14









                Rathore

                1




                1






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53245898%2fphaser-js-is-it-possible-to-add-css-to-a-sprite%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)