简体   繁体   English

在画布上水平移动对象

[英]moving object horizentally on canvas

I need the object to move left to right, but in the code I have, it keeps right to left and goes back left to right: 我需要对象从左向右移动,但是在我拥有的代码中,它保持从右向左,然后从左向右返回:

function draw() {
    var time = new Date().getTime() * 0.002;
    var x = Math.sin(time * 0.5)*700
    var y = 58;

The problem is in line 3 . 问题在第3行。

Math.sin values oscillate between -1 and 1, which is why you're seeing your object go from right to left then back to right again. Math.sin值在-1和1之间振荡,这就是为什么您看到对象从右向左移动然后又从右向右移动的原因。 It's cycling through the min and max of Math.sin. 它在Math.sin的最小值和最大值之间循环。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin

Use this instead of Math.sin to achieve the desired effect: var x += time; 用它代替Math.sin来达到预期的效果: var x += time;

Why are you using Math.sin? 为什么要使用Math.sin? just use this: 只需使用此:

var x += time;

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

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