下一

In this example, we'll actually rotate our square plane.

This example uses the glMatrix library to perform its matrix and vertex math. You'll need to include it if you create your own project based on this code. Our sample loads a copy from a CDN in our HTML's <head> .

Making the square rotate

Let's start by making the square rotate. The first thing we'll need is a variable in which to track the current rotation of the square:

var squareRotation = 0.0;
					

Now we need to update the drawScene() function to apply the current rotation to the square when drawing it. After translating to the initial drawing position for the square, we apply the rotation like this:

  mat4.rotate(modelViewMatrix,  // destination matrix
              modelViewMatrix,  // matrix to rotate
              squareRotation,   // amount to rotate in radians
              [0, 0, 1]);       // axis to rotate around
					

This rotates the modelViewMatrix by the current value of squareRotation , around the Z axis.

To actually animate, we need to add code that changes the value of squareRotation over time. We can do that by creating a new variable to track the time at which we last animated (let's call it then ), then adding the following code to the end of the main function

  var then = 0;
  // Draw the scene repeatedly
  function render(now) {
    now *= 0.001;  // convert to seconds
    const deltaTime = now - then;
    then = now;
    drawScene(gl, programInfo, buffers, deltaTime);
    requestAnimationFrame(render);
  }
  requestAnimationFrame(render);
					

This code uses requestAnimationFrame to ask the browser to call the function " render " on each frame. requestAnimationFrame passes us the time in milliseconds since the page loaded. We convert that to seconds and then subtract from it the last time to compute deltaTime which is the number of second since the last frame was rendered. At the end of drawscene we add the code to update squareRotation.

  squareRotation += deltaTime;
					

This code uses the amount of time that's passed since the last time we updated the value of squareRotation to determine how far to rotate the square.

View the complete code | Open this demo on a new page

元数据

  1. WebGL API
  2. WebGL 教程
    1. Getting started with WebGL
    2. Adding 2D content to a WebGL context
    3. Using shaders to apply color in WebGL
    4. Animating objects with WebGL
    5. Creating 3D objects using WebGL
    6. Using textures in WebGL
    7. Lighting in WebGL
    8. Animating textures in WebGL
  3. Examples and articles
    1. Matrix math for the web
    2. WebGL model view projection
    3. WebGL best practices
    4. 使用 WebGL 扩展
    5. A basic 2D WebGL animation example
    6. WebGL by example
  4. 接口
    1. WebGLRenderingContext
    2. WebGL2RenderingContext
    3. WebGLActiveInfo
    4. WebGLBuffer
    5. WebGLContextEvent
    6. WebGLFramebuffer
    7. WebGLProgram
    8. WebGLQuery
    9. WebGLRenderbuffer
    10. WebGLSampler
    11. WebGLShader
    12. WebGLShaderPrecisionFormat
    13. WebGLSync
    14. WebGLTexture
    15. WebGLTransformFeedback
    16. WebGLUniformLocation
    17. WebGLVertexArrayObject
  5. 文档编制:
  6. 有用清单
    1. Pages tagged "WebGL"
  7. 贡献
    1. WebGL doc status
    2. MDN 工程