Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

458
Views
Cómo cargar una imagen dentro de un rectángulo en Fabric.js (2022)

Este número ha llevado alrededor de cuatro meses de estrés e investigación. No puedo encontrar una buena solución para fabric v.4 +

Esta es una de las soluciones que aprendí en línea: 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); } }

La cuestión es que funciona si lo cargo en una biblioteca Fabric 3.6. Pero en las versiones modernas, no funciona.

En mi aplicación, cargo un rectángulo gris incoloro a través de JSON. Y el usuario selecciona ese rectángulo y carga una imagen en él. Para que la imagen se recorte en el rectángulo.

He estado intentando mucho para tratar de arreglar esto. Por favor, ayúdame.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!