Flex 布局
感谢 @Winter 同学的支持,现在SpriteJS能够使用Flex布局了。
在Group上可以设置属性display: flex
,开启flex布局。
;(async function () {
const scene = new Scene('#flex-basic', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
await scene.preload([
'https://p5.ssl.qhimg.com/t01f47a319aebf27174.png',
'https://s3.ssl.qhres.com/static/a6a7509c33a290a6.json',
]);
const layer = scene.layer(),
layout = new Group();
layout.attr({
display: 'flex',
alignItems: 'flex-start',
justifyContent: 'space-around',
flexWrap: 'wrap',
// size: [300, 500],
width: 1200,
pos: [770, 50],
anchor: [0.5, 0],
border: [6, '#aaa'],
});
layer.append(layout);
const s1 = new Sprite('guanguan1.png'),
s2 = new Sprite('guanguan2.png'),
s3 = new Sprite('guanguan3.png'),
s4 = new Sprite('guanguan3.png');
s4.attr({
anchor: 0.5,
scale: [-1, 1],
});
layout.append(s1, s2, s3, s4);
layout.animate([
{width: 1200},
{width: 400},
], {
duration: 3000,
direction: 'alternate',
iterations: Infinity,
});
}())
容器属性
以下5个属性设置在容器上:
- flexDirection
- flexWrap
- justifyContent
- alignItems
- alignContent
flexDirection
flexDirection属性决定主轴的方向(即项目的排列方向)。
它可能取4个值:
- row(默认值):主轴为水平方向,起点在左端。
- row-reverse:主轴为水平方向,起点在右端。
- column:主轴为垂直方向,起点在上沿。
- column-reverse:主轴为垂直方向,起点在下沿。
const scene = new Scene('#flex-direction', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Sprite();
s1.attr({
size: [50, 50],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [200, 300],
width: 200,
bgcolor: 'grey',
display: 'flex',
flexDirection: 'row',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
x: 500,
flexDirection: 'row-reverse',
});
g2.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
x: 800,
flexDirection: 'column',
width: '',
height: 200,
});
g3.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g3);
const g4 = g1.cloneNode();
g4.attr({
x: 1100,
flexDirection: 'column-reverse',
width: '',
height: 200,
});
g4.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g4);
flexWrap
默认情况下,项目都排在一条线(又称”轴线”)上。flexWrap属性定义,如果一条轴线排不下,如何换行。
它可能取3个值:
- nowrap(默认值):不换行
- wrap:换行,第一行在上方
- wrap-reverse:换行,第一行在下方
const scene = new Scene('#flex-wrap', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Sprite();
s1.attr({
size: [100, 100],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [300, 300],
width: 200,
bgcolor: 'grey',
display: 'flex',
flexDirection: 'row',
flexWrap: 'nowrap',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
x: 800,
flexWrap: 'wrap',
});
g2.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
x: 1300,
flexWrap: 'wrap-reverse',
});
g3.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g3);
justifyContent
justifyContent属性定义了项目在主轴上的对齐方式。
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右:
- flex-start(默认值):左对齐
- flex-end:右对齐
- center: 居中
- space-between:两端对齐,项目之间的间隔都相等。
- space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
const scene = new Scene('#flex-justify', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Sprite();
s1.attr({
size: [50, 50],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [770, 100],
width: 300,
bgcolor: 'grey',
display: 'flex',
justifyContent: 'flex-start',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
y: 200,
justifyContent: 'flex-end',
});
g2.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
y: 300,
justifyContent: 'center',
});
g3.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g3);
const g4 = g1.cloneNode();
g4.attr({
y: 400,
justifyContent: 'space-between',
});
g4.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g4);
const g5 = g1.cloneNode();
g5.attr({
y: 500,
justifyContent: 'space-around',
});
g5.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g5);
alignItems
alignItems属性定义项目在交叉轴上如何对齐。
它可能取4个值,具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。
- flex-start:交叉轴的起点对齐。
- flex-end:交叉轴的终点对齐。
- center:交叉轴的中点对齐。
- stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
const scene = new Scene('#flex-alignItems', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Sprite();
s1.attr({
size: [50, 100],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue', height: 200});
const s3 = new Label('中\n国');
s3.attr({
fillColor: 'cyan',
font: '2rem "宋体"',
bgcolor: 'green',
});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [200, 300],
width: 200,
bgcolor: 'grey',
display: 'flex',
justifyContent: 'space-around',
alignItems: 'flex-start',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
x: 550,
alignItems: 'flex-end',
});
g2.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
x: 900,
alignItems: 'center',
});
g3.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g3);
const g4 = g1.cloneNode();
g4.attr({
x: 1250,
alignItems: 'stretch',
});
g4.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g4);
alignContent
alignContent属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
该属性可能取6个值:
- flex-start:与交叉轴的起点对齐。
- flex-end:与交叉轴的终点对齐。
- center:与交叉轴的中点对齐。
- space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
- space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
- stretch(默认值):轴线占满整个交叉轴。
const scene = new Scene('#flex-alignContent', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Sprite();
s1.attr({
size: [100, 100],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [200, 300],
width: 200,
height: 300,
bgcolor: 'grey',
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
alignContent: 'stretch',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
x: 500,
alignContent: 'flex-start',
});
g2.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
x: 800,
alignContent: 'flex-end',
});
g3.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g3);
const g4 = g1.cloneNode();
g4.attr({
x: 1100,
alignContent: 'space-between',
});
g4.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g4);
const g5 = g1.cloneNode();
g5.attr({
x: 1400,
alignContent: 'space-around',
});
g5.append(...[s1, s2, s3].map(s => s.cloneNode()));
layer.append(g5);
元素属性
以下5个属性设置在元素上:
- order:决定元素的排序方式
- flexGrow:当元素被拉伸时,决定它被拉伸的比例
- flexShrink:当元素被压缩时,决定它被压缩的比例
- flexBasis:定义了在分配多余空间之前,项目占据的主轴空间(main size)。spritejs根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
- alignSelf:覆盖容器的alignItems属性通过设置order来排序:
const scene = new Scene('#flex-order', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Label('1');
s1.attr({
lineHeight: 100,
fillColor: 'white',
font: 'bold 2rem "宋体"',
textAlign: 'center',
size: [100, 100],
bgcolor: 'red',
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue', text: '2'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green', text: '3'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [300, 300],
width: 200,
bgcolor: 'grey',
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
x: 800,
});
g2.append(...[s1, s2, s3].map((s, i) => s.cloneNode().attr({order: 2 - i})));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
x: 1300,
});
g3.append(...[s1, s2, s3].map((s, i) => s.cloneNode().attr({order: Math.abs(1 - i)})));
layer.append(g3);
通过设置flex-grow来决定元素占据的空间:
const scene = new Scene('#flex-flex', {viewport: ['auto', 'auto'], resolution: [1540, 600]});
const layer = scene.layer();
const s1 = new Label('1');
s1.attr({
lineHeight: 100,
fillColor: 'white',
font: 'bold 2rem "宋体"',
textAlign: 'center',
size: [100, 100],
bgcolor: 'red',
flexGrow: 1,
});
const s2 = s1.cloneNode();
s2.attr({bgcolor: 'blue', text: '2'});
const s3 = s1.cloneNode();
s3.attr({bgcolor: 'green', text: '3'});
const g1 = new Group();
g1.attr({
anchor: 0.5,
pos: [770, 150],
width: 600,
bgcolor: 'grey',
display: 'flex',
flexDirection: 'row',
});
layer.append(g1);
g1.append(s1, s2, s3);
const g2 = g1.cloneNode();
g2.attr({
y: 300,
});
g2.append(...[s1, s2, s3].map((s, i) => s.cloneNode().attr({flexGrow: i})));
layer.append(g2);
const g3 = g1.cloneNode();
g3.attr({
y: 450,
});
g3.append(...[s1, s2, s3].map((s, i) => s.cloneNode().attr({flexGrow: Math.abs(1 - i)})));
layer.append(g3);