2

Making a simple JS game (all I can use is Jquery and vanilla JS) and would like to use art like this:

        .-.
       |_:_|
      /(_Y_)\
     (_\/M\/_)
      /.'-'.\
     //|[_]|\\
     \\|   |//
      #|===|\#
     / |\ /I \
    /_ | | I _\
       |_|_| 
 

and use Jquery to toss it around to different divs on my page. How and where can I put this as to not lose that spacing but still be able to call it? When I have it in as a value i cant have multiple lines per value it would seem because sublime turns vader into a rainbow.

ggruessing
  • 23
  • 2
  • Last time I just had all my art in my HTML file and set to display:none until I needed them and changed to :block accordingly. I can do it this way again but it not be very DRY of me. – ggruessing Jun 19 '17 at 07:02
  • Isn't ASCII art just text? So, why not a string? – Álvaro González Jun 19 '17 at 07:21
  • but then how would i go about capturing all the blank spaces in between things and in between lines for when it gets tossed back into a div? Throw
    s in there with empty spaces?
    – ggruessing Jun 19 '17 at 07:26
  • 1
    Please check [ASCII art in HTML](https://stackoverflow.com/questions/1702559/ascii-art-in-html), though it's a pretty old question (in 2017 you can use safely CSS to emulate all `
    ` properties in any tag).
    – Álvaro González Jun 19 '17 at 07:30
  • @ggruessing You can use `/s` – Weedoze Jun 19 '17 at 08:01

1 Answers1

0

Since SO seems to display your stuff correctly i just inspected what is the element and seems to be <pre>. So i created a var with the styling for recreating a darthvader like this:

var darthvador = "        .-.<br>       |_:_|<br>      /(_Y_)\\<br>     (_\\/M\\/_)<br>      /.'-'.\\<br>     //|[_]|\\\\<br>     \\\\|   |//<br>      #|===|\\#<br>     / |\\ /I \\<br>    /_ | | I _\\<br>       |_|_| ";

<br> to make a linebreak. and doubles all the \ so it doesn't consider it as an escape character.

then i've made a div to hold the player:

 <div id="player">

</div>

simple div didn't apply any style you can do what ever you like with it.

<input type="button" value="addPlayer" onclick="addPlayer();"/> 

I made this button so you can add it when you want. You can make some logic to add it only once or just store it in player div and hide the button or what ever.

And then add player simply creates a <pre> element and adds the darthvador player in it.

function addPlayer(){
    document.getElementById("player").innerHTML = "<pre>" + darthvador +"</pre>";
}

css style to make pre look like a normal div. You can change the width or what ever if you need.

 pre{
    background-color: transparent;
    border: 0px;
}

hope this helps.

Then if you need to manipulate the player and toss it around other divs jQuery has a detach function to detach a div. And then you can attach if with after or appendTo or before.

PS: Sry i can't provide jsFiddle link i don't have access to all the website. But it's just css html and js so you can easily rebuild it :p

user5014677
  • 694
  • 6
  • 22
  • oh perfect thank you! I was going towards the same way but had no idea why my \s were disappearing and assumed it didnt like the syntax – ggruessing Jun 19 '17 at 19:21