Rysowanie grafik za pomocą Canvas
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
Spis treści |
[edytuj] Wprowadzenie
Od wersji Firefoksa 1.5, Firefox zawiera nowy element
HTML dla programowalnej grafiki - <canvas> oparty na specyfikacji:
WHATWG canvas specification,
która bazuje na specyfikacji <canvas> Apple'a wprowadzonej w Safari.
Może być użyty dla wyświetlania grafik, elementów interfejsu użytkownika, i innych
grafik tworzonych po stronie klienta.
Element <canvas> tworzy powierzchnię do rysowania o stałej wielkości that exposes one or more rendering contexts. We'll focus on the 2D rendering
context (incidentally, the only currently defined rendering context).
In the future, other contexts may provide different types of
rendering; for example, it is likely that a 3D context based on OpenGL ES will eventually be added to the <canvas> specification.
[edytuj] The 2D Rendering Context
[edytuj] Prosty przykład
Rozpoczniemy od prostego przykładu w którym narysujemy dwa przecinające się prostokąty , z których jeden posiada przezroczystość alpha:
<html>
<head>
<script type="application/x-javascript">
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (10, 10, 50, 50);
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (30, 30, 50, 50);
}
</script>
</head>
<body onload="draw()">
<canvas id="canvas" width="300" height="300"></canvas>
</body>
</html>
Funkcja draw gets the canvas element, then
obtains the 2d context. The ctx object can then be
used to actually render to the canvas. Przykład po prostu wypełnia dwa prostokąty
,używając własności wypełnienia fillStyle dla dwóch różnych kolorów ze specyfikacji CSS i wywołując funkcję fillRect. Drugie wypełnienie używa wartości rgba()aby wraz z kolorem określić wartość przezroczystości alpha .
Funkcja fillRect, strokeRect, i clearRect wywołuje rysowanie wypełnienia, konturu, lub wyczyszczenie prostokata. Żeby rysować bardziej skomplikowane kształty, są używane ścieżki.
[edytuj] Zastosowanie ścieżek
Funkcja beginPathtworzy nową ścieżkę, i za pomocą moveTo, lineTo, arcTo, arc, oraz podobnych metod dodajemy do niej kolejne segmenty owej ścieżki. Ścieżkę kończymy używając funkcji closePath. Jeśli tylko scieżka zostanie utworzona, możemy użyć fill lub stroke do odtworzenia ścieżki dla elementu canvas .
<html>
<head>
<script type="application/x-javascript">
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.beginPath();
ctx.moveTo(30, 30);
ctx.lineTo(150, 150);
ctx.quadraticCurveTo(60, 70, 70, 150);
ctx.lineTo(30, 30);
ctx.fill();
}
</script>
</head>
<body onload="draw()">
<canvas id="canvas" width="300" height="300"></canvas>
</body>
</html>
Calling fill() lub stroke() causes the current path
to be used. To be filled or stroked again, the path must be recreated.
[edytuj] Graphics State
Attributes of the context such as fillStyle,
strokeStyle, lineWidth, and lineJoin are
part of the current graphics state. The context provides two
methods, save() and restore(), that can be used to
move the current state to and from the state stack.
[edytuj] Bardziej skomplikowany przykład
Here's a little more complicated example, that uses paths, state, and
also introduces the current transformation matrix. The context
methods translate(), scale(), and rotate()
all transform the current matrix. All rendered points are first
transformed by this matrix.
<html>
<head>
<script type="application/x-javascript">
function drawBowtie(ctx, fillStyle) {
ctx.fillStyle = "rgba(200,200,200,0.3)";
ctx.fillRect(-30, -30, 60, 60);
ctx.fillStyle = fillStyle;
ctx.globalAlpha = 1.0;
ctx.beginPath();
ctx.moveTo(25, 25);
ctx.lineTo(-25, -25);
ctx.lineTo(25, -25);
ctx.lineTo(-25, 25);
ctx.closePath();
ctx.fill();
}
function dot(ctx) {
ctx.save();
ctx.fillStyle = "black";
ctx.fillRect(-2, -2, 4, 4);
ctx.restore();
}
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// note that all other translates are relative to this
// one
ctx.translate(45, 45);
ctx.save();
//ctx.translate(0, 0); // unnecessary
drawBowtie(ctx, "red");
dot(ctx);
ctx.restore();
ctx.save();
ctx.translate(85, 0);
ctx.rotate(45 * Math.PI / 180);
drawBowtie(ctx, "green");
dot(ctx);
ctx.restore();
ctx.save();
ctx.translate(0, 85);
ctx.rotate(135 * Math.PI / 180);
drawBowtie(ctx, "blue");
dot(ctx);
ctx.restore();
ctx.save();
ctx.translate(85, 85);
ctx.rotate(90 * Math.PI / 180);
drawBowtie(ctx, "yellow");
dot(ctx);
ctx.restore();
}
</script>
</head>
<body onload="draw()">
<canvas id="canvas" width="300" height="300"></canvas>
</body>
</html>
This defines two methods, drawBowtie and dot, that
are called 4 times. Before each call, translate() and
rotate() are used to set up the current transformation
matrix, which in turn positions the dot and the bowtie. dot
renders a small black square centered at (0, 0). That dot is
moved around by the transformation matrix. drawBowtie
renders a simple bowtie path using the passed-in fill style.
As matrix operations are cumulative, save() and
restore() are used around each set of calls to restore the
original canvas state. One thing to watch out for is that rotation
always occurs around the current origin; thus a translate()
rotate() translate() sequence will yield different results than a
translate() translate() rotate() series of calls.
[edytuj] Compatibility With Apple <canvas>
For the most part, <canvas> is compatible with Apple's and other implementations. There are, however, a few issues to be aware of, described here.
[edytuj] Żądany znacznik </canvas>
In the Apple Safari implementation, <canvas> is an element implemented in much the same way <img> is; it does not have an end tag. However, for <canvas> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation has a required end tag.
If fallback content is not needed, a simple <canvas id="foo" ...></canvas> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.
If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content). Todo: get hixie to put the CSS bits in
[edytuj] Dodatkowe możliwości
[edytuj] Rendering Web Content Into A Canvas
Mozilla's canvas is extended with the drawWindow
method. This method draws a snapshot of the contents of a DOM window into the canvas. For example,
ctx.drawWindow(window, 0, 0, 100, 200, "rgb(0,0,0)");
would draw the contents of the current window, in the rectangle (0,0,100,200) in pixels relative to the top-left of the viewport, on a black background, into the canvas. By specifying "rgba(0,0,0,0)" as the color, the contents would be drawn with a transparent background (which would be slower).
With this method, it is possible to fill a hidden IFRAME with arbitrary content (e.g., CSS-styled HTML text, or SVG) and draw it into a canvas. It will be scaled, rotated and so on according to the current transformation.
Ted Mielczarek's tab preview extension uses this technique in chrome to provide thumbnails of web pages, and the source is available for reference.


