sin
๋ ์ฃผ๋ก ํ๋์ ๋์ด ํํ,cos
์ ํ๋์ ์์์ ์ ํํํ๋ ๋ฐ์ ์ฃผ๋ก ์ฌ์ฉ๋จ.
๊ฐ์ฅ ์ค์ํ ๊ฑด, ๊ทธ๋ฆฌ๊ณ ์ ํ๋ ์ ๋๋ฉ์ด์ ์ขํ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ! ์ฆ, stageWidth, stageHeight ๊ต์ฅํ ์ค์
์ข
๋ฏผ๋ ์จ์ด๋ธ ํด๋ก !
์ด๊ฑด ๋๋๋๋ ๊ณต๋ถํด์ผ๊ฒ ๋ค
์ด๋ ต๋ค! ๊ทผ๋ฐ ์ฌ๋ฐ๋ค
์ฝ๋๋ ๋ ๋ด์ผ๊ฒ ๋ค
export class Point {
constructor(x, y) {
/* ์จ์ด๋ธ๋ฅผ ๊ทธ๋ฆฌ๋๋ฐ ์ด์ฉ๋๋ ์ ๋ค์
์๋ ์๋ก ๋๋คํ๊ฒ offset ๊ฐ์ ๊ฐ์ง
*/
this.x = x;
this.y = y;
this.fixedY = y; // ๊ธฐ๋ณธ Y ์ค์ฌ
this.speed = 0.1;
this.cur = 0;
this.max = Math.random() * 100 + 150;
}
update() {
this.cur += this.speed;
this.y = this.fixedY + Math.sin(this.cur) * this.max;
}
}
this.max = Math.random() * 100 + 150;
this.y = this.fixedY + Math.sin(this.cur) * this.max;
๋จผ์ ,
totalPoints
๋ฅผ ๋๊ฒจ์ค์ ๋ช๊ฐ์ ํฌ์ธํธ๋ฅผ ์์ฑํ ๊ฒ์ธ์ง ๊ฐ๊ฐ Wave ๋ง๋ค ์ ์ํด์ฃผ๊ธฐ
๊ฐ๊ฒฉ = ์ด stageWidth
์์ totalPoints
๋ฅผ ๋๋๊ฐ
this.pointGap = this.stageWidth / (this.totalPoints - 1);
๊ฐ๊ฒฉ๋งํผ, ๊ฐ๊ฒฉ์ ๋ง์ถฐ์ point๋ฅผ ํ๋ฉด์ ๊ทธ๋ ค์ฃผ๊ธฐ
// init() ํจ์๋ฅผ ์คํ์์ผ Point ์์ฑ
init() {
// ์์ฑ๋ point์๋ ์ resize ํจ์์์ ์ ์ํ ํ๋ฉด์ค์์ ๋ํ๋๋๋ก
this.point = new Point(this.centerX, this.centerY);
}
init() {
this.points = [];
for(let i = 0; i < this.totalPoints; i++){
const point = new Point(
this.index + i,
this.pointGap * i,
this.centerY,
);
this.points[i] = point;
}
}
์ ๋ฐ์ดํธ๋ point๊ฐ์์ ๋ง์ถ์ด drawํจ์๋ ์ ๋ฐ์ดํธ
// ์๊น ์์๋ก ๋นจ๊ฐ ๊ณต
draw(ctx) {
ctx.beginPath();
ctx.fillStyle = '#ff0000';
this.point.update();
ctx.arc(this.point.x, this.point.y, 30, 0, Math.PI * 2);
ctx.fill();
}
draw(ctx) {
ctx.beginPath();
ctx.fillStyle = this.color; // ํ์ฌ wave color๋ก
let prevX = this.points[0].x;
let prevY = this.points[0].y;
ctx.moveTo(prevX, prevY);
for (let i = 0; i < this.totalPoints; i++) {
if (i < this.totalPoints - 1) {
this.points[i].update();
}
const cx = (prevX + this.points[i].x) / 2;
const cy = (prevY + this.points[i].y) / 2;
ctx.lineTo(cx, cy);
prevX = this.points[i].x;
prevY = this.points[i].y;
}
ctx.lineTo(prevX, prevY); // ์ค๊ฐ๊ฐ
ctx.lineTo(this.stageWidth, this.stageHeight);
ctx.lineTo(this.points[0].x, this.stageHeight);
ctx.fill();
ctx.closePath();
}
ctx.lineTo(cx, cy);
ctx.quadraticCurveTo(prevX, prevY, cx, cy);