Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:17:41+00:00 2026-06-11T16:17:41+00:00

I am working on a application that uses Fabric.js . I need to be

  • 0

I am working on a application that uses Fabric.js.

I need to be able to change the fill of the rectangle that I put up as background.

I use canvas.getActiveObject() to change the fill of the rectangle. Unfortunately I cant seem to find a method that will change the Fill of the rectangle.

Here is my code:

<html>
  <head>
    <title>Text Rendering Example</title>
    <script src="fabric.js"></script>  
    <script src="canvas2image.js"></script>
    <script src="base64.js"></script>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
    
  </head>
  <body>
    <canvas id="canvas" width="1000" height="600" style="border:1px solid #000000"></canvas>
    
    
    
    <form name = "boxForm" onsubmit="addBox()">
        Width: <input type="text" name="firstname" /><br />
        Text: <input id="text-control" type="text" name="textString" />
        Text Color: <input id="text-color" type="text" value = "#FF0000" name="textColor" />
        Background Color: <input id="background-color" type="text" value = "#333333" name="backgroundColor" />
    </form> 
    
    <button onclick="addBox()">Background</button>
    
    <button onclick="addText()">Add Text</button>
    
    <!--button onclick="updateControls()">Edit Text</button-->
    
    <button onclick="saveImage()">File</button>

    <script type="text/javascript" >
    
        var canvas = new fabric.Canvas('canvas');       
        
        var $ = function(id){return document.getElementById(id)};
      
        var textArray = new Array();
        
        var textControl = $('text-control');        
        var textColor = $('text-color');
        var backgroundColor = $('background-color');
      
        function addBox()
        {       
            var rect = new fabric.Rect({
            width: 1000,
            height: 600,
            top: 300,
            left: 500,
            fill: 'rgba(51,51,51,1)',
            draggable: false
            });
            rect.lockMovementX = true;
            rect.lockMovementY = true; 
            rect.lockUniScaling = true; 
            rect.lockRotation = true; 

            canvas.add(rect);
        }

        function addText()
        {
            var content = document.boxForm.textString.value;
            var color = document.boxForm.textColor.value;
            
            text = new fabric.Text(content, {               
                left: 100, 
                top: 100,
                fill: color
            });
            text.lockUniScaling = true; 
            text.lockRotation = true;

            //textArrayAdd(text);   
                
            canvas.add(text);
        }
        
        textControl.onchange = function() {
            canvas.getActiveObject().setText(this.value);
            canvas.renderAll();
        };
        
        textColor.onchange = function() {
            canvas.getActiveObject().setColor(this.value);
            canvas.renderAll();
        };
        
        /*----This is where I change the color of the Rectangle-----*/

        backgroundColor.onchange = function() {         
            canvas.getActiveObject().fillRect( 20, 100, 100, 50 );
            canvas.renderAll();
        };

        /*----This is where I change the color of the Rectangle-----*/

        
        /*function textArrayAdd(obj)
        {
            textArray.push(obj);
        }*/
        
        function updateControls() {         
            textControl.value = canvas.getActiveObject().getText();
        }
        
        canvas.on({
            'object:selected': updateControls,
        });
                
        /*var strDataURI = canvas.toDataURL('image/png'); 
        
        function saveImage()
        {           
            Canvas2Image.saveAsPNG(canvas); 
        }*/
        
        
    </script>
    
  </body>
</html>

Any ideas on how to change it? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:17:42+00:00Added an answer on June 11, 2026 at 4:17 pm

    Fixed the problem by using basic set method of fabric:

    backgroundColor.onchange = function() {         
        canvas.getActiveObject().set("fill", this.value);
        canvas.renderAll();
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Web Application that uses Google map. I need to verify
I'm working on an application that uses MSMQ for interprocess communication, and I need
I need a working example of an application that uses an expandableListView. I have
I am working on an application that uses Oracle's built in authentication mechanisms to
I'm working on an application that uses multiple threads to process its data. The
I am currently working on an application that uses a TableViewer in several places
I am working on a php that application that uses ajax. On all the
I am working on an iOS application that uses a SQLite database. In the
I am working on a Java UI application that uses JDBC. I assume the
I am working on a new application that uses a jet (MS Access) database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.