您可以使用 CSS 为 PNG 内容添加非方形阴影吗?

发布于 2024-11-16 19:06:20 字数 87 浏览 2 评论 0原文

是否可以对 PNG 内容添加阴影?

不是一个正方形,而是一个物体阴影
作用于 PNG 的非透明内容。

Is it possible to do a drop shadow on the content of a PNG?

Not a square, but an object drop shadow that
acts on the non-transparent content of the PNG.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅忆 2024-11-23 19:06:20

这绝对是可能的。

使用过滤器示例:

img { 
    -webkit-filter: drop-shadow(5px 5px 5px #222);
    filter:         drop-shadow(5px 5px 5px #222); 
}

It's definitely possible.

Using filters, sample:

img { 
    -webkit-filter: drop-shadow(5px 5px 5px #222);
    filter:         drop-shadow(5px 5px 5px #222); 
}
明天过后 2024-11-23 19:06:20

在 CSS 中不可能做到这一点。但是,很可能通过画布来完成此操作,但效率会有些低(因为每次都由客户端处理)并且依赖于 JavaScript。在 PNG 中执行此操作会更容易,并且适用于更多浏览器。

如果您想了解更多信息,请在网上搜索“html canvas Blur”和“html canvas load image”等内容。或者更好的是,使用可以完成这一切的画布阴影功能。

这是一个示例: http://philip.html5.org/demos/canvas/shadows /various.html

  • 从画布集 context.shadow(Color|OffsetX|OffsetY|Blur) 创建上下文
  • 根据需要
  • 使用 context.drawImage 从 img 标签加载 PNG
  • 啊!阴影!

还有一个好处:

  • 如果您想导出为 PNG,请使用 context.toDataURL(制作一个网络应用程序,您将 PNG 文件放入其中,它就会为您提供阴影!)

It's not possible to do that in CSS. However, it's quite possible to do it through a canvas, but it will be somewhat inefficient (as it's processed by the client each time) and will be JavaScript dependent. Doing it in the PNG will be easier and will work on more browsers.

If you want more information about it, search the web for things like "html canvas blur" and "html canvas load image". Or better still, use the canvas shadow functionality which can do it all.

Here's an example: http://philip.html5.org/demos/canvas/shadows/various.html

  • create context from canvas
  • set context.shadow(Color|OffsetX|OffsetY|Blur) as desired
  • load PNG from img tag with context.drawImage
  • ah! the shadows!

And a bonus:

  • use context.toDataURL if you want to export to PNG (make a web app which you drop PNGs in and it gives you shadows!)
风渺 2024-11-23 19:06:20

时代如何变迁。现在可以在某些浏览器中,如当前接受的答案所示。


<罢工>
使用CSS不可能做到这一点:

这就是我认为你所要求的。

How times change. It's now possible in some browsers, as shown in the currently accepted answer.



It's not possible to do this using CSS:

That's what I assume you're asking for.

尴尬癌患者 2024-11-23 19:06:20

在 CSS 可用之前,您可以尝试我的方法。

我的示例使用这张图片:

original image

因为它具有透明背景并且不是正方形(对于 CSS 的 box-shadow开始行动)。它不是很花哨,但是可以很好地满足这个小而简单的教程。

我采取的下一步是根据上面的图像创建另一张图像,将其放置在原始图像下方。

步骤1.添加模糊:

模糊原始

步骤2。删除了光线和对比度:

the Shadow

所以我有了原始图像和阴影。

接下来,我将把它显示为阴影:

样式:

.common {width: 100px;height: 100px;background-size: 100% 100%; position:absolute;}

.divOriginal {background-image: url(../img/original.png);top:2em;left:2em;z-index:10;}

.divShadow {background-image: url(../img/shadow.png);top: 3em;left: 2.5em;z-index:8;}

发挥魔法:

添加一个 div 并设置 attribute class="divOriginal common" 和另一个 class="divShadow common"

结果应该是:

result:pseudo-shadow

干杯!

阿迪

Until it will be available to CSS, you can try my approach.

My example uses this picture:

original image

because it has a transparent background and it isn't square (for CSS's box-shadow to get into action). It's not very fancy, but serves this small and simple tutorial fine.

Next step I took was to create another image, based on the one above, to place it under the original.

Step1. Add blur:

blurred original

Step2. Removed light and contrast:

the shadow

So I have the original and the shadow.

Next I will display it as if it is a shadow:

Style:

.common {width: 100px;height: 100px;background-size: 100% 100%; position:absolute;}

.divOriginal {background-image: url(../img/original.png);top:2em;left:2em;z-index:10;}

.divShadow {background-image: url(../img/shadow.png);top: 3em;left: 2.5em;z-index:8;}

Do the magic:

Add one div and set attribute class="divOriginal common" and another one with class="divShadow common".

The result should be:

result: pseudo-shadow

Cheers!

Adi

胡渣熟男 2024-11-23 19:06:20

我根据 Chris Morgan 关于画布的建议为此编写了一个 jQuery 插件。您可以在 GitHub 上找到它:https://github.com/Kukunin/image-shadow

I've wrote a jQuery plugin for this, based on Chris Morgan's suggestion about canvas. You can find it on GitHub here: https://github.com/Kukunin/image-shadow

小瓶盖 2024-11-23 19:06:20

使用createjs
样本可以在以下位置找到:
JSFiddle

shadowTarget.shadow = shadow;

stage.addChild(shadowTarget);

shadowTarget.x = 500 / 2;
shadowTarget.y = 450 / 2;

shadowTarget.regX = shadowTarget.image.width/2;
shadowTarget.regY = shadowTarget.image.height/2;

Use createjs
sample can be found at
JSFiddle

shadowTarget.shadow = shadow;

stage.addChild(shadowTarget);

shadowTarget.x = 500 / 2;
shadowTarget.y = 450 / 2;

shadowTarget.regX = shadowTarget.image.width/2;
shadowTarget.regY = shadowTarget.image.height/2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文