@amoy/components
使用该插件,可以快速进行元素布局。
Tips:
- 在 Sprite 中设置 border-radius 必然会有 overflow 的效果;
- backgroundFrame 无法修改 texture 宽高,只能更新定位;
- text标签内容不能为空;
使用姿势:
- 模板语法糖形式:
import { render, style } from '@amoy/components'
const game = new PIXI.Application()
game.loader.add('rect', rect).load((loader, resources) => {
style({
'.container': {
width: '100%',
height: '100%',
centerX: 0,
centerY: 0,
},
})
render(`
<container class="container">
<sprite src="rect" style="width: 200; height: 200; left: 50; top: 50;">
</container>
`, game.stage)
})
- 使用元素API直接创建:
import { createComponent, Sprite, View, Rect, Text, Circle } from '@amoy/components'
import rect from './images/rect.jpg'
const game = new PIXI.Application()
const parent = View({
width: 600,
height: 600,
centerX: 0,
centerY: 0,
borderWidth: 5,
borderColor: 'black',
})
parent.appendTo(game.stage)
game.loader.add('rect', rect).load((loader, resources) => {
const sprite = Sprite(resources.rect.texture, {
width: 200,
height: 200,
left: 50,
top: 50,
})
sprite.appendTo(parent)
})
const text = Text('我是一排文字~我会根据宽度自动换行~~', {
width: 200,
height: 200,
right: 50,
bottom: 50,
fontSize: 30,
color: '#a72461',
lineHeight: 60,
})
text.appendTo(parent)
const rect = Rect({
backgroundColor: '#f9e090',
borderWidth: 5,
borderColor: '#dc5353',
width: 200,
height: 200,
right: 50,
top: 50,
})
rect.appendTo(parent)
API
configComponents
全局配置 Components
configComponents({
uiDesignWidth: 1000,
debug: boolean,
})
创建元素
创建精灵
Sprite(image: PIXI.Texture | HTMLCanvasElement | HTMLVideoElement | HTMLImageElement, Style)
创建帧动画精灵
AnimatedSprite(animatedFrames: PIXI.Texture[], style)
创建容器
View(style)
创建矩形
Rect(style)
创建圆形
Circle(style)
创建文字
Text(content: string, style)
style
布局参数
interface style {
width?: string | number,
height?: string | number,
left?: string | number,
right?: string | number,
top?: string | number,
bottom?: string | number,
centerX?: string | number,
centerY?: string | number,
rotation?: number | string,
scale?: number
opacity?: number,
backgroundColor?: number | string,
backgroundFrame?: number[],
borderWidth?: number | string,
borderColor?: number | string,
borderRadius?: number | string,
overflow?: 'hidden',
anchor?: { x: number, y: number },
zIndex?: number
color?: string,
textAlign?: 'left' | 'center' | 'right'
textJustify?: 'top' | 'center' | 'bottom'
fontSize?: number | string,
fontWeight?: 'normal'| 'bold'| 'bolder'| 'lighter' | '100' | '200' ...
fontStyle?: 'normal' | 'italic' | 'oblique'
fontFamily?: string | string[]
lineHeight?: number,
animatedFrames?: PIXI.Texture[]
animatedLoop?: boolean
animatedSpeed?: number
}
模板API
render(`
<sprite src="data.texture" />
`, game.stage, {
texture: texture,
})
style(css)
style({
'.class': `width: 300; height: 300;`,
'#id': {
width: 300,
height: 300,
},
tag: `font-size: 30`
})
元素操作
更新布局
el.setStyle({
width: 500,
height: 500,
})
el.layout.setScale(scale: number)
添加与删除
child.appendTo(parent)
parent.append(child1)
child1.before(child2)
child1.after(child2)
child2.insertBefore(child1)
child2.insertAfter(child1)
child2.remove()
parent.removeChild(child2)
获取定位
el.getRelativeBounds()
el.getGlobalBounds()
工具函数
- el.transformStyle(style): 用于将 style 类型转换成便于外部使用的定位参数;
- return { x: number, y: number, width: number, height: number, rotation: number }
标签专属API
- AnimatedSprite.replay()
- 当 AnimatedSprite.loop = false 时,该方法能使动画重新播放一遍;