Skip to main content
Known Participant
February 11, 2021
Answered

Animate CC HTML5 Canvas subscript

  • February 11, 2021
  • 1 reply
  • 461 views

Hi, 

 

I put together an interactive HTML5 canvas document. 

 

After a week of fighting multiple snags and learning curves, I finally have a working product! Just when I thought I was done, I am stumped once more after I received the content to update the text with. 

 

I am trying to put the word CO2 in there, but I need the 2 to be subscript.

 

var string1 = "how much CO";
var string2 = "2";
var substring = string2.sup();
var string3 = " is used?";


_this.inst_text.text = string1 + substring + string3;

 

It shows up as,

 

"How many <sub>2</sub> is used."

 

I need to stick with Dynamic text because I change it out later in code. 

 

Any suggestions?

 

Thanks!

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    You can use unicodes and the static method fromCharCode from the String class. Like this:

    var root = this;
    
    root.charfromUnicode = function(code)
    {
    	return String.fromCharCode(parseInt(code, 16));
    };
    
    console.log("CO" + root.charfromUnicode(2082));

     

    I hope this helps.

     

    Regards,

    JC 

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    February 11, 2021

    Hi.

     

    You can use unicodes and the static method fromCharCode from the String class. Like this:

    var root = this;
    
    root.charfromUnicode = function(code)
    {
    	return String.fromCharCode(parseInt(code, 16));
    };
    
    console.log("CO" + root.charfromUnicode(2082));

     

    I hope this helps.

     

    Regards,

    JC 

    Known Participant
    February 11, 2021

    You Rock!

     

    That worked no problem. 

     

    Thanks very much for the prompt reply.

     

    Take care!

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 11, 2021

    Excellent! You're welcome!