Javascript/pixi.js

[PIXI] Graphic ํด๋ž˜์Šค ํ™œ์šฉํ•˜์—ฌ ๋„ํ˜•๊ทธ๋ฆฌ๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 12. 26. 15:18
728x90

app.renderer vs app.view

app.renderer๋Š” ๋‚ด๋ถ€ ๋งค์ปค๋‹ˆ์ฆ˜์ด๋ผ๋ฉด,(์บ”๋ฒ„์Šค ์š”์†Œ ์กฐ์ •)

app.view๋Š” ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ์บ”๋ฒ„์Šค ์ž์ฒด

//๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ
app.renderer.backgroundColor = 0x23395D;
//css
app.renderer.view.style.position = 'absolute';

 

Graphic ํด๋ž˜์Šค

- ์‚ฌ๊ฐํ˜•

app.stage๋Š” ํฌํ† ์ƒต์—์„œ ๋ ˆ์ด์–ด๋ฅผ ์œ„๋กœ ๊ณ„์† ์Œ“์•„๊ฐ€๋Š” ๋А๋‚Œ์„ ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค.

const graphics = PIXI.Graphics;
const rect = new graphics();
rect.beginFill(0x00ff00)
	.drawRect(40, 40, 200, 250)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(rect);

- ์ž์œ ๋กœ์šด ํด๋ฆฌ

const poly = new graphics();
rect.beginFill(0x00ff00)
	.lineStyle(4, 0xFFEAA00, 1)
	.drawPolygon(
		[600, 50,
			800, 150,
			900, 300,
			400, 300]
	)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(poly);

- ์›

const circle = new graphics();
circle.beginFill(0x00ff00)
	.lineStyle(4, 0xFFEAA00, 1)
	.drawCircle(440, 400, 80)//x์ขŒํ‘œ y์ขŒํ‘œ ๋ฐ˜์ง€๋ฆ„
	.endFill()
app.stage.addChild(circle);

- ์„ 

const line = new graphics();
line.lineStyle(4, 0xFFEAA00, 1)
	.moveTo(1500, 100)
	.lineTo(1500, 800)
app.stage.addChild(line);

- ๊ฐ€์šด๋ฐ๊ฐ€ ๋šซ๋ฆฐ ์›

-๊ทธ๋ž˜ํ”ฝ ์—‘์ŠคํŠธ๋ผ ๋ชจ๋“ˆ ์„ค์น˜

npm  install @pixi/graphics-extras

<script src="./node_modules/@pixi/graphics-extras/dist/graphics-extras.min.js"></script>

const torus = new graphics();
rect.beginFill(0x00ff00)
	.drawTorus(440, 440, 200, 250)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(torus);

const torus = new graphics();
rect.beginFill(0x00ff00)
	.drawTorus(440, 440, 200, 250, 0, Math.PI / 2)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(torus);

 

- ๋ณ„

const star = new graphics();
star.beginFill(0x00ff00)
	.drawStar(860, 740, 30, 80)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(star);

 

์ „์ฒด ์ฝ”๋“œ

const Application = PIXI.Application;
const app = new Application({
	//๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
	width: window.innerWidth,
	height: window.innerHeight,
	transparent: false,
	antialias: true,
	backgroundColor: 0xAAAAAA
	//์ด๋ฏธ์ง€ ๋ฐ ๋ชจ์–‘์˜ ํ”ฝ์…€ํ™”๋œ ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งค๋„๋Ÿฝ๊ฒŒ ํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ์†์„ฑ
	//์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด์˜ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ ์ค‘ ์ผ๋ถ€๋Š” ์บ”๋ฒ„์Šค์˜ ๋„ˆ๋น„์™€ ๋†’์ด, ํˆฌ๋ช…๋„ ๋“ฑ์ด ์žˆ๋‹ค.
});
//๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ
app.renderer.backgroundColor = 0x23395D;
//์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ ์ด์™€ ๊ฐ™์ด ํฌ๊ธฐ ์กฐ์ • ๊ฐ€๋Šฅ
app.renderer.view.style.position = 'absolute';
document.body.appendChild(app.view);


const graphics = PIXI.Graphics;
const rect = new graphics();
rect.beginFill(0x00ff00)
	.lineStyle(4, 0xFFEAA00, 1)
	.drawRect(40, 40, 200, 250)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(rect);

const poly = new graphics();
rect.beginFill(0x00ff00)
	.lineStyle(4, 0xFFEAA00, 1)
	.drawPolygon(
		[600, 50,
			800, 150,
			900, 300,
			400, 300]
	)
	.endFill()
app.stage.addChild(poly);

const circle = new graphics();
circle.beginFill(0x00ff00)
	.lineStyle(4, 0xFFEAA00, 1)
	.drawCircle(440, 400, 80)//x์ขŒํ‘œ y์ขŒํ‘œ ๋ฐ˜์ง€๋ฆ„
	.endFill()
app.stage.addChild(circle);

const line = new graphics();
line.lineStyle(4, 0xFFEAA00, 1)
	.moveTo(1500, 100)
	.lineTo(1500, 800)
app.stage.addChild(line);

const torus = new graphics();
torus.beginFill(0x00ff00)
	.drawTorus(440, 440, 200, 250, 0, Math.PI / 2)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(torus);


const star = new graphics();
star.beginFill(0x00ff00)
	.drawStar(860, 740, 30, 80)//x์ขŒํ‘œ y์ขŒํ‘œ ๋„ˆ๋น„ ๋†’์ด
	.endFill()
app.stage.addChild(star);
728x90