gpt4 book ai didi

javascript - 球跟随鼠标并延迟 Canvas

转载 作者:行者123 更新时间:2023-12-03 00:41:34 25 4
gpt4 key购买 nike

所以我在这里有点初学者,决定尝试学习一下使用 Canvas 。我遵循了一些教程并成功制作了一个“球”来跟随我的鼠标。它有效。然而,有一个延迟。当我移动鼠标时,“球”会跟随我的鼠标,但会有延迟。总是迟到一点点。所以我的问题是...这就是它应该的样子( Canvas 如何工作并且没有办法解决它)还是我做错了什么。

这是我的 JavaScript 代码:

let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function (event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;
function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

我的 HTML:

<body>
<canvas></canvas>
<script src="javascript.js"></script>
</body>

let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();
<canvas></canvas>

任何帮助将不胜感激。

附注按要求更新。这是我的整个代码。

最佳答案

您能做的最好的事情就是消除感知延迟,这种延迟只有在鼠标和移动物体同时可见时才会明显。延迟非常小,如果没有光标,人们根本不会注意到。

该示例只是在鼠标位于 Canvas 上方时隐藏鼠标。

请注意,只有在有其他东西来表示鼠标位置时才应隐藏光标。

let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.style.cursor = "none";

canvas.width = innerWidth-40;
canvas.height = innerHeight-40;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();
<canvas></canvas>

关于javascript - 球跟随鼠标并延迟 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53440579/

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