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

142
Visualizações
How to draw a tag icon in canvas?

I'm trying to make a tag like icon in canvas, but having a hard time doing the punch hole in the middle with no lines.

If possible, would like to make it like in the image, since here I tried with just basics shapes. Image Example:
tag icon

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(100, 30, 150, 100);
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.translate(c.width/2,c.height/2);
ctx.moveTo(0,0);
ctx.lineTo(0,0-100);
ctx.rotate(Math.PI / 3);
ctx.lineTo(0,0-100);
ctx.closePath();
ctx.stroke();
<canvas id="myCanvas" width="500" height="260" style="border:1px solid #d3d3d3;">

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You had a good start, we just needed to draw the circle with the ctx.arc( and use the globalCompositeOperation to cut out the hole to match your image.

On my example below I created a function that does the drawing of the tag, that way we can reuse to draw multiple tags

Improvements you could do later, maybe pass more parameters to have more control over how big is that triangle also the radius of the circle, and of course add color that is all up to you.

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.lineWidth = 1;

function drawTag(x, y, w, h) {
  ctx.beginPath();
  ctx.rect(x, y, w, h);

  ctx.moveTo(x + w, y);
  ctx.lineTo(x + w + w / 2, y + h / 2);
  ctx.lineTo(x + w, y + h);
  ctx.fill();

  ctx.beginPath();
  ctx.globalCompositeOperation = 'destination-out';
  ctx.arc(x + w + w / 8, y + h / 2, h / 8, 0, 8, false)
  ctx.fill();
  ctx.globalCompositeOperation = 'source-over';
}

drawTag(10, 10, 75, 50)

ctx.translate(140,50);
ctx.rotate(1);
drawTag(0, 0, 60, 40)

ctx.translate(40,60);
ctx.rotate(2);
drawTag(0, 0, 50, 30)
<canvas id="myCanvas" style="border:1px solid #d3d3d3;">

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