gpt4 book ai didi

html - 使用 CSS 在黑色叠加层上添加模板 - 倒圆掩码

转载 作者:行者123 更新时间:2023-11-28 00:08:29 25 4
gpt4 key购买 nike

我有一张 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>

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

iPhone screenshot with black overlay

这可能吗?谢谢。

最佳答案

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

DIV 叠加层:

LIVE DEMO

我建议创建一个不透明的大方形 .png,每个 Angular 有四个 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;
}

enter image description here


否则使用 Canvas :)

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';

关于html - 使用 CSS 在黑色叠加层上添加模板 - 倒圆掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16972349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com