简体   繁体   English

CSS background-blend-mode乘以不透明度

[英]CSS background-blend-mode multiply with opacity

I'm porting a UI-based game from Unity to Cordova. 我正在将基于UI的游戏从Unity移植到Cordova。 In Unity, I was tinting predominately white images with various colors to reuse assets. 在Unity中,我主要将白色图像着色为各种颜色以重用资产。 The rough CSS equivalent seems to be using the [mostly standard] background-blend-mode property set to multiply and have the image in the background with the desired tint color as the bg color. 粗略的CSS等效项似乎是使用[大多是标准的] background-blend-mode属性集来进行multiply ,并将背景中的图像具有所需的色调颜色作为bg颜色。

.tinted {
  background-image: url('theimg.png');
  background-size: 100% 100%;
  background-color: #0f0;
  background-blend-mode: multiply;
}

The problem is that it doesn't preserve the opacity of the image, namely the transparent parts become the tint color. 问题在于它不能保留图像的不透明度,即透明部分变成了着色颜色。 The spec says something about blending from the top down so I thought it might relate to blending with the bg color, but it doesn't work if I layer a solid color (as a gradient) on top of the image either. 规格说明了从上到下的混合操作,因此我认为这可能与与bg颜色混合有关,但是如果我在图像上方分层放置纯色(作为渐变)也行不通。

.tinted2 {
  background-image: url('theimg.png'), linear-gradient(to bottom, #0f0, #0f0);
  background-size: 100% 100%;
  background-blend-mode: multiply;
}

Reversing the order of the background images or changing the blend mode to normal, multiply or multiply, normal doesn't work either. 反转背景图像的顺序或将混合模式更改为normal, multiplymultiply, normal也不起作用。 Any suggestions on how to do this correctly using CSS? 关于如何使用CSS正确执行此操作的任何建议?

EDIT: As the answer mentions, the alpha aspect can be achieved using the mask property. 编辑:正如答案所述,可以使用mask属性实现alpha方面。 I used a combination of the two techniques to get what I needed: 我结合使用了两种技术来获得所需的信息:

.tintedMasked {
  background-image: url('theimg.png');
  background-size: 100% 100%;
  mask-image: url('theimg.png');
  mask-size: 100% 100%;
  background-color: #0f0;
  background-blend-mode: multiply;
}

If I understand correctly what you're trying to do, then background blending is not the way, but masking. 如果我正确理解您要执行的操作,则背景混合不是方法,而是遮罩。

 div { height: 200px; background-image:linear-gradient(SlateBlue, Tomato); -webkit-mask: url(https://cdn.pixabay.com/photo/2016/12/28/19/37/denied-1936877_960_720.png) top left no-repeat / contain; mask: url(https://cdn.pixabay.com/photo/2016/12/28/19/37/denied-1936877_960_720.png) top left no-repeat / contain; } } 
 <div></div> <h1>No stairway??</h1> 

That's assuming your mask images are alpha transparent PNGs. 假设您的遮罩图像是Alpha透明PNG。 you could also use luminance mask by setting mask-mode: luminance; 您还可以通过设置mask-mode: luminance;使用亮度遮罩mask-mode: luminance;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM