简体   繁体   English

如何使用onclick改变旋转方向

[英]how to change rotating direction using onclick

I should make a star rotating on first run, once the mouse is clicked the star should change its color and rotate in the opposite direction, if clicked again it should change the color and the direction again and so on.我应该在第一次运行时让星星旋转,一旦点击鼠标,星星应该改变它的颜色并以相反的方向旋转,如果再次点击它应该再次改变颜色和方向,依此类推。

I made 2 matrices one for each direction我为每个方向制作了 2 个矩阵

     function rotateZ(m, angle) {  // angle (distance) = velocity * time
      
        var csine = Math.cos(angle);
        var sine = Math.sin(angle);
      //  var sineanticlockwise= math.sin(angle)*-1;


        var mv0 = m[0], mv4 = m[4], mv8 = m[8];

        m[0] = csine *m[0]-sine *m[1];
       m[4] = csine *m[4]-sine *m[5];

       m[1] = csine *m[1]+sine *mv0;
       m[5] = csine *m[5] +sine *mv4;
     }

     function rotateclockwise(m, angle) {  // angle (distance) = velocity * time
      
        var csine = Math.cos(angle);
        var sine = Math.sin(angle)*-1;
      //  var sineanticlockwise= math.sin(angle)*-1;


        var mv0 = m[0], mv4 = m[4], mv8 = m[8];

        m[0] = csine *m[0]-sine *m[1];
       m[4] = csine *m[4]-sine *m[5];

       m[1] = csine *m[1]+sine *mv0;
       m[5] = csine *m[5] +sine *mv4;
     }

this is the animate function with a condition inside so that when the mouse event call it it changes direction every time the mouse is clicked but it doesn't work I tried putting it in the mouse event and still这是带有内部条件的动画函数,因此当鼠标事件调用它时,每次单击鼠标时它都会改变方向,但它不起作用我尝试将其放入鼠标事件中,但仍然

     var click=true;
     var time_old = 0;
     var animate = function(time) {
        var dt = time-time_old;
        
        if (click=true)
        {
            rotateZ(mov_matrix,dt*0.002); // distance = time* velocity(speed)
        time_old = time;
        click=false;
        }

        else
        {
     rotateclockwise(mov_matrix,dt*0.002); // distance = time* velocity(speed)
        time_old = time;
        click=true;
        }
 
     /* Step5: Drawing the required object (triangle) */
     gl.clearColor(0.19, 0.19, 0.20, 1); // Clear the canvas
     gl.enable(gl.DEPTH_TEST); // Enable the depth test
     gl.clear(gl.COLOR_BUFFER_BIT); // Clear the color buffer bit
     gl.viewport(0,0,canvas.width,canvas.height); // Set the view port
     gl.uniformMatrix4fv(Mmatrix, false, mov_matrix);
     gl.drawArrays(gl.TRIANGLES, 0, 9);  // Draw the triangles

        window.requestAnimationFrame(animate);   
     }


      function mouseFunction(e){

    var clickXrelativToCanvas = e.pageX - e.target.offsetLeft;
    var clickYrelativToCanvas = e.pageY - e.target.offsetTop;


    color = [Math.random(), Math.random(), Math.random(), 1];
    var colorLocation = gl.getUniformLocation(shaderProgram, "u_color");
    gl.uniform4fv(colorLocation, color);
      var time_old = 0;
     rotateZ(mov_matrix, dt*0.002);
     //rotateclockwise (mov_matrix, dt*0.002);

  };
     animate(0);

There are issues with the code.代码有问题。 Whether fixing them is enough to make your code work is not clear修复它们是否足以使您的代码工作尚不清楚

First off the if is not checking if click is true, it's setting click to true.首先, if不检查click是否为真,而是将click设置为真。

if (click = true)    // bad
if (click === true)  // good

and in general you should use === not ==一般来说你应该使用===而不是==

second the 2 code paths for the if both set click其次是如果两者都设置click的 2 个代码路径

  if (click === true) {   // <------------ fixed
    rotateZ(mov_matrix, dt * 0.002); // distance = time* velocity(speed)
    time_old = time;
    click = false;        // <------------ WHAT?
  } else {
    rotateclockwise(mov_matrix, dt * 0.002); // distance = time* velocity(speed)
    time_old = time;
    click = true;         // <------------ WHAT?
  }

It seems like you should delete those 2 lines otherwise every time through the loop it will just toggle click true -> false -> true -> false.似乎您应该删除这两行,否则每次循环时它只会切换单击 true -> false -> true -> false。 Instead toggle click in the mouse function而是在鼠标功能中切换单击

function mouseFunction(e) {
  click = !click;
  ...

Not sure why this code is in the mouse function不知道为什么这段代码在鼠标功能中

     var time_old = 0;
     rotateZ(mov_matrix, dt*0.002);

A more common way to do this though would be to set a rotation speed不过,一种更常见的方法是设置旋转速度

var rotationSpeed = 0.002;
var time_old = 0;
var animate = function(time) {
  var dt = time - time_old;
  time_old = time;

  rotateZ(mov_matrix, dt * rotationSpeed); // distance = time* velocity(speed)

  /* Step5: Drawing the required object (triangle) */
  gl.clearColor(0.19, 0.19, 0.20, 1); // Clear the canvas
  gl.enable(gl.DEPTH_TEST); // Enable the depth test
  gl.clear(gl.COLOR_BUFFER_BIT); // Clear the color buffer bit
  gl.viewport(0, 0, canvas.width, canvas.height); // Set the view port
  gl.uniformMatrix4fv(Mmatrix, false, mov_matrix);
  gl.drawArrays(gl.TRIANGLES, 0, 9); // Draw the triangles

  window.requestAnimationFrame(animate);
}


function mouseFunction(e) {
  rotationSpeed = -rotationSpeed; // flip the direction of rotation

  var clickXrelativToCanvas = e.pageX - e.target.offsetLeft;
  var clickYrelativToCanvas = e.pageY - e.target.offsetTop;


  color = [Math.random(), Math.random(), Math.random(), 1];
  var colorLocation = gl.getUniformLocation(shaderProgram, "u_color");
  gl.uniform4fv(colorLocation, color);
};
animate(0);

It's also not common to set uniforms in mouse events.在鼠标事件中设置制服也不常见。 It only works because you're drawing 1 thing.它之所以有效,是因为您正在绘制 1 样东西。 As soon as you try to draw 2 things in different colors you'll need to move setting the color inside animate so in the mouse function you'd choose a color but on animate you'd call gl.uinform4fv(colorLocation, color)一旦您尝试以不同的颜色绘制 2 个东西,您就需要在animate移动设置颜色,因此在鼠标功能中您可以选择一种颜色,但在animate您将调用gl.uinform4fv(colorLocation, color)

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

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