- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
接下来是我的问题。如何生成 SVG 或 Canvas 以使其像全屏页面覆盖(50% 透明)。但我想突出显示此叠加层下方的一些 div。我知道那个 div 的坐标和宽高。如何根据叠加层上的那个 div 裁剪区域,以便该区域完全透明并清楚地看到该 div?该 div 将位于不同页面的不同位置。我不擅长 canvas 和 svg。请帮助我。
最佳答案
希望这就是您所需要的。在 DOM 上,您有 2 个 div:#square
和 #circle
。
我使用的是半透明(不透明度 .9)叠加层,它被剪切在 #square
上。请阅读我的评论。
var SVG_NS = 'http://www.w3.org/2000/svg';
// define the position of the #square
let _left = 120;
let _top = 230;
let w = 150;
let h = 150;
// recalculate the points needed for the clip-path
let p = svg.createSVGPoint();
p.x = _left;
p.y = _top;
let ctm = svg.getScreenCTM().inverse();
p = p.matrixTransform(ctm);
let p1 = svg.createSVGPoint();
p1.x = w;
p1.y = h;
let ctm1 = svg.getScreenCTM().inverse();
p1 = p1.matrixTransform(ctm1);
//define the d for the clip path
let oClipRect={
d:`M0,0L100,0L100,500L0,500L0,0
M${p.x+p1.x},${p.y}
L${p.x},${p.y}
L${p.x},${p.y+p1.y}
L${p.x+p1.x},${p.y+p1.y}
L${p.x+p1.x},${p.y}`,
id:"clip_rect"
}
//a big overlay rectangle that gets clipped
//The rectangle is semitransparent so that you can see the the circle
let oRect={
width:"100%",
height:"100%",
"clip-path":"url(#_clip)",
fill:"rgba(0,0,0,.9)"
}
drawElmt(oClipRect,"path", _clip);
drawElmt(oRect,"rect", svg);
// function update needed on resize
function update(){
p = svg.createSVGPoint();
p.x = _left;
p.y = _top;
ctm = svg.getScreenCTM().inverse();
p = p.matrixTransform(ctm);
p1 = svg.createSVGPoint();
p1.x = w;
p1.y = h;
ctm1 = svg.getScreenCTM().inverse();
p1 = p1.matrixTransform(ctm1);
d = `M0,0L100,0L100,500L0,500L0,0
M${p.x+p1.x},${p.y}
L${p.x},${p.y}
L${p.x},${p.y+p1.y}
L${p.x+p1.x},${p.y+p1.y}
L${p.x+p1.x},${p.y}`;
console.log(d)
clip_rect.setAttributeNS(null,"d",d);
}
setTimeout(function() {
update();
addEventListener('resize', update, false);
}, 15);
function drawElmt(o,el, parent) {
var elmt = document.createElementNS(SVG_NS, el);
for (var name in o) {
if (o.hasOwnProperty(name)) {
elmt.setAttributeNS(null, name, o[name]);
}
}
parent.appendChild(elmt);
return elmt;
}
*{margin:0;padding:0;}
#wrap {
height: auto;
position: relative;
}
.test {
width: 150px;
height: 150px;
position: absolute;
}
#square{background: gold;left: 120px;
top: 230px;}
#circle{background:red;left: 150px;
top: 270px;border-radius:50%;}
svg {
border: 1px solid;
position: absolute;
}
<div id="wrap">
<div id="square" class="test"></div>
<div id="circle" class="test"></div>
<svg id="svg" viewBox="0 0 100 500">
<clipPath id="_clip">
</clipPath>
</svg>
</div>
关于javascript - SVG 或 Canvas,如何按坐标和 div 大小切割正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724303/
Python 中是否有任何预制的优化工具/库来切割/切片值“小于”某物的列表? 问题来了:假设我有一个列表: a=[1,3,5,7,9] 我想删除所有 2 的项目,将会有很多迭代)。 我也可以使用二进
我创建了一个 fiddle 来更好地展示我想问的问题。 angle crop of pseudo element 这是一张显示所需结果的图片: li.active::after { conte
我有一个字符串如下: 2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576 我想提取
RESTEasy 看起来会忽略尾部斜杠,因此我无法编写带有和不带有尾部斜杠的两个不同的 Web 服务,此示例显示: @Path("foo") public class TestClass {
mov rax,QWORD PTR [rbp-0x10] mov eax,DWORD PTR [rax] add eax,0x1 mov DWORD PTR [rbp-0x14], eax 下
我有以下 xml: External Vendor File External vendor file for so
我在 Python 中工作,并在 Pandas 中使用剪切功能。我想让我的 pd.cut 中的 bins 基于用户定义的逗号分隔整数,具有预定义的上限和下限。换句话说,我想将 bins 预定义为 [0
我正在尝试嵌入 ShareThis将代码写入使用 Bootstrap 创建的网站。 问题是由于某种原因,两个按钮的底部都被切断了,而且我似乎无法使用 Bootstrap 列偏移将按钮居中,因为使用这种
我正在尝试模拟无线传感器节点网络,以研究网络的稳健性。我面临以下问题: 我有一个具有一些边缘容量的节点网络。这相当于算法中的网络流问题。有一个源节点(检测某些事件)和一个接收节点(我的基站)。现在,我
我有一个 UIImageView,带有特定的图像。我还有一个形状奇怪的 UIBezierPath。我想将图像剪切成该形状并返回该形状的新图像。 形式为: func getCut(bezier:UIBe
我有一个大型的全局 .nc 文件数据集,我正试图将它们裁剪到一个较小的区域。我将此区域存储为 .shp 文件。 我曾尝试使用 Qgis 中的 gdal,但需要通过转换每个变量来做到这一点,我必须为所有
我正在使用以下命令来剪切 FLAC 文件:ffmpeg -i input.flac -ss 10s -t 10s -c copy output.flacoutput.flac包含正确的音频持续时间。但
我希望为以下组件制作足迹: AG EMCO HV power supply 现在,这个组件可以通过电路板安装,虽然它不是一个很高的组件,但我希望探索这个选项。我想一个附带问题是:PCB 制造商有多喜欢
我目前正在用 C++ 编写一个小程序(在 Mac 上,其中包含一些 C 代码)并且需要从套接字中检索二进制数据(效果很好),但我还需要切断 HTTP标题。 基本上,我连接到服务器,发送 HTTP GE
我想制作一个Python程序,其中在文本文件中给出DNA序列。它有超过9000个字符。我必须将序列剪成 3 个字符所以我们的框架从1到3读取,然后4到6,然后7到9,这被称为密码子。 例如,序列是 A
我正在开发一个迷你游戏,如果我将 DIN-Ax 作为输入,我需要找出可以在 DIN-A0 纸上画多少行。例如: ;car horizontal and cdr vertical lines (defi
我在下面有这个名为 monitor.log 的报告文件 switch#sh mac address-table int g1/0/1 Mac Address Table -----
我的设备上有 MPEG-TS 文件。我想从设备上的文件开始处截断一个相当准确的时间。 使用 FFmpegWrapper作为基地,我希望能够实现这一目标。 不过,我对 ffmpeg 的 C API 有点
我导出了两个字段:name和header从数据库中使用: SELECT name, header INTO OUTFILE '/var/lib/mysql-files/myfile.txt' FIEL
这个问题在这里已经有了答案: How do you parse and process HTML/XML in PHP? (31 个答案) 关闭 9 年前。 我尝试转换以下字符串 st
我是一名优秀的程序员,十分优秀!