Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
FabricJS modify custom properties

I am using the FabricJS graphics library and have added an additional property (name) to a fabric.Rect object. All well and good and it serializes out to JSON correctly.

I am struggling though with the code needed to allow me to subsequently change the customer property once set i.e. to change 'some name' to something else. It is driving me a bit crazy.

Any additional help really appreciated.

Thanks,

Shaun

                const o = new fabric.Rect({
                    width: width,
                    height: height,
                    fill: tableFill,
                    stroke: tableStroke,
                    strokeWidth: 2,
                    shadow: tableShadow,
                    originX: "center",
                    originY: "center",
                    centeredRotation: true,
                    snapAngle: 45,
                    selectable: true,
                    strokeUniform: true
                });

                o.toObject = (function(toObject) {
                    return function(propertiesToInclude) {
                        return fabric.util.object.extend(toObject.call(this, propertiesToInclude), {
                            name: 'some name'
                        });
                    };
                })(o.toObject);

                console.log(o.toObject().name)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So, basically run this code to allow any additional properties to be serialised to and from JSON. In this example a property called name is added.

const originalToObject = fabric.Object.prototype.toObject;
const myAdditional = ['name'];
fabric.Object.prototype.toObject = function (additionalProperties) {
    return originalToObject.call(this, myAdditional.concat(additionalProperties));
}

Then create a new Fabric object and set or get the additional properties as needed...

const o = new fabric.Rect({
    width: width,
    height: height,
    fill: tableFill,
    stroke: tableStroke,
    strokeWidth: 2,
    shadow: tableShadow,
    originX: "center",
    originY: "center",
    centeredRotation: true,
    snapAngle: 45,
    selectable: true,
    strokeUniform: true
});

o.name = 'Fred'
console.log(o.toJSON())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!