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

157
Visualizações
Fading Canvas from Opacity 1 to 0 on Edges

I am creating a Canvas with an image inside using

context.drawImage(image, (cWidth - iRatio)/2, 0, iRatio, cHeight);

And behind the canvas there is a Blue Wallpaper inside a Div.

Canvas

As you can see on the image (done on Photoshop), the Canvas (Image with the Sun) is fading from opacity 1 to 0.

How can I do it in Canvas with JS or CSS ? I want the Canvas edges(All, so Left, Right, Top and botoom) to have an opacity 0 and fading to center with opacity 1.

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

0

You can use mask:

img {
  mask:radial-gradient(circle, white 30%, transparent 60%);
}
<img src="https://i.stack.imgur.com/Onn3y.png" />

MDN has many example - you can use gradients, svg, and even pngs to get the effect you want.

There's also mask-border but it isn't as well supported (for example, Firefox doesn't support it yet).

about 4 years ago · Juan Pablo Isaza Relatório

0

Using filter

Use ctx.filter to create a blur and use that blur to keep pixels you want and remove pixels from edge using ctx.globalCompoiteOperation = "detination-in"

Example below uses function featherEdge feather edges of canvas content.

const ctx = canvas.getContext("2d");
const img = new Image;
img.src = "https://i.stack.imgur.com/Onn3y.png";
img.addEventListener("load", draw, {once: true});
function draw() {    
    ctx.drawImage(img, 0, 0);
    featherEdge(20, 2);
}

function featherEdge(blurRadius, inset) {
    ctx.filter = "blur(" + blurRadius + "px)";
    ctx.globalCompositeOperation = "destination-in";
    const inBy = blurRadius + inset;
    ctx.fillRect(inBy, inBy, canvas.width - inBy * 2, canvas.height - inBy * 2);
}
canvas {
   background: cyan;
}
<canvas id="canvas" width="200" height="200"></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