askmeaquestion1234
askmeaquestion1234

Reputation: 179

Dynamically Change text based on String in Canvas

I'm not sure if this is possible, and I'm fairly new to canvas in general, but my goal is to read a string and if the string contains a specific word, then change that specific words specific color while leaving the rest of the string normal. So for example is text == degraded-performance change that text to Purple, but leave the rest of the text normal. But if text == degraded-performance and operational, change degraded-performance to Purple, and operational to Green

    // == Color Rules ==
    // Partial Outage = Orange
    // Major Outage = Red
    // Degraded Performance = Purple
    // Under Maintenance = Grey
    // Operational = Green

    function degraded() {
        ctx.fillStyle = "Purple";
        ctx.fill();
        ctx.fillText("Partial Outage", 75, 50);
    }
    function operational() {
        ctx.fillStyle = "Green";
        ctx.fill();
        ctx.fillText("Operational", 75, 50);
    }


    // JSON
    result = {"PF":"operational","PU":"degraded-performance","EA":"operational"}                

    const json = JSON.stringify(result, null, 1);
    const removeBracket = json.replace(/{/g, '').replace(/}/g, '');
    const unquoted = removeBracket.replace(/\"/g, "");  

            // load bg
            ctx = canvas.getContext("2d");
            img = document.getElementById("bg");
            ctx.drawImage(img, 0, 0);

            // Add to String and Split Lines
            var x = 10;
            var y = 50;
            var lineheight = 30;
            splitlines = ("PF" + ' ' + result.PF.replace(new RegExp(' ', 'g'), '\n') +
            "\n" + "PU" + ' ' + result.PU + "\n" + "EA" + ' ' + result.EA)

            // Split Lines
            var lines = splitlines.split('\n');
            for (var i = 0; i<lines.length; i++){
            ctx.fillText(lines[i], x, y + (i*lineheight) );
            }

            // If string contains a text swap out THAT specific text. How can i do that?
            if (lines.includes('Operational') == true) {
                operational();
            } else {

            }

Upvotes: 1

Views: 535

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17644

Here is what I would do:

You need the if statement inside the loop setting the right color

json = {
  "PF": "operational",
  "PU": "degraded-performance",
  "EA": "partial-outage"
}

canvas = document.getElementById("c")
ctx = canvas.getContext("2d");
var x = 10;
var y = 10;

for (var prop in json) {
  if (json[prop].includes("degraded-performance")) {
    ctx.fillStyle = "Purple";
  } else if (json[prop].includes("partial-outage")) {
    ctx.fillStyle = "Orange";
  } else {
    ctx.fillStyle = "Green";
  }
  ctx.fillText(prop + ' ' + json[prop], x, y);
  y += 20
}
<canvas id="c"></canvas>

If your text will be in one line look into measureText https://www.w3schools.com/tags/canvas_measuretext.asp
You can use that to "concatenate" text of different color in one line.

Upvotes: 1

Related Questions