2 changed files with 65 additions and 1 deletions
@ -0,0 +1,62 @@ |
|||||
|
import { Text, Graphics, Container, TextStyle } from "pixi.js"; |
||||
|
|
||||
|
interface IOpts{ |
||||
|
text: string |
||||
|
} |
||||
|
|
||||
|
const defaultOpts:IOpts = { |
||||
|
text: "" |
||||
|
} |
||||
|
|
||||
|
export default function Button(opts: IOpts) { |
||||
|
let curOpts: IOpts = {} as IOpts |
||||
|
Object.keys(defaultOpts).forEach((key: keyof IOpts) => { |
||||
|
if(opts[key]!=undefined){ |
||||
|
curOpts[key] = opts[key] |
||||
|
}else{ |
||||
|
curOpts[key] = defaultOpts[key] |
||||
|
} |
||||
|
}); |
||||
|
console.log(curOpts); |
||||
|
|
||||
|
let textStyle = new TextStyle({fontFamily : 'Arial', fontSize: 50, fill : 0x000000, align : 'center'}) |
||||
|
let textStr = new Text(curOpts.text, textStyle); |
||||
|
let xPadding = 60; |
||||
|
let yPadding = 20; |
||||
|
|
||||
|
let button = new Container(); |
||||
|
let width = textStr.width + xPadding; |
||||
|
let height = textStr.height + yPadding; |
||||
|
|
||||
|
|
||||
|
textStr.x = xPadding / 2; |
||||
|
textStr.y = yPadding / 2; |
||||
|
|
||||
|
const circle = new Graphics(); |
||||
|
circle.name = "circle"; |
||||
|
circle.beginFill(0xff0000); |
||||
|
circle.drawRect(0, 0, width, height); |
||||
|
circle.endFill(); |
||||
|
circle.interactive = true; |
||||
|
circle.buttonMode = true; |
||||
|
|
||||
|
button.addChild(circle); |
||||
|
button.addChild(textStr); |
||||
|
|
||||
|
button.x = 100; |
||||
|
button.y = 50; |
||||
|
button.interactive = true |
||||
|
button.on("touchstart", ()=>{ |
||||
|
button.alpha = .8 |
||||
|
}) |
||||
|
button.on("touchend", ()=>{ |
||||
|
// 点击成功
|
||||
|
console.log("success"); |
||||
|
|
||||
|
button.alpha = 1 |
||||
|
}) |
||||
|
button.on("touchendoutside", ()=>{ |
||||
|
button.alpha = 1 |
||||
|
}) |
||||
|
return button; |
||||
|
} |
Loading…
Reference in new issue