【问题标题】:Adding a stencil on a black overlay with CSS - Inverted circle mask使用 CSS 在黑色覆盖层上添加模板 - 倒圆蒙版
【发布时间】:2013-06-03 01:36:28
【问题描述】:

我有一个带有黑色覆盖层(有一些不透明度)的 iPhone 屏幕截图(jpeg 背景)。我需要动态添加一个具有特定半径的圆形模板,它会覆盖黑色背景,使其变得更加明显。

动态地我的意思是我必须将 x,y 坐标设置为我需要的任何值。

CSS

#img {
    display: block;
    width: 480px;
    height: 720px;
    background: url('iphone.jpg') no-repeat center center
}
div.overlay {
    display: block;
    width: 480px;
    height: 720px;
    background-color: #000;
    filter: alpha(opacity=65);
    -khtml-opacity: 0.65;
    -moz-opacity: 0.65;
    opacity: 0.65;
}
div.stencil {
    /* ??? */
}

HTML

<div id="img">
    <div class="overlay"></div>
    <div class="stencil"></div>
</div>

这是我想要实现的一个示例:

有可能吗?谢谢。

【问题讨论】:

标签: html css canvas opacity


【解决方案1】:

我将向您展示 2 个示例,使用 DIV 作为叠加层,并使用 HTML5 canvas

DIV 叠加层:

LIVE DEMO

我建议创建一个不透明的大方形 .png,每个角有四个 1/4 圆孔。
将其设置为.overlay 的重复背景。 与设置 DIV 的 packground-position: Xpx , Ypx; 相比,您将完美地瞄准任何所需区域的精确中心。

HTML:

<div id="img">
    <div class="overlay"></div>
</div>

CSS:

#img {    
    position:relative;
    overflow:hidden;
    width: 199px;
    height: 390px;
    background: url('iphone.jpg') no-repeat center center;
}

.overlay {
    position:absolute;
    height:100%;
    width:100%;
    background:url(http://i.stack.imgur.com/ohb0l.png);
    /* PLAY HERE: */
    background-position: 120px 130px;
}


否则使用画布:)

globalCompositeOperation = 'destination-out'; 可以解决问题:

以下代码将在图像上放置一个遮罩并从遮罩中移除一个圆弧

LIVE DEMO

HTML:

<canvas id="iphone"></canvas>

JS:

var cirsleSize = 30 ,  // circle radius
    circleX = 120 ,    // X pos
    circleY = 130 ;    // Y pos

// ---------------------------------------------

var canvas = document.getElementById('iphone'),
    ctx = canvas.getContext('2d'),
    img = new Image();

canvas.height=390;
canvas.width=199;

img.onload = function() {

   ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
   ctx.fillStyle = "rgba(255,255,255,0)";
   ctx.fillRect(0, 0, canvas.width, canvas.height);
   var mask = document.createElement('canvas');
   mask.width = canvas.width;
   mask.height = canvas.height;
   var maskCtx = mask.getContext('2d');
   maskCtx.fillStyle = "rgba(0,0,0,0.6)";
   maskCtx.fillRect(0, 0, mask.width, mask.height);
   maskCtx.globalCompositeOperation = 'destination-out';
   maskCtx.arc(circleX, circleY, cirsleSize, 0, 2*Math.PI);
   maskCtx.fill();
   ctx.drawImage(mask,0,0);
      
};

img.src = 'iphone_199x390.jpg';

【讨论】:

  • 这将是一个很好的开始。谢谢!
  • @Andufo 谢谢! (如果您不介意,我会将html5canvas 标签添加到您的问题中,以便其他人更轻松地找到您的Q!)祝您编码愉快!
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 2021-11-22
  • 2019-07-24
  • 2017-11-16
  • 1970-01-01
  • 2013-08-21
  • 2019-10-24
  • 1970-01-01
相关资源
最近更新 更多