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

CKTIS / CSS

xywh() — retângulo por posição, largura e altura

`xywh()` cria uma `<basic-shape>` a partir do deslocamento superior esquerdo e tamanho explícito, por exemplo para `clip-path`.

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

Sintaxe

xywh( <length-percentage>{2} <length-percentage [0,∞]>{2} [ round <'border-radius'> ]? )

xywh() cria uma <basic-shape> a partir do deslocamento superior esquerdo e tamanho explícito, por exemplo para clip-path.

xywh(20% 10px 50% 60px round 10px) a 100 px dá alfa interior/exterior/canto 255/0/0, e a 200 px 255/0/0. A largura negativa xywh(10px 10px -1px 20px) tem suporte false.

Gramática: xywh( <length-percentage>{2} <length-percentage [0,∞]>{2} [ round <'border-radius'> ]? ).

Os quatro componentes são x, y, width e height; percentagens horizontais usam a largura da reference box, verticais a altura. Os tamanhos não podem ser negativos, embora a posição possa sair. round aceita toda a sintaxe border-radius, reduzindo proporcionalmente raios excessivos. A forma também serve shape-outside.

Exemplos

const render=async(width,points)=>{const value='xywh(20% 10px 50% 60px round 10px)';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="width:'+width+'px;height:100px;background:green;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=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 small=await render(100,[[30,30],[75,40],[20,10]]);const wide=await render(200,[[60,30],[150,40],[40,10]]);
const negative=CSS.supports('clip-path','xywh(10px 10px -1px 20px)');
console.log('100-in/out/corner='+small.join('/')+'; 200-in/out/corner='+wide.join('/')+'; negative-width='+negative);

Resultado

100-in/out/corner=255/0/0; 200-in/out/corner=255/0/0; negative-width=false

`xywh()` cria uma `<basic-shape>` a partir do deslocamento superior esquerdo e tamanho explícito, por exemplo para `clip-path`.

Fontes