Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS property `mask-composite`

`mask-composite` selects the Porter–Duff operator that combines the current mask layer with the already-composited result of the layers below it.

Version: CSS modules; captured CSSWG editor index includes draft and obsolete features requiring review

Syntax

<compositing-operator>#

mask-composite selects the Porter–Duff operator that combines the current mask layer with the already-composited result of the layers below it.

The example uses two perpendicular half masks with subtract: when the left half is the upper layer, only bottom-left remains (255/0 for bottom-left/top-right); reversing the image order leaves top-right instead (0/255).

Grammar: <compositing-operator>#.

The operators are add (source-over), subtract (source-out), intersect (source-in), and exclude (XOR). The first listed image is closest to the viewer and is the source against the composed layers below; the bottom layer’s operator is ignored. The list repeats to the image count and excess values are unused; the initial value is add.

Examples

<!doctype html><meta charset="utf-8"><svg id="stage" xmlns="http://www.w3.org/2000/svg" width="100" height="40"><rect id="first" width="40" height="40" fill="green" style="mask-image:linear-gradient(to right,black 0 50%,transparent 50%),linear-gradient(to bottom,black 0 50%,transparent 50%);mask-composite:subtract"/><rect id="reversed" x="60" width="40" height="40" fill="green" style="mask-image:linear-gradient(to bottom,black 0 50%,transparent 50%),linear-gradient(to right,black 0 50%,transparent 50%);mask-composite:subtract"/></svg><pre id="result"></pre><script>

const rasterize=svg=>new Promise((resolve,reject)=>{const image=new Image();image.onload=()=>{const canvas=document.createElement('canvas');canvas.width=Number(svg.getAttribute('width'));canvas.height=Number(svg.getAttribute('height'));const context=canvas.getContext('2d');context.drawImage(image,0,0);resolve(context)};image.onerror=()=>reject(new Error('SVG rasterization failed'));image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(new XMLSerializer().serializeToString(svg))});
const alpha=(context,x,y)=>context.getImageData(x,y,1,1).data[3];
(async()=>{const context=await rasterize(stage),aBL=alpha(context,10,30),aTR=alpha(context,30,10),bBL=alpha(context,70,30),bTR=alpha(context,90,10),computed=getComputedStyle(first).maskComposite;if(aBL<250||aTR>5||bBL>5||bTR<250||computed!=='subtract, subtract')throw new Error('mask-composite order mismatch '+[aBL,aTR,bBL,bTR,computed]);result.textContent='first-BL/TR='+aBL+'/'+aTR+'; reversed-BL/TR='+bBL+'/'+bTR+'; computed='+computed})().catch(error=>result.textContent='ERROR:'+error.message);
</script>

Result

first-BL/TR=255/0; reversed-BL/TR=0/255; computed=subtract, subtract

The example was executed locally in an isolated, network-free Chrome session; the result comes from the browser CSS API.

Sources