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

331
Visualizações
FabricJS - How to prevent leaving object from the given object?

I was able to prevent leaving objects from the canvas but I was not able to prevent leaving child objects from the given object.

I tried to do it in this way (see code example below) but this locks the object leaving in the same size (width, height) of the locked object but positioned absolutely (top, left). I need to prevent leaving in the same coords there the parent object is positioned (see please the attached image).

Any ideas are welcome.

enter image description here

preventLeavingInsideBox(e: IEvent) {
    let activeObject = e.target;
    if ((activeObject.get('left') - (activeObject.get('width') * activeObject.get('scaleX')  / 2) < 0))
      activeObject.set('left', activeObject.get('width') * activeObject.get('scaleX')  / 2);
    if ((activeObject.get('top') - (activeObject.get('height') * activeObject.get('scaleY')  / 2) < 0))
      activeObject.set('top', activeObject.get('height') * activeObject.get('scaleY')  / 2);
    if (activeObject.get('left') + (activeObject.get('width') * activeObject.get('scaleX')  / 2) > this.rect.width)
    {
      var positionX = this.rect.width - (activeObject.get('width') * activeObject.get('scaleX'))  / 2;
      activeObject.set('left', positionX > this.rect.width / 2 ? positionX : this.rect.width / 2);
    }
    if (activeObject.get('top') + (activeObject.get('height') * activeObject.get('scaleY') / 2) > this.rect.height)
    {
      var positionY = this.rect.height - (activeObject.get('height') * activeObject.get('scaleY') / 2);
      activeObject.set('top', positionY > this.rect.height / 2 ? positionY : this.rect.height / 2);
    }

    //below just prevention for object from getting width or height greater than canvas width and height
    if (activeObject.get('width')  * activeObject.get('scaleX') > this.rect.width)
    {
      activeObject.set('scaleX', this.rect.width / activeObject.get('width'));
    }
    if (activeObject.get('height') * activeObject.get('scaleY') > this.rect.height)
    {
      activeObject.set('scaleY', this.rect.height / activeObject.get('height'));
    }
  }
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