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

207
Visualizações
D3.js How to get value of transform attr

I have a small question. In my application I'm using d3.js. And I need to get the values of transform attribute. I found the same question How to log rotate attribute in transform using d3.js? But I am using a newer version of the library. How can I get it? Because:

<g class='myclass' transform='translate(30, 0)'>

...

d3.select('.myclass').attr('transform')

return the string.

translate(30, 0)

(I need to get only numbers 30 and 0)I didn't find any functions for this. I can get values using regex, but I think it too much in this case. There must be some normal way to get these values. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since the <g> element implements the SVGGraphicsElement interface it features a .transform property that allows access to the SVGAnimatedTransformList containing the transformations on that element. Given that there is only one translation like in your code it can be accessed like this:

d3.select("g").node()
  .transform          // get the animated transform list
  .baseVal            // get its base value
  .getItem(0)         // get the first transformation from the list, i.e. your translate
  .matrix             // get the matrix containing the values

The .matrix property is of type SVGMatrix and contains the values for the translation of the group element in its e and f properties.

Have a look at the following demo to see it in action:

const translate = d3.select("g").node() // get the node
                    .transform          // get the animated transform list
                    .baseVal            // get its base value
                    .getItem(0)         // get the first transformation from the list, i.e. your translate
                    .matrix             // get the matrix containing the values
                    
console.log(`Translate x: ${translate.e}, y: ${translate.f}`);
<script src="https://d3js.org/d3.v7.js"></script>
<svg>
  <g transform='translate(30, 0)'></g>
</svg>

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