Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda