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

374
Visualizações
Detect collision between two text object (not the bounding box)

I'm trying to check for a collision between 2 text objects and using the intersectsWithObject. It's working but it's taking the bouding rect into account. Is it possible to check on pixel level?

Current behaviour:

enter image description here

Wanted behaviour:

enter image description here

const canvas = new fabric.Canvas('canvas');
canvas.setWidth(document.body.clientWidth);
canvas.setHeight(document.body.clientHeight);


const text = new fabric.Textbox('some text', {
  width: 300,
  fontSize: 70,
  top: 120,
  left: 100
});

const text2 = new fabric.Textbox('some more text', {
  width: 350,
  fontSize: 50,
  top: 200,
  left: 20,
})


if (text.intersectsWithObject(text2, true, true)) {
  text.set('fill', 'red');
}
else {
  text.set('fill', 'black');
}

canvas.on('after:render', function() {
  canvas.contextContainer.strokeStyle = '#555';

  canvas.forEachObject(function(obj) {
    var bound = obj.getBoundingRect();

    canvas.contextContainer.strokeRect(
      bound.left + 0.5,
      bound.top + 0.5,
      bound.width,
      bound.height
    );
  })
});


canvas.add(text);
canvas.add(text2);

https://jsbin.com/menadejato/edit?js,console,output

about 4 years ago · Juan Pablo Isaza
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