Sign in Join
Wiki copytemporary

BGRABitmap tutorial 14: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-This tutorial shows how to use the Canvas2D property of BGRABitmap. Canvas2D is designed to work like the Javascript 2D context of the HTML Canvas object.
1+[[BGRABitmap tutorial|**Home**]] | [[BGRABitmap tutorial 1|**Tutorial 1**]] | [[BGRABitmap tutorial 2|**Tutorial 2**]] | [[BGRABitmap tutorial 3|**Tutorial 3**]] | [[BGRABitmap tutorial 4|**Tutorial 4**]] | [[BGRABitmap tutorial 5|**Tutorial 5**]] | [[BGRABitmap tutorial 6|**Tutorial 6**]] | [[BGRABitmap tutorial 7|**Tutorial 7**]] | [[BGRABitmap tutorial 8|**Tutorial 8**]] | [[BGRABitmap tutorial 9|**Tutorial 9**]] | [[BGRABitmap tutorial 10|**Tutorial 10**]] | [[BGRABitmap tutorial 11|**Tutorial 11**]] | [[BGRABitmap tutorial 12|**Tutorial 12**]] | [[BGRABitmap tutorial 13|**Tutorial 13**]] | **Tutorial 14** | [[BGRABitmap tutorial 15|**Tutorial 15**]] | [[BGRABitmap tutorial 16|**Tutorial 16**]] | [[/wiki.freepascal.org/index.php?title=Template:BGRABitmap tutorial index&action=edit|Edit]]
22
3-## First program
3+This tutorial shows how to use the Canvas2D property of BGRABitmap. Canvas2D is designed to work like the Javascript 2D context of the HTML Canvas object.
44
5-Here is a very simple example:
5+## Contents
66
7-``` pascal
8-procedure TForm1.FormPaint(Sender: TObject);
9-var bmp: TBGRABitmap;
10- ctx: TBGRACanvas2D;
11-begin
12- bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
7+ * 1 First program
8+ * 2 Complex shapes
9+ * 3 Using transformations
10+ * 4 Gradients and memory usage
11+ * 5 More examples
1312
14- ctx := bmp.Canvas2D;
15- ctx.fillStyle('rgb(240,128,0)');
16- ctx.fillRect(30,30,80,60);
17- ctx.strokeRect(50,50,80,60);
13+## First program
1814
19- bmp.Draw(Canvas,0,0);
20- bmp.Free;
21-end;
15+Here is a very simple example:
2216```
23-
24-<table>
25-
26-<tr>
27-
28-<td bgcolor="#FFFFe0" style="border: 1px solid black; padding: 4px; text-align: center; border-radius:5px" width="30%">
29-
30-Note that path fonctions are defined in the [[BGRABitmap Geometry types|IBGRAPath interface]]. The <b>TBGRAPath</b> class of unit BGRAPath[1](https://github.com/bgrabitmap/bgrabitmap/blob/master/bgrabitmap/bgrapath.pas) provides an independent path object that you can use to create shapes. A sample project can be found in [[BGRABitmap AggPas]].
31-
32-</td>
33-
34-</tr>
35-
36-</table>
37-
38-The bitmap has a Canvas2D property which provides the fillRect and strokeRect function. The fillStyle is defined to orange by specifying a css color string. When the shape is filled, it uses the fill style, and when the border is drawn, it uses the stroke style.
17+procedure TForm1.FormPaint(Sender: TObject);
18+ var bmp: TBGRABitmap;
19+ ctx: TBGRACanvas2D;
20+ begin
21+ bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
22+
23+ ctx := bmp.Canvas2D;
24+ ctx.fillStyle('rgb(240,128,0)');
25+ ctx.fillRect(30,30,80,60);
26+ ctx.strokeRect(50,50,80,60);
27+
28+ bmp.Draw(Canvas,0,0);
29+ bmp.Free;
30+ end;
31+```
3932
40-The code above is equivalent to this Javascript code:
33+Note that path fonctions are defined in the [[BGRABitmap Geometry types|IBGRAPath interface]]. The **TBGRAPath** class of unit BGRAPath[[1]](<https://github.com/bgrabitmap/bgrabitmap/blob/master/bgrabitmap/bgrapath.pas>) provides an independent path object that you can use to create shapes. A sample project can be found in [[BGRABitmap AggPas]]> "BGRABitmap AggPas").
34+---
35+
36+The bitmap has a Canvas2D property which provides the fillRect and strokeRect function. The fillStyle is defined to orange by specifying a css color string. When the shape is filled, it uses the fill style, and when the border is drawn, it uses the stroke style.
4137
42-``` javascript
43- var canvas = document.getElementsByTagName("canvas")[0];
44- canvas.width = 200
45- canvas.height = 200
46- if (canvas.getContext){
47- var ctx = canvas.getContext("2d");
48- ctx.fillStyle = "rgb(240,128,0)";
49- ctx.fillRect(30,30,80,60);
50- ctx.strokeRect(50,50,80,60);
51- }
38+The code above is equivalent to this Javascript code:
5239```
40+var canvas = document.getElementsByTagName("canvas")[0];
41+ canvas.width = 200
42+ canvas.height = 200
43+ if (canvas.getContext){
44+ var ctx = canvas.getContext("2d");
45+ ctx.fillStyle = "rgb(240,128,0)";
46+ ctx.fillRect(30,30,80,60);
47+ ctx.strokeRect(50,50,80,60);
48+ }
49+```
5350
54-<figure>
55-<img src="BGRATutorial14a.png" title="BGRATutorial14a.png" />
56-<figcaption>BGRATutorial14a.png</figcaption>
57-</figure>
51+[![BGRATutorial14a.png](/wiki-images/5/5d/BGRATutorial14a.png)](</File:BGRATutorial14a.png>)
5852
5953## Complex shapes
6054
61-To draw a complex shape, it is necessary to define a path:
62-
63-``` pascal
64- procedure pave();
65- begin
66- ctx.fillStyle ('rgb(130,100,255)');
67- ctx.strokeStyle ('rgb(0,0,255)');
68- ctx.beginPath();
69- ctx.lineWidth:=2;
70- ctx.moveTo(5,5);ctx.lineTo(20,10);ctx.lineTo(55,5);ctx.lineTo(45,18);ctx.lineTo(30,50);
71- ctx.closePath();
72- ctx.stroke();
73- ctx.fill();
74- end;
75-
76-begin
77- bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
78- ctx := bmp.Canvas2D;
79- pave();
80- bmp.Draw(Canvas,0,0);
81- bmp.Free;
82-end;
55+To draw a complex shape, it is necessary to define a path:
8356```
57+procedure pave();
58+ begin
59+ ctx.fillStyle ('rgb(130,100,255)');
60+ ctx.strokeStyle ('rgb(0,0,255)');
61+ ctx.beginPath();
62+ ctx.lineWidth:=2;
63+ ctx.moveTo(5,5);ctx.lineTo(20,10);ctx.lineTo(55,5);ctx.lineTo(45,18);ctx.lineTo(30,50);
64+ ctx.closePath();
65+ ctx.stroke();
66+ ctx.fill();
67+ end;
68+
69+ begin
70+ bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
71+ ctx := bmp.Canvas2D;
72+ pave();
73+ bmp.Draw(Canvas,0,0);
74+ bmp.Free;
75+ end;
76+```
8477
85-Notice that the line width is defined by the property lineWidth and that the path begins with a beginPath call. If you want more information on the way path works, see the [Javascript documentation](https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes).
78+Notice that the line width is defined by the property lineWidth and that the path begins with a beginPath call. If you want more information on the way path works, see the [Javascript documentation](<https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes>).
8679
87-<figure>
88-<img src="Tutorial14b.png" title="Tutorial14b.png" />
89-<figcaption>Tutorial14b.png</figcaption>
90-</figure>
80+[![Tutorial14b.png](/wiki-images/a/a4/Tutorial14b.png)](</File:Tutorial14b.png>)
9181
9282## Using transformations
9383
94-Now we can draw the triangle six times with a rotation by calling transformation functions.
95-
96-``` pascal
97- procedure six();
98- var
99- i: Integer;
100- begin
101- ctx.save();
102- for i := 0 to 5 do
103- begin
104- ctx.rotate(2*PI/6);
105- pave();
106- end;
107- ctx.restore();
108- end;
109-begin
110- bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
111- ctx := bmp.Canvas2D;
112- ctx.translate(80,80);
113- six;
114- bmp.Draw(Canvas,0,0);
115- bmp.Free;
116-end;
84+Now we can draw the triangle six times with a rotation by calling transformation functions.
11785```
86+procedure six();
87+ var
88+ i: Integer;
89+ begin
90+ ctx.save();
91+ for i := 0 to 5 do
92+ begin
93+ ctx.rotate(2*PI/6);
94+ pave();
95+ end;
96+ ctx.restore();
97+ end;
98+ begin
99+ bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
100+ ctx := bmp.Canvas2D;
101+ ctx.translate(80,80);
102+ six;
103+ bmp.Draw(Canvas,0,0);
104+ bmp.Free;
105+ end;
106+```
118107
119-<figure>
120-<img src="BGRATutorial14c.png" title="BGRATutorial14c.png" />
121-<figcaption>BGRATutorial14c.png</figcaption>
122-</figure>
108+[![BGRATutorial14c.png](/wiki-images/9/91/BGRATutorial14c.png)](</File:BGRATutorial14c.png>)
123109
124110## Gradients and memory usage
125111
126-To use gradients, Canvas2D provides the createLinearGradient and createPattern functions. Theses functions return an interfaced object. You must not free them explicitely. They are freed when there are no more references to them. For example:
127-
128-``` pascal
112+To use gradients, Canvas2D provides the createLinearGradient and createPattern functions. Theses functions return an interfaced object. You must not free them explicitely. They are freed when there are no more references to them. For example:
113+```
129114var
130- grad: IBGRACanvasGradient2D;
131-begin
132- grad := ctx.createLinearGradient(0,0,320,240);
133- grad.addColorStop(0.3, '#ff0000');
134- grad.addColorStop(0.6, '#0000ff');
135- ctx.fillStyle(grad);
136-
137- grad := ctx.createLinearGradient(0,0,320,240);
138- grad.addColorStop(0.3, '#ffffff');
139- grad.addColorStop(0.6, '#000000');
140- ctx.strokeStyle(grad);
141- ctx.lineWidth := 5;
142-
143- ctx.moveto(160,120);
144- ctx.arc(160,120,100,Pi/6,-Pi/6,false);
145- ctx.fill();
146- ctx.stroke();
147-end;
115+ grad: IBGRACanvasGradient2D;
116+ begin
117+ grad := ctx.createLinearGradient(0,0,320,240);
118+ grad.addColorStop(0.3, '#ff0000');
119+ grad.addColorStop(0.6, '#0000ff');
120+ ctx.fillStyle(grad);
121+
122+ grad := ctx.createLinearGradient(0,0,320,240);
123+ grad.addColorStop(0.3, '#ffffff');
124+ grad.addColorStop(0.6, '#000000');
125+ ctx.strokeStyle(grad);
126+ ctx.lineWidth := 5;
127+
128+ ctx.moveto(160,120);
129+ ctx.arc(160,120,100,Pi/6,-Pi/6,false);
130+ ctx.fill();
131+ ctx.stroke();
132+ end;
148133```
149134
150-The grad variable is assigned with the gradient objects, but there is no call to Free.
135+The grad variable is assigned with the gradient objects, but there is no call to Free.
151136
152-<figure>
153-<img src="BGRATutorial14d.png" title="BGRATutorial14d.png" />
154-<figcaption>BGRATutorial14d.png</figcaption>
155-</figure>
137+[![BGRATutorial14d.png](/wiki-images/9/96/BGRATutorial14d.png)](</File:BGRATutorial14d.png>)
156138
157139## More examples
158140
159-Other examples are in the testcanvas2d directory of BGRABitmap archive. Scripts are taken from the [Jean-Paul Davalan web site](https://web.archive.org/web/20190207062328/http://jean-paul.davalan.pagesperso-orange.fr/lang/jsc/js09.html) (Internet Archive link) which contains HTML Canvas examples.
141+Other examples are in the testcanvas2d directory of BGRABitmap archive. Scripts are taken from the [Jean-Paul Davalan web site](<https://web.archive.org/web/20190207062328/http://jean-paul.davalan.pagesperso-orange.fr/lang/jsc/js09.html>) (Internet Archive link) which contains HTML Canvas examples.
160142
161143[[BGRABitmap tutorial 13|Previous tutorial (pixel coordinates)]] [[BGRABitmap tutorial 15|Next tutorial (3D)]]
162144
163145---
164-*From the Free Pascal wiki, page "BGRABitmap tutorial 14", as it stood on 2023-05-21 after a change by Circular, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
165-Category: BGRABitmap
166-Category: Graphics
146+*From the Free Pascal wiki, page "BGRABitmap tutorial 14", as the Internet Archive kept it on 2024-12-09. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history