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

460
Visualizações
How to Load Image inside a rectangle in Fabric.js (2022)

This issue has taken about four months of stress and research. I can't find a good solution for fabric v.4 +

Here's one of the solutions I learned from online: https://jsfiddle.net/naimsajjad/8w7hye2v/8/

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.3/fabric.min.js"> </script>

  function ImageNew(){
    let img01URL = prompt("Please enter your image URL", "https://"); 

    
fabric.Image.fromURL(img01URL, function(oImg) {
  oImg.scale(.25);
  oImg.left = 10;
  oImg.top = 10;
  oImg.clipPath = canvas.getActiveObject();
  oImg.clipTo = function(ctx) {
    clipObject(this, ctx)
  }
  canvas.add(oImg);
  canvas.renderAll();
});
}


function clipObject(thisObj, ctx) {
    if (thisObj.clipPath) {
      ctx.save();
      if (thisObj.clipPath.fixed) {
        var retina = thisObj.canvas.getRetinaScaling();
        ctx.setTransform(retina, 0, 0, retina, 0, 0);
        // to handle zoom
        ctx.transform.apply(ctx, thisObj.canvas.viewportTransform);
        thisObj.clipPath.transform(ctx);
      }

      thisObj.clipPath._render(ctx);
      ctx.restore();
      ctx.clip();
      var x = -thisObj.width / 2,
        y = -thisObj.height / 2,
        elementToDraw;

      if (thisObj.isMoving === false && thisObj.resizeFilter && thisObj._needsResize()) {
        thisObj._lastScaleX = thisObj.scaleX;
        thisObj._lastScaleY = thisObj.scaleY;
        thisObj.applyResizeFilters();
      }
      elementToDraw = thisObj._element;
      elementToDraw && ctx.drawImage(elementToDraw,
        0, 0, thisObj.width, thisObj.height,
        x, y, thisObj.width, thisObj.height);
      thisObj._stroke(ctx);
      thisObj._renderStroke(ctx);
    }
  }

The thing is it works if I load it a 3.6 Fabric Library. But in modern versions, it doesn't work.

In my app, I load a grey colorless rectangle through JSON. And user selects that rectangle, and loads an image into it. So that the image will be cropped into the rectangle.

I've been trying quite a lot in trying to fix this. Please help me.

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