Here's an example of an HTML file with an inline JavaScript using p5.js that animates a planet orbiting two stars in a figure 8 pattern. In this example, the planet's distance from the center of the stars is controlled by the r and r2 variables, which ensure that the planet stays a safe distance away.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Figure 8 Orbit</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
</head>
<body>
<div id="sketch-holder"></div>
<script>
function setup() {
createCanvas(400, 400);
noStroke();
ellipseMode(CENTER);
star1 = new Star(200, 200, 20, 40);
star2 = new Star(200, 200, 20, 40);
planet = new Planet(250);
}
function draw() {
background(220);
star1.display();
star2.display();
planet.update();
planet.display();
}
class Star {
constructor(x, y, r1, r2) {
this.x = x;
this.y = y;
this.r1 = r1;
this.r2 = r2;
}
display() {
ellipse(this.x, this.y, this.r1 * 2, this.r1 * 2);
ellipse(this.x, this.y, this.r2 * 2, this.r2 * 2);
}
}
class Planet {
constructor(d) {
this.x = 0;
this.y = 0;
this.d = d;
this.r = 10;
this.r2 = 60;
this.theta = 0;
}
update() {
this.theta += 0.02;
this.x = (this.r * sin(this.theta)) + (this.r2 * sin(this.theta * 0.5));
this.y = (this.r * cos(this.theta)) - (this.r2 * cos(this.theta * 0.5));
}
display() {
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
}
</script>
</body>
</html>
In this example, the r and r2 variables control the distance of the planet from the center of the stars. The r variable is used to control the main orbit around the stars, while r2 is used to create the figure 8 pattern. Adjust these values to suit your preferences.
You can copy and paste the above code into an HTML file, or use an HTML editor like Adobe Dreamweaver or Visual Studio Code to create the file and insert the code. Once you've created the HTML file, open it in a web browser to see the animation.
Please note that this is a basic example, and you can customize the appearance and behavior of the stars and planet by modifying the Star and Planet classes. This code uses the p5.js library version 1.4.0. You can update the version by modifying the script tag's src attribute or by including a newer version from the official p5.js website.
번역결과
다음은 p5.js를 사용하여 행성이 두 개의 별 주위를 8자 형태로 돌고 있는 애니메이션을 하는 인라인 JavaScript를 사용하는 HTML 파일의 예입니다.