Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / CSS

shape() — trajeto adaptável com comandos CSS

`shape()` descreve uma forma básica com comandos `CSS`, mantendo unidades, cálculos e variáveis indisponíveis numa cadeia de `path()`.

Versão: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Sintaxe

shape( <'fill-rule'>? from <coordinate-pair>, <shape-command># )

shape() descreve uma forma básica com comandos CSS, mantendo unidades, cálculos e variáveis indisponíveis numa cadeia de path().

shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close) com --cut: 20% produz alfa interior/exterior 255/0 a 100 px e 255/0 a 200 px de largura; o suporte é true. A escala prova que percentagem e variável se resolvem contra a caixa de referência.

Gramática: shape( <'fill-rule'>? from <coordinate-pair>, <shape-command># ).

from fixa o início; to fornece um ponto da caixa e by um deslocamento relativo ao segmento. Há move, line, hline, vline, curve, smooth, arc e close. A percentagem x usa a largura, y a altura. A regra predefinida é nonzero, alternativa evenodd; var() e calc() funcionam, mas as listas têm de coincidir para interpolar.

Exemplos

const render=async(width,cut,points)=>{const value='shape(from 0 0, line to 100% 0, line to '+cut+' 100%, close)';const svg='<svg xmlns="http://www.w3.org/2000/svg" width="'+width+'" height="100"><foreignObject width="'+width+'" height="100"><div xmlns="http://www.w3.org/1999/xhtml" style="--cut:'+cut+';width:'+width+'px;height:100px;background:green;clip-path:shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close)"></div></foreignObject></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=width;canvas.height=100;const context=canvas.getContext('2d');context.drawImage(image,0,0);return points.map(([x,y])=>context.getImageData(x,y,1,1).data[3])};
const first=await render(100,'20%',[[20,80],[80,80]]);const second=await render(200,'20%',[[40,80],[160,80]]);
const value='shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close)';
console.log('100-in/out='+first.join('/')+'; 200-in/out='+second.join('/')+'; supported='+CSS.supports('clip-path',value));

Resultado

100-in/out=255/0; 200-in/out=255/0; supported=true

`shape()` descreve uma forma básica com comandos `CSS`, mantendo unidades, cálculos e variáveis indisponíveis numa cadeia de `path()`.

Fontes