1

Surely there must be a way to add some color to the line in the code below where it shows m.codeletters = "&#+%?£@§$"; etc? I want to change the color to : **"&#+%?£@§$"**

var Messenger = function(el){
  'use strict';
  var m = this;
  
  m.init = function(){
    m.codeletters = "&#*+%?£@§$";
    m.message = 0;
    m.current_length = 0;
    m.fadeBuffer = false;
    m.messages = [
       'readers',
      'via feed',
      ''
    ];
    
    setTimeout(m.animateIn, 100);
  };
  
  m.generateRandomString = function(length){
    var random_text = '';
    while(random_text.length < length){
      random_text += m.codeletters.charAt(Math.floor(Math.random()*m.codeletters.length));
    } 
    
    return random_text;
  };
  
  m.animateIn = function(){
    if(m.current_length < m.messages[m.message].length){
      m.current_length = m.current_length + 2;
      if(m.current_length > m.messages[m.message].length) {
        m.current_length = m.messages[m.message].length;
      }
      
      var message = m.generateRandomString(m.current_length);
      $(el).html(message);
      
      setTimeout(m.animateIn, 20);
    } else { 
      setTimeout(m.animateFadeBuffer, 20);
    }
  };
  
  m.animateFadeBuffer = function(){
    if(m.fadeBuffer === false){
      m.fadeBuffer = [];
      for(var i = 0; i < m.messages[m.message].length; i++){
        m.fadeBuffer.push({c: (Math.floor(Math.random()*12))+1, l: m.messages[m.message].charAt(i)});
      }
    }
    
    var do_cycles = false;
    var message = ''; 
    
    for(var i = 0; i < m.fadeBuffer.length; i++){
      var fader = m.fadeBuffer[i];
      if(fader.c > 0){
        do_cycles = true;
        fader.c--;
        message += m.codeletters.charAt(Math.floor(Math.random()*m.codeletters.length));
      } else {
        message += fader.l;
      }
    }
    
    $(el).html(message);
    
    if(do_cycles === true){
      setTimeout(m.animateFadeBuffer, 20);
    } else {
      setTimeout(m.cycleText, 2000);
    }
  };
  
  m.cycleText = function(){
    m.message = m.message + 1;
    if(m.message >= m.messages.length){
      m.message = 0;
    }
    
    m.current_length = 0;
    m.fadeBuffer = false;
    $(el).html('');
    
    setTimeout(m.animateIn, 200);
  };
  
  m.init();
}

console.clear();
var messenger = new Messenger($('#messenger'));

I have looked up and down, to try and see where in the code I can do this. I also want to know what I need to add to be able to do this. I simply have no clue. I've seen other codes of similar where the codeletter area is colored, then the rest of the text another color.

wazz
  • 4,953
  • 5
  • 20
  • 34
Ladan
  • 33
  • 6

1 Answers1

1

If you're trying to color console output, you would use ANSI escape sequences that look something like this \u001b[31;1m, and which you can learn more about in this SO post. There is also an npm module available called colors that greatly simplifies the process of implementing them.

If you're trying to color HTML output, you would just wrap each character in an individual <span> element and add a class or style attribute with CSS-styling for the desired color.

selfagency
  • 1,481
  • 1
  • 9
  • 12
  • Thank you. Would either color console output or HTML output work with what I want to give color to? – Ladan Nov 20 '20 at 03:14
  • Ok my mess up. Question should've been...where in my JS do I need to put the escape character codes? – Ladan Nov 21 '20 at 18:59
  • 1
    Just so we're clear, where are you trying to display the colored text? In the console/terminal or in the browser? – selfagency Nov 22 '20 at 03:50
  • on my browser, and on my blog. I have the CodePen if you'd like to see it? That's where I originally found it. – Ladan Nov 22 '20 at 14:54
  • 1
    If that's the case, then you would not use color codes, but instead HTML, wrapping each character in a different ``, as I had mentioned. You can do that on this line: `message += m.codeletters.charAt(Math.floor(Math.random()*m.codeletters.length));` by changing it to `message += "" + m.codeletters.charAt(Math.floor(Math.random()*m.codeletters.length)) + "";` – selfagency Nov 22 '20 at 16:53
  • Thank you, this worked!. How bout for when it starts? It still shows white. – Ladan Nov 22 '20 at 16:58
  • Found out where the scramble starts, the color to that is in the CSS – Ladan Nov 22 '20 at 17:24