--- url: "https://web.dev/articles/webgl-transforms" titulek: "WebGL transforms | Articles | web.dev" h1: "WebGL transforms Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/webgl-transforms" drobecky: ["Articles", "WebGL transforms"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:36:21" znaku: 21550 slov: 3630 kodovani: "iso-8859-2" sha256: "c6a786bb1a28961dacf3fb1919b430515666307c15153872ca33adab5e416212" --- WebGL transforms Stay organized with collections Save and categorize content based on your preferences. Gregg Tavares WebGL 2D Translation Before we move on to 3D let's stick with 2D for a little while longer. Bear with me please. This article might seem exceedingly obvious to some but I'll build up to a point in a few articles. This article is a continuation of a series starting with [WebGL Fundamentals](https://www.html5rocks.com/tutorials/webgl/webgl_fundamentals/). If you haven't read it I suggest you read at least the first chapter then come back here. Translation is some fancy math name that basically means "to move" something. I suppose moving a sentence from English to Japanese fits as well but in this case we're talking about moving geometry. Using the sample code we ended up with in [the first post](https://www.html5rocks.com/tutorials/webgl/webgl_fundamentals/) you could easily translate our rectangle just by changing the values passed to setRectangle right? Here's a sample based on our [previous sample](https://www.html5rocks.com/tutorials/webgl/webgl_fundamentals/). `// First lets make some variables // to hold the translation of the rectangle var translation = [0, 0]; // then let's make a function to // re-draw everything. We can call this // function after we update the translation. // Draw the scene. function drawScene() { // Clear the canvas. gl.clear(gl.COLOR_BUFFER_BIT); // Setup a rectangle setRectangle(gl, translation[0], translation[1], width, height); // Draw the rectangle. gl.drawArrays(gl.TRIANGLES, 0, 6); }` So far so good. But now imagine we wanted to do the same thing with a more complicated shape. Let's say we wanted to draw an 'F' that consists of 6 triangles like this. Well, following are current code we'd have to change setRectangle to something more like this. `// Fill the buffer with the values that define a letter 'F'. function setGeometry(gl, x, y) { var width = 100; var height = 150; var thickness = 30; gl.bufferData( gl.ARRAY_BUFFER, new Float32Array([ // left column x, y, x + thickness, y, x, y + height, x, y + height, x + thickness, y, x + thickness, y + height, // top rung x + thickness, y, x + width, y, x + thickness, y + thickness, x + thickness, y + thickness, x + width, y, x + width, y + thickness, // middle rung x + thickness, y + thickness * 2, x + width * 2 / 3, y + thickness * 2, x + thickness, y + thickness * 3, x + thickness, y + thickness * 3, x + width * 2 / 3, y + thickness * 2, x + width * 2 / 3, y + thickness * 3]), gl.STATIC_DRAW); }` You can hopefully see that's not going to scale well. If we want to draw some very complex geometry with hundreds or thousands of lines we'd have to write some pretty complex code. On top of that, every time we draw JavaScript has to update all the points. There's a simpler way. Just upload the geometry and do the translation in the shader. Here's the new shader `` And in JavaScript we need to multiply by the projection matrix `// Draw the scene. function drawScene() { ... // Compute the matrices var projectionMatrix = make2DProjection(canvas.width, canvas.height); ... // Multiply the matrices. var matrix = matrixMultiply(scaleMatrix, rotationMatrix); matrix = matrixMultiply(matrix, translationMatrix); matrix = matrixMultiply(matrix, projectionMatrix); ... }` We also removed the code that set the resolution. With this last step we've gone from a rather complicated shader with 6-7 steps to a very simple shader with only 1 step all do to the magic of matrix math. I hope this article has helped demystify matrix math. I'll move on to 3D next. In 3D matrix math follows the same principles and usage. I started with 2D to hopefully keep it simple to understand. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2012-04-18 UTC.