SVG过滤器,使半透明的像素不透明?

时间:2018-12-13 05:25:38

标签: svg svg-filters

我将如何创建svg过滤器以将所有半透明像素映射到不透明像素?所有透明像素应保持透明。

我正在尝试创建形状遮罩,但需要遮罩的边缘完全不透明。否则,使用口罩时会留下光环。这是我尝试使用此SVG过滤器删除的光环的示例:https://codepen.io/jedierikb/pen/yGYqKK

enter image description here

This answer here的作用类似-使用feComponentTransfer将所有半透明像素设置为透明像素。

建议?

2 个答案:

答案 0 :(得分:2)

这是一种方式。除0以外的所有A值都设置为1(不透明)。

var ctx = canvas.getContext('2d');
ctx.fillStyle = '#ABEDBE';
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fillStyle = 'black';
ctx.font = '14px sans-serif';
ctx.textAlign = 'center';

// first without filter
ctx.fillText('no filter', 60, 20);
drawArc();
drawTriangle();
// then with filter
ctx.setTransform(1, 0, 0, 1, 120, 0);
ctx.filter = 'url(#remove-alpha)';
// and do the same ops
ctx.fillText('no alpha', 60, 20);
drawArc();
drawTriangle();

// to remove the filter
ctx.filter = 'none';


function drawArc() {
  ctx.beginPath();
  ctx.arc(60, 80, 50, 0, Math.PI * 2);
  ctx.stroke();
}

function drawTriangle() {
  ctx.beginPath();
  ctx.moveTo(60, 150);
  ctx.lineTo(110, 230);
  ctx.lineTo(10, 230);
  ctx.closePath();
  ctx.stroke();
}
// unrelated
// simply to show a zoomed-in version
var zCtx = zoomed.getContext('2d');
zCtx.imageSmoothingEnabled = false;
canvas.onmousemove = function drawToZoommed(e) {
  var x = e.pageX - this.offsetLeft,
    y = e.pageY - this.offsetTop,
    w = this.width,
    h = this.height;
    
  zCtx.clearRect(0,0,w,h);
  zCtx.drawImage(this, x-w/6,y-h/6,w, h, 0,0,w*3, h*3);
}
<svg width="0" height="0" style="position:absolute;z-index:-1;">
  <defs>
    <filter id="remove-alpha" x="0" y="0" width="100%" height="100%">
      <feComponentTransfer>
        <feFuncA type="discrete" tableValues="0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
                                              1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"></feFuncA>
      </feComponentTransfer>
      </filter>
  </defs>
</svg>

<canvas id="canvas" width="250" height="250" ></canvas>
<canvas id="zoomed" width="250" height="250" ></canvas>

答案 1 :(得分:1)

为了与Paul的答案进行比较,下面是一个带有type="linear" slope="255"

的过滤器

var ctx = canvas.getContext('2d');
ctx.fillStyle = '#ABEDBE';
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fillStyle = 'black';
ctx.font = '14px sans-serif';
ctx.textAlign = 'center';

// first without filter
ctx.fillText('no filter', 60, 20);
drawArc();
drawTriangle();
// then with filter
ctx.setTransform(1, 0, 0, 1, 120, 0);
ctx.filter = 'url(#remove-alpha)';
// and do the same ops
ctx.fillText('no alpha', 60, 20);
drawArc();
drawTriangle();

// to remove the filter
ctx.filter = 'none';


function drawArc() {
  ctx.beginPath();
  ctx.arc(60, 80, 50, 0, Math.PI * 2);
  ctx.stroke();
}

function drawTriangle() {
  ctx.beginPath();
  ctx.moveTo(60, 150);
  ctx.lineTo(110, 230);
  ctx.lineTo(10, 230);
  ctx.closePath();
  ctx.stroke();
}
// unrelated
// simply to show a zoomed-in version
var zCtx = zoomed.getContext('2d');
zCtx.imageSmoothingEnabled = false;
canvas.onmousemove = function drawToZoommed(e) {
  var x = e.pageX - this.offsetLeft,
    y = e.pageY - this.offsetTop,
    w = this.width,
    h = this.height;
    
  zCtx.clearRect(0,0,w,h);
  zCtx.drawImage(this, x-w/6,y-h/6,w, h, 0,0,w*3, h*3);
}
<svg width="0" height="0" style="position:absolute;z-index:-1;">
  <defs>
    <filter id="remove-alpha" x="0" y="0" width="100%" height="100%">
      <feComponentTransfer>
        <feFuncA type="linear" slope="255"></feFuncA>
      </feComponentTransfer>
      </filter>
  </defs>
</svg>

<canvas id="canvas" width="250" height="250" ></canvas>
<canvas id="zoomed" width="250" height="250" ></canvas>