gpt4 book ai didi

javascript - 我不知道如何在 JavaScript 中创建一个额外的轨道圈

转载 作者:行者123 更新时间:2023-12-02 17:55:15 25 4
gpt4 key购买 nike

我目前对 JavaScript 还很陌生,我真的需要一些帮助来理解某些东西是如何工作的。我在 fiddle 中发现了一段 JavaScript 代码,其中一个矩形在圆形轨道上移动。

现在我正在尝试用它制作类似太阳系的东西。多个圆圈以不同的速度围绕太阳移动。我设法将矩形更改为圆形,但我不知道如何创建其他矩形(具有不同的颜色、大小和速度)。如果有人碰巧知道答案那就太好了!

好的,这是代码。

<canvas id="canvas" width=500 height=500></canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
var dd = 3;
var angle = 0;
var cx = 200;
var cy = 275;
var radius = 80;

ctx.fillStyle = "orange";
ctx.strokeStyle = "lightgray";

function draw(x, y) {
ctx.clearRect(0, 0, w, h);
ctx.save();
ctx.beginPath();
ctx.beginPath();
ctx.arc(x - 8/2,y - 5,12 ,0 ,2*Math.PI);
ctx.fill();
ctx.stroke();
ctx.restore();
};

var fps = 60;

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / fps);
};
})();

function animate() {
setTimeout(function () {
requestAnimFrame(animate);

// increase the angle of rotation
angle += Math.acos(1-Math.pow(dd/radius,2)/2);

// calculate the new ball.x / ball.y
var newX = cx + radius * Math.cos(angle);
var newY = cy + radius * Math.sin(angle);
// draw
draw(newX, newY);

// draw the centerpoint
ctx.beginPath();
ctx.arc(cx, cy, radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.stroke();

}, 1000 / fps);
}
animate();
</script>

最佳答案

Here is a complete example of animating multiple circles on an HTML canvas :

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="A Whole Lotta' Circles!" name="title">
<title>A Whole Lotta' Circles!</title>

<style>
body {
margin: 0px;
padding: 0px;
}
#myCanvas {
border: 1px #CCC solid;
}
</style>


</head>

<body>
<div id="container">
<canvas id="myCanvas" width="500" height="500">

</canvas>
</div>

<script>

var mainCanvas = document.getElementById("myCanvas");
var mainContext = mainCanvas.getContext('2d');

var circles = new Array();

var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;


function Circle(radius, speed, width, xPos, yPos) {
this.radius = radius;
this.speed = speed;
this.width = width;
this.xPos = xPos;
this.yPos = yPos;
this.opacity = .05 + Math.random() * .5;

this.counter = 0;

var signHelper = Math.floor(Math.random() * 2);

if (signHelper == 1) {
this.sign = -1;
} else {
this.sign = 1;
}
}

Circle.prototype.update = function () {

this.counter += this.sign * this.speed;

mainContext.beginPath();

mainContext.arc(this.xPos + Math.cos(this.counter / 100) * this.radius,
this.yPos + Math.sin(this.counter / 100) * this.radius,
this.width,
0,
Math.PI * 2,
false);

mainContext.closePath();

mainContext.fillStyle = 'rgba(185, 211, 238,' + this.opacity + ')';
mainContext.fill();
};

function drawCircles() {
for (var i = 0; i < 100; i++) {
var randomX = Math.round(-200 + Math.random() * 700);
var randomY = Math.round(-200 + Math.random() * 700);
var speed = .2 + Math.random() * 3;
var size = 5 + Math.random() * 100;

var circle = new Circle(100, speed, size, randomX, randomY);
circles.push(circle);
}
draw();
}
drawCircles();

function draw() {
mainContext.clearRect(0, 0, 500, 500);

for (var i = 0; i < circles.length; i++) {
var myCircle = circles[i];
myCircle.update();
}
requestAnimationFrame(draw);
}
</script>
</body>

</html>

关于javascript - 我不知道如何在 JavaScript 中创建一个额外的轨道圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21007836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com