Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / CSS

Właściwość CSS `mask-clip`

`mask-clip` ogranicza obszar malowania każdej maski obrazowej do wskazanego pola geometrii albo znosi ograniczenie przez `no-clip`.

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

Składnia

[ <coord-box> | no-clip ]#

mask-clip ogranicza obszar malowania każdej maski obrazowej do wskazanego pola geometrii albo znosi ograniczenie przez no-clip.

Przykład nakłada jednolitą maskę na dwa prostokąty SVG z obrysem: fill-box daje alfę 0 w części samego obrysu i 255 we wnętrzu, natomiast stroke-box zachowuje także punkt obrysu z alfą 255.

Składnia: [ <coord-box> | no-clip ]#.

Dla pudełek CSS dostępne są m.in. content-box, padding-box i border-box; dla SVG fill-box, stroke-box i view-box odwołują się do odpowiednich granic. mask-clip nie steruje odwołaniem do elementu SVG <mask> — jego obszar określają atrybuty maski. Wartość jest listą warstw, domyślnie border-box, a no-clip nie przycina malowania maski.

Przykłady

<!doctype html><meta charset="utf-8"><svg id="stage" xmlns="http://www.w3.org/2000/svg" width="110" height="50"><rect id="fill" x="10" y="10" width="30" height="30" fill="blue" stroke="blue" stroke-width="10" style="mask-image:linear-gradient(black,black);mask-clip:fill-box"/><rect id="stroke" x="70" y="10" width="30" height="30" fill="blue" stroke="blue" stroke-width="10" style="mask-image:linear-gradient(black,black);mask-clip:stroke-box"/></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),fillStroke=alpha(context,6,25),fillCenter=alpha(context,20,25),strokeStroke=alpha(context,66,25),a=getComputedStyle(fill).maskClip,b=getComputedStyle(stroke).maskClip;if(fillStroke>5||fillCenter<250||strokeStroke<250||a!=='fill-box'||b!=='stroke-box')throw new Error('mask-clip geometry mismatch');result.textContent='fill-stroke='+fillStroke+'; fill-center='+fillCenter+'; stroke-stroke='+strokeStroke+'; computed='+a+'/'+b})().catch(error=>result.textContent='ERROR:'+error.message);
</script>

Wynik

fill-stroke=0; fill-center=255; stroke-stroke=255; computed=fill-box/stroke-box

Przykład uruchomiono lokalnie w izolowanej, bezsieciowej sesji Chrome; wynik pochodzi z API CSS przeglądarki.

Źródła