如何用透明度绘制png图像轮廓(边框)

时间:2013-02-12 13:21:31

标签: jquery html5 css3

如何以透明度绘制png图像轮廓(边框)。

就像我们有这张图片:enter image description here

我们想要这个:enter image description here

我正在通过HTML5创建图像阴影,但无法获得我想要的。 我想用HTML5或jquery或两者绘制它,如果有人有其他想法,那么请告诉我。

<img id="scream" src="images/apple/Tool.png" alt="The Scream" style="display:none">
<p>Canvas:</p>
<canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    var img=document.getElementById("scream");

    ctx.shadowColor="#000000";
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    ctx.shadowBlur = 15;
    ctx.drawImage( img, 25, 25 );
</script>

1 个答案:

答案 0 :(得分:0)

我认为this问题很有用 也许这个代码

<div id="fadeMe">
    <img src="transparent.png" alt="" />
</div>

另一种解决这个问题的方法是使用这个简单的jQuery插件,因为我无法改变结构。我会给予归属但我真的不记得我在哪里找到它。

/* IE PNG fix multiple filters */
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
            if (!($.browser.msie)) return this;
            var emptyimg = "empty.gif"; //Path to empty 1x1px GIF goes here
            sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
            this.each(function() {
                var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                    imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                    src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                if (isImg) this.src = emptyimg;
                else this.style.backgroundImage = "url(" + emptyimg + ")";
            });
            return this;
        }
    });
})(jQuery);