Sanjay Nakate
Sanjay Nakate

Reputation: 2098

How to set text background color using fabricjs

this is my code

$("#textbackground").click(function() {

        var obj = canvas.getActiveObject();
    ![enter image description here][1]
    //alert("weight");
    if (!obj) return;

    obj.setTextBackgroundColor('rgb(0,200,0)');

    canvas.renderAll();
    });

i have to do set the background color to text only, following like this link , but i am getting background color to text in square, not for text only enter link description here

so how do i set the

obj.setTextBackgroundColor('rgb(0,200,0)');

Upvotes: 3

Views: 7213

Answers (3)

Tom
Tom

Reputation: 4662

var obj = canvas.getActiveObject();

if (obj.isType('text')) {
    obj.textBackgroundColor = "#ccc"; // or obj.backgroundColor = "#ccc";
    canvas.renderAll();
}

Upvotes: 4

letiagoalves
letiagoalves

Reputation: 11302

Use fabric.Object set(key, value) function.

obj.set('backgroundColor', 'rgb(0,200,0)');

Upvotes: 9

GautamD31
GautamD31

Reputation: 28763

Try with .css() of property background-color like

obj.css('background-color', 'rgb(0,200,0)');

Or you can give the color directly like

obj.css('background-color', 'red');

Upvotes: -6

Related Questions