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

148
Visualizações
Canvas repeat a svg or rect path

I need to make this: bar I tried with making a single rect but I don't know how to repeat it along the x. I tried repeating a image (svg) but it doesn't work too. I tried with background repeat in a div but the result is everytime the same: blank. I'd like to make it in canvas. This is my code:

    const canvas = document.getElementById('barre');
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = '../img/barre.svg';
img.addEventListener('load', ()=>{
    const ptrn = ctx.createPattern(img,'repeat-x');
    ctx.fillStyle = ptrn;
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

By assigning a fillStyle to a canvas you are really just doing one thing: determining a property. At this point you are not actually drawing anything yet. This is done using the appropriate drawing methods. In your case the fillRect() method would be a perfect fit. As the name implies, it draws a filled rectangle. It takes four parameters for it's position and the dimensions.

Here's an example:

const canvas = document.getElementById('barre');
const ctx = canvas.getContext('2d');
const img = new Image();
img.crossOrigin = "";
img.addEventListener('load', () => {
  const ptrn = ctx.createPattern(img, 'repeat-x');
  ctx.fillStyle = ptrn;
  ctx.fillRect(0, 0, canvas.width, canvas.height)
});
img.src = 'https://api.codetabs.com/v1/proxy?quest=https://picsum.photos/id/237/20/30';
<canvas id="barre"></canvas>

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