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

CKTIS / CSS

polygon() — polígono de pares de coordenadas

`polygon()` une pares comprimento-percentagem numa forma básica fechada relativa à caixa de referência.

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

Sintaxe

polygon( <'fill-rule'>? [ round <length> ]?, [ <length-percentage> <length-percentage> ]# )

polygon() une pares comprimento-percentagem numa forma básica fechada relativa à caixa de referência.

polygon(0 0, 100% 0, 0 100%) numa caixa 100×60 dá a amostra interior/exterior 255/0; alargada para 200×60 continua a dar 255/0, mostrando a escala percentual. polygon(round 10px, 0 0, 100% 0, 0 100%) arredondado indica suporte true.

Gramática: polygon( <'fill-rule'>? [ round <length> ]?, [ <length-percentage> <length-percentage> ]# ).

A primeira coordenada percentual resolve-se pela largura e a segunda pela altura; o UA liga sempre o último vértice ao primeiro. A regra predefinida é nonzero; evenodd altera o interior de figuras auto-intersetadas e round arredonda vértices. A função serve clip-path e shape-outside. Interpolar exige o mesmo número de vértices e a mesma regra.

Exemplos

const render=async(value,width,height,points)=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="'+width+'" height="'+height+'"><foreignObject width="'+width+'" height="'+height+'"><div xmlns="http://www.w3.org/1999/xhtml" style="width:'+width+'px;height:'+height+'px;background:blue;clip-path:'+value+'"></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=height;const context=canvas.getContext('2d');context.drawImage(image,0,0);return points.map(([x,y])=>context.getImageData(x,y,1,1).data[3])};
const value='polygon(0 0, 100% 0, 0 100%)';
const small=await render(value,100,60,[[10,10],[80,40]]);
const wide=await render(value,200,60,[[100,10],[180,40]]);
const rounded=CSS.supports('clip-path','polygon(round 10px, 0 0, 100% 0, 0 100%)');
console.log('small-in/out='+small.join('/')+'; wide-in/out='+wide.join('/')+'; rounded-supported='+rounded);

Resultado

small-in/out=255/0; wide-in/out=255/0; rounded-supported=true

`polygon()` une pares comprimento-percentagem numa forma básica fechada relativa à caixa de referência.

Fontes