用Javascript裁剪图像

3

到目前为止,我还没有找到任何可以替代使用clip属性的方法。我尝试过使用负边距,但是没有成功。现在我的最后希望是使用javascript。我已经搜索过了,但是没有真正找到我想要的。

我想要的是裁剪图片的边缘,这样当模糊滤镜应用于图像时,边缘不会被模糊,而是保持清晰。是的,我确实使用了svg滤镜,但是没有找到使过渡平滑的合适方法。

<style>
#content{
background:#fff;
}

.one{
width:455px;
height:213px;
overflow:hidden;
display:inline-block;
vertical-align:top;
}

.one img{
position:relative;
top:-30px;
left:-7px;
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-ms-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}

.one img:hover{
filter: blur(3px); -webkit-filter: blur(3px); -moz-filter: blur(3px);
-o-filter: blur(3px); -ms-filter: blur(3px);
filter: url(blur.svg#blur);
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-ms-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
</style>

<div id="content">

<span class="one">
<a href="/"><img src="http://0.tqn.com/d/create/1/0/z/I/4/-/forthebirds.jpg" /></a>
</span>

</div>

请问您能否展示一下您目前正在使用的代码? - Undefined
当鼠标悬停在图片上时,仍然可以看到边缘模糊。我甚至尝试减小.one img的宽度和位置,但没有任何效果。http://jsfiddle.net/QktNN/ - Xceptic
请在帖子中添加一些代码,而不仅仅是链接到一个Fiddle(虽然这也很有帮助)。除此之外,例子似乎使用了overflow:hidden技术,但还存在一些尖锐的边缘...? - David Hellsing
该死,是的,这很棘手。好问题。我还没有找到一个通用的仅使用CSS的解决方案。 - Jacob Mouka
1个回答

0

您可以通过JavaScript剪裁任何图像,方法如下:

  • 创建一个大小与剪裁区域相同的<canvas>标签

  • 使用canvas.getContext().drawImage()从剪裁源坐标在画布内绘制图像,也称为切片

https://developer.mozilla.org/en-US/docs/HTML/Canvas/Tutorial/Using_images#Slicing

自然,微软浏览器仍然拥有活跃的市场份额,可能需要他们自己的魔法,Flash扩展或其他东西来让雷蒙德感到满意。

另外,因为您显然正在进行图像过滤,所以这可能会对您非常有兴趣。

http://www.html5rocks.com/en/tutorials/canvas/imagefilters/

http://www.pixastic.com/lib/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接