gpt4 book ai didi

javascript - 在保持均匀速度的同时通过 Canvas 上的一系列点为对象设置动画?

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:29 25 4
gpt4 key购买 nike

(我是 Javascript/HTML/CSS 编码的新手,请原谅我糟糕的风格!)

我想以静态速度沿着数组中的一组坐标为数组中的对象设置动画:

数组示例:

_myPtArr = [{x:297, y:30},{x:299,y:47},{x:350,y:56},{x:305,y:176},{x:278,y:169},
{x:303,y:108},{x:269,y:79},{x:182,y:90},{x:137,y:81},{x:173,y:33},{x:231,y:38}];

我为此所做的是;创建一个在程序开头运行的函数,以接收任何数组并将距离属性(名为“dis”)添加到它计算距离的两个点中的第一个,看起来像这样:

function findDist(array) {
for (var i = 0; i<array.length-1;i++){
var p = array[i],
q = array[i+1],
dx = p.x - q.x,
dy = p.y - q.y,
dist = Math.sqrt(dx*dx + dy*dy);
array[i].dis=dist;
}
}

我这样做是因为我为它们制作动画的方式是创建一个名为“_tick”的变量,它会通过从 0 到 1 递增将它们从一个点移动到另一个点,0 是起点,1 是终点。我打算将刻度乘以距离,以便不同的线长度以相同的速度进行动画处理。但我还没有得到这样的工作!我卡住了!这是执行我刚才所说的功能的功能:

function calculateInnerPts(pts, pos){
var ptArr = [];
console.log(pts.length);
for(var i = 0; i < pts.length-1; i++){
ptArr[i] = {x: pts[i].x + (pts[i+1].x - pts[i].x) * pos, y: pts[i].y +
(pts[i+1].y - pts[i].y) * pos};
}
return ptArr;
}

在这样的系列中被调用:

在 enterFrameHandler(持续迭代)中,“部分”只是递增以获取每组 2 个点/对象,因此在 _tick 变为“1”之后,部分++。 :

ballAnim([_myPtArr[_section],_myPtArr[_section+1]],_tick);

然后在外面:

function ballAnim(pts,pos){
var iPts = pts;

for(var i = 0; i < pts.length-1; i++){
iPts = calculateInnerPts(iPts, pos);
}

drawBall(iPts[0]);
}

最后,我画球:

function drawBall(pts){
var w = 6,
h = 2;
ctx.clearRect(0,0,400,300);
ctx.beginPath();
ctx.arc(pts.x, pts.y, 5, 0, Math.PI * 2);
ctx.fillStyle="blue";
ctx.fill();
ctx.strokeStyle="black";
ctx.closePath();
ctx.stroke();
}

任何人都可以阐明我如何在给定这个系统的情况下实现这一目标,或者甚至是实现相同效果的另一种更好的方法吗?我想放入任何数组并获得相同的结果(意味着球以静态速度运动)!

注意:除了普通的 Javascript/HTML/CSS 之外,我不想使用任何东西;没有 API!

非常感谢大家,这个社区太棒了,这是我的第一篇文章,所以请放轻松! XD

最佳答案

我确信上面的答案更优雅,但我整个上午都在处理这段代码,我想分享一下......

Demo of code here

//requestAnimFrame shim via Paul Irish
window.requestAnimFrame = (function(callback){
return (window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback){window.setTimeout(callback, 1000/60);});
})();

//main code
window.onload = function(){
//setup canvas
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');

//variables
var canvasWidth = 400, canvasHeight = 400; //as set up in the <canvas> tag
var animationTime = 10000; //animation length in milliseconds

//clear canvas function
var clearCanvas = function(){
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
};

//define the line; build function for displaying it
var _myPtArr = [
{x:297, y:30}, {x:299,y:47}, {x:350,y:56}, {x:305,y:176},
{x:278,y:169}, {x:303,y:108}, {x:269,y:79}, {x:182,y:90},
{x:137,y:81}, {x:173,y:33}, {x:231,y:38}
];
var lineColor = 'red';
var drawLine = function(){
ctx.beginPath();
ctx.moveTo(_myPtArr[0].x, _myPtArr[0].y);
for(var i=1, z=_myPtArr.length; i<z; i++){
ctx.lineTo(_myPtArr[i].x, _myPtArr[i].y);
}
ctx.strokeStyle = lineColor;
ctx.stroke();
};

//define the ball; build function for displaying it
var myBall = {x: 0, y: 0, radius: 5, fillStyle: 'blue', strokeStyle: 'black'};
var drawBall = function(){
ctx.beginPath();
ctx.arc(myBall.x, myBall.y, myBall.radius, 0, (Math.PI*2));
ctx.closePath();
ctx.fillStyle = myBall.fillStyle;
ctx.strokeStyle = myBall.strokeStyle;
ctx.fill();
ctx.stroke();
};

//build an array of objects for each line to help ball position calculations
var myBallVectors = [];
var fullDistance = 0, p, q, dx, dy, tempDistance, tempTime, fullTime = 0;
for(var i=1, z=_myPtArr.length; i<z; i++){
p = _myPtArr[i-1], q = _myPtArr[i];
dx = q.x - p.x;
dy = q.y - p.y;
tempDistance = Math.sqrt((dx*dx)+(dy*dy));
fullDistance += tempDistance;
myBallVectors.push({x: p.x, y: p.y, dX: dx, dY: dy, magnitude: tempDistance});
}
for(var i=0, z=myBallVectors.length; i<z; i++){
myBallVectors[i]['start'] = fullTime * animationTime;
tempTime = myBallVectors[i].magnitude/fullDistance;
fullTime += tempTime
myBallVectors[i]['duration'] = tempTime * animationTime;
myBallVectors[i]['finish'] = fullTime * animationTime;
}

//function to work out where ball is in space and time
var myBallPosition = function(){
if(Date.now() > breakTime){
//move on to next line
currentLine++;
//check to see if animation needs to finish
if(currentLine >= myBallVectors.length){
doAnimation = false;
}
//otherwise, set new break point
else{
breakTime = startTime + parseInt(myBallVectors[currentLine].finish);
}
}
if(doAnimation){
//calculate ball's current position
var timePassed = Date.now() - (startTime + myBallVectors[currentLine].start)
var percentageLineDone = timePassed/myBallVectors[currentLine].duration;
myBall.x = myBallVectors[currentLine].x + (myBallVectors[currentLine].dX * percentageLineDone);
myBall.y = myBallVectors[currentLine].y + (myBallVectors[currentLine].dY * percentageLineDone);
}
};

//setup and display initial scene
drawLine();
myBall.x = _myPtArr[0].x;
myBall.y = _myPtArr[0].y;
drawBall();

//animation loop function
animate = function(){
clearCanvas();
drawLine();
myBallPosition();
drawBall();
if(doAnimation){
requestAnimFrame(function(){
animate();
});
}
};

//setup control variables, and start animation
var startTime = Date.now(), currentLine = 0;
var breakTime = startTime + parseInt(myBallVectors[currentLine].finish);
var doAnimation = true;
animate();

};

关于javascript - 在保持均匀速度的同时通过 Canvas 上的一系列点为对象设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18404310/

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