我正在使用clipPath
对图片应用不同的“遮罩”效果。
如何在悬停时用彩色填充剪裁的图像?我尝试在CSS中使用:hover
,但这似乎不起作用,除非我的目标是错误的元素。
我在这个项目中使用jQuery,所以如果需要JS解决方案,我可以依靠jQuery。
以下是我正在使用的HTML:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
<defs>
<clipPath id="ellipse">
<ellipse cx="50" cy="35.5" rx="49.5" ry="35" />
</clipPath>
<clipPath id="hexagon">
<polygon points="25, 0 75, 0 100, 43.30127018922193 75, 86.60254037844386 25, 86.60254037844386 0, 43.30127018922193"/>
</clipPath>
<clipPath id="rectangle">
<rect x="0" y="0" width="100" height="70"></rect>
</clipPath>
</defs>
<g>
<image preserveAspectRatio="none" x="0" y="0" width="100%" height="100%" xlink:href="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Bucephala-albeola-010.jpg/800px-Bucephala-albeola-010.jpg" id="clippy" clip-path="url(#hexagon)">
</g>
</svg>
答案 0 :(得分:7)
您可能希望使用滤镜效果在悬停时为图像添加一些颜色(see Tinkerbin):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
<style type="text/css">
image:hover {
filter:url(#Matrix);
}
</style>
<defs>
<clipPath id="ellipse">
<ellipse cx="50" cy="35.5" rx="49.5" ry="35" />
</clipPath>
<clipPath id="hexagon">
<polygon points="25, 0 75, 0 100, 43.30127018922193 75, 86.60254037844386 25, 86.60254037844386 0, 43.30127018922193"/>
</clipPath>
<clipPath id="rectangle">
<rect x="0" y="0" width="100" height="70"></rect>
</clipPath>
<filter id="Matrix" filterUnits="objectBoundingBox"
x="0%" y="0%" width="100%" height="100%">
<feColorMatrix type="matrix" in="SourceGraphic"
values="1 0 0 0 .5
.1 .9 0 0 0
.1 0 .9 0 0
0 0 0 1 0"/>
</filter>
</defs>
<g>
<image preserveAspectRatio="none" x="0" y="0" width="100%" height="100%" xlink:href="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Bucephala-albeola-010.jpg/800px-Bucephala-albeola-010.jpg" id="clippy" clip-path="url(#hexagon)">
</g>
</svg>
编辑:有关过滤器的一些说明:
应用的滤镜会更改每个光栅像素的颜色。它采用原始颜色值,将<feColorMatrix>
指定的矩阵应用于颜色矢量,生成的颜色矢量成为显示的颜色。
矩阵如何运作?
矩阵由四行组成。第一行计算新的红色成分,第二行计算绿色,第三行计算蓝色,第四行。
每行中五个数字的含义是什么?第一个数字乘以原始颜色的红色成分,第二个数字乘以绿色,第三个蓝色,第四个alpha。总结了所有四个产品,并且还添加了行中的第五个值(作为不依赖于任何原始颜色分量的常量)。
让我们看看上面的例子:假设我们有一个灰色像素,颜色值如
rgba(25%,25%,25%,1)
结果红色值是多少?计算红色值的第一行是
1 0 0 0 .5
我们计算以下内容:
1*r + 0*g + 0*b + 0*a + .5
= 1*.25 + 0*.25 + 0*.25 + 0*1 + .5 = .75
这意味着,像素的最终红色分量为75%。其他组件的计算方法类似。
答案 1 :(得分:1)
不确定这是否正是您想要的。鼠标事件不会发送到cliparea之外的区域。快速&amp;脏,在IE9中工作,没有在FF中测试过。例如。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
<script type="application/ecmascript">
function fillit(evt) {
document.getElementById('fillarea').setAttribute('display', 'visible');
}
function emptyit(evt) {
document.getElementById('fillarea').setAttribute('display', 'none');
}
</script>
<defs>
<clipPath id="ellipse">
<ellipse cx="50" cy="35.5" rx="49.5" ry="35" />
</clipPath>
<clipPath id="hexagon">
<polygon points="25, 0 75, 0 100, 43.30127018922193 75, 86.60254037844386 25, 86.60254037844386 0, 43.30127018922193" />
</clipPath>
<clipPath id="rectangle">
<rect x="0" y="0" width="100" height="70"></rect>
</clipPath>
</defs>
<g>
<image preserveAspectRatio="none" x="0" y="0" width="100%" height="100%"
xlink:href="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Bucephala-albeola-010.jpg/800px-Bucephala-albeola-010.jpg"
id="clippy" clip-path="url(#hexagon)" onmouseover="fillit(evt)" />
<rect x="0" y="0" width="100%" height="100%" fill="red" display="none"
id="fillarea" clip-path="url(#hexagon)" onmouseout="emptyit(evt)" />
</g>
</svg>