简体   繁体   English

在画布上制作一个球慢慢向鼠标移动

[英]Make a ball on a canvas slowly move towards the mouse

I am trying to make a ball move slowly towards my mouse. 我想让一个球慢慢向我的老鼠移动。

Im using paper.js which is a simple animation library. 我使用paper.js这是一个简单的动画库。 Using this i have a ball moving on screen. 使用这个我有一个球在屏幕上移动。 These are some of the properties of the ball: 这些是球的一些属性:

balls[0].vector.angle is its direction. 球[0] .vector.angle是它的方向。 0 = right, 90 = down, 180 = left etc and everything in between 0 =正确,90 =向下,180 =向左等等,其间的一切

balls[0].point.x is its x position and .y for y position. balls [0] .point.x是x的位置,y是y的位置。

balls[0].vector.length is its speed. 球[0] .vector.length是它的速度。

I have put in a mouse move event and i think ive got the angle between them below: 我已经进行了鼠标移动事件,我认为我们之间的角度是:

    canvas.addEventListener("mousemove", function(e){

    var a = balls[0].point.y - e.clientY;
    var b = balls[0].point.x - e.clientX;
    var angleDeg = Math.atan2(a, b) * 180 / Math.PI;
});

So i have made the ball stationary to test this and moved my mouse around it. 所以我让球静止不动以测试它并将鼠标移动到它周围。 To the left of the ball gives me 0 degrees. 球的左边给我0度。 Above gives me 90. To the right gives me 180. And below the ball gives me -90 etc and everything in between. 上面给了我90.右边给我180.球下面给了我-90等以及介于两者之间的一切。

I then calculated the distance in the same event and changed the speed to reflect the distance giving it a cap as max speed: 然后我计算了同一事件中的距离,并改变了速度以反映距离,给出了最大速度上限:

var distance = Math.sqrt( a*a + b*b );

var maxSpeed = 20; 
balls[0].vector.length = (distance/30 > maxSpeed) ? maxSpeed : distance/30;

So ive tested the speed and this and it works perfect. 所以我测试了速度和它,它的工作完美。 When i give the ball the angle from earlier its going in all sorts of directions. 当我向球提供从各个方向前进的角度。 The speed still works, its just the ball is going in the wrong direction and im not sure what ive done wrong. 速度仍然有效,它只是球走向错误的方向,我不知道我做错了什么。

Frankly, you don't need trig functions. 坦率地说,你不需要触发功能。 All you need is good old Pythagoras theorem. 你所需要的只是古老的毕达哥拉斯定理。

var MAX_SPEED = 20;
var MIN_SPEED = 0.25; // Very slow if close but not frozen.
var ATTRACTION = 0.5; 
var diff_y = e.clientY - balls[0].point.y;
var diff_x = e.clientX - balls[0].point.x;
var distance = Math.sqrt(diff_x * diff_x + diff_y * diff_y)
var speed = distance * ATTRACTION;
if (speed > MAX_SPEED) speed = MAX_SPEED;
if (speed < MIN_SPEED) speed = MIN_SPEED;
// The rates along axes are proportional to speed;
// we use ratios instead of sine / cosine.
balls[0].point.x += (diff_x / distance) * speed;
balls[0].point.y += (diff_y / distance) * speed;

Much more fun can be had by introducing forces and inertia. 通过引入力和惯性可以获得更多乐趣。

Specify the direction in terms of deltas 根据增量指定方向

var deltaX = e.clientX - balls[0].point.x;
var deltaY = e.clientY - balls[0].point.y;
var distance = Math.sqrt(deltaX*deltaX+deltaY*deltaY);
var maxSpeed = 20; 
balls[0].vector.length = (distance/30 > maxSpeed ) ? maxSpeed  : distance / 30;
balls[0].point.x = balls[0].point.x + (balls[0].vector.length * deltaX / distance);
balls[0].point.y = balls[0].point.y + (balls[0].vector.length * deltaY / distance);

I think that will work 我认为这会奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM