CKTIS / CSS
Właściwość CSS `mask-image`
`mask-image` wskazuje dla każdej warstwy brak obrazu, obraz CSS taki jak gradient albo URL obrazu bądź elementu SVG `<mask>`.
Wersja: CSS modules; captured CSSWG editor index includes draft and obsolete features requiring review
Składnia
<mask-reference>#mask-image wskazuje dla każdej warstwy brak obrazu, obraz CSS taki jak gradient albo URL obrazu bądź elementu SVG <mask>.
Przykład rasteryzowany w Chromium porównuje cztery źródła: pojedyncze none pozostawia próbkę z alfą 255, gradient daje 255/0 po obu stronach granicy, poprawny URL do półmaski SVG także daje 255/0, a przezroczysty obraz pod URL-em daje 0.
Składnia: <mask-reference>#.
Wartość początkowa to none. W wielowarstwowej liście none nadal zajmuje warstwę przezroczystej czerni i może zmienić wynik składania. Pusty obraz, błąd pobierania, nieistniejąca lub nieobsługiwana referencja również stają się przezroczystą czernią zamiast odsłonić element. Inne listy mask-* są dopasowywane do liczby pozycji mask-image; wartość inna niż none tworzy kontekst stosu.
Przykłady
<!doctype html><meta charset="utf-8"><svg id="stage" xmlns="http://www.w3.org/2000/svg" width="150" height="20"><defs><mask id="half" maskUnits="userSpaceOnUse" x="80" y="0" width="30" height="20" mask-type="alpha"><rect x="80" width="15" height="20" fill="white"/></mask></defs><rect id="plain" width="30" height="20" fill="purple" style="mask-image:none"/><rect id="gradient" x="40" width="30" height="20" fill="purple" style="mask-image:linear-gradient(to right,black 0 50%,transparent 50%)"/><rect id="linked" x="80" width="30" height="20" fill="purple" style="mask-image:url(#half);mask-mode:alpha"/><rect id="missing" x="120" width="30" height="20" fill="purple" style="mask-image:url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221%22%20height%3D%221%22%2F%3E)"/></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),none=alpha(context,15,10),gL=alpha(context,45,10),gR=alpha(context,65,10),uL=alpha(context,85,10),uR=alpha(context,105,10),transparent=alpha(context,135,10),computed=getComputedStyle(gradient).maskImage;if(none<250||gL<250||gR>5||uL<250||uR>5||transparent>5||!computed.startsWith('linear-gradient'))throw new Error('mask-image source boundary mismatch '+[none,gL,gR,uL,uR,transparent,computed]);result.textContent='none='+none+'; gradient='+gL+'/'+gR+'; url='+uL+'/'+uR+'; transparent-url='+transparent+'; computed-gradient=true'})().catch(error=>result.textContent='ERROR:'+error.message);
</script>Wynik
none=255; gradient=255/0; url=255/0; transparent-url=0; computed-gradient=true
Przykład uruchomiono lokalnie w izolowanej, bezsieciowej sesji Chrome; wynik pochodzi z API CSS przeglądarki.