WEB DEVELOPMENT
Enviado por jajajaja3 • 17 de Mayo de 2015 • Tesis • 2.752 Palabras (12 Páginas) • 146 Visitas
Header indie font bundle logo
Grab the Indie Font Bundle today and save over 97%! Check it out
tuts+
Free Tutorials
Courses
eBooks
Blog
Pricing
Sign In
Free Account
Code
Categories
Learning Guides Expert HelpNew!
WEB DEVELOPMENT
Canvas From Scratch: Transformations and Gradients
by Rob Hawkes20 Apr 201124 Comments
Share
1
Share
Share
This post is part of a series called Canvas From Scratch.
Canvas From Scratch: Advanced Drawing
Canvas from Scratch: Pixel Manipulation
In this article, I'm going to walk you through transformations in the canvas, as well as shadows and gradients. Transformations are an extremely valuable set of methods that allow you to start being creative with the way you draw objects on the canvas. Let's get started after the jump!
Setting Up
You're going to use the same HTML template from the previous articles, so open up your favourite editor and paste in the following code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html>
<head>
<title>Canvas from scratch</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
});
</script>
</head>
<body>
<canvas id="myCanvas" width="500" height="500">
<!-- Insert fallback content here -->
</canvas>
</body>
</html>
Here we have nothing more than a basic HTML page with a canvas element and some JavaScript that runs after the DOM has loaded. Nothing crazy.
Translations in Action
Translate essentially shifts the entire coordinate system.
One of the simplest transformations in canvas is translate. This allows you to move the origin point of the 2d rendering context; the (0, 0) position on the canvas. Let me show you what this means.
First, place a square in canvas at the position (0, 0):
1
ctx.fillRect(0, 0, 100, 100);
It will draw itself at the top left hand edge of the canvas. Still - nothing out of the ordinary here.
A simple square
Now, try translating the 2d rendering context and drawing another square in the same position:
1
2
3
4
5
ctx.save();
ctx.translate(100, 100);
ctx.fillStyle = "rgb(0, 0, 255)";
ctx.fillRect(0, 0, 100, 100);
ctx.restore();
What do you think will happen? Have a gold star if you guessed that the new square will be drawn at the position (100, 100). No play time for those who guessed wrong. Sorry!
Translating a square
So what happened here then? As far as the code for drawing the second square is concerned, you drew it in the same place as the first square. The reason for this is that you basically shifted the entire coordinate system of the canvas so that its (0, 0) position is now at the place (100, 100).
How translation works
Does it make a little more sense now? I hope so. It can take a little while to get your head around, but it's a simple concept once you understand it.
You probably wouldn't use this transformation too much on its own, as you could simply draw the second square at (100, 100) to
...