BGRABitmap tutorial 14

From Free Pascal wiki
Revision as of 22:33, 15 June 2011 by Circular (talk | contribs) (→‎First program: combining shapes)
Jump to navigationJump to search

Deutsch (de) English (en)


Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 | Tutorial 16 | Edit

This tutorial show how to use Canvas2D property of BGRABitmap. Canvas2D is designed to work as the javascript 2d context of the HTML Canvas object.

First program

Here is a very simple example : <delphi> procedure TForm1.FormPaint(Sender: TObject); var bmp: TBGRABitmap;

   ctx: TBGRACanvas2D;   

begin

 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
 ctx := bmp.Canvas2D;
 ctx.fillStyle('rgb(240,128,0)');
 ctx.fillRect(30,30,80,60);
 ctx.strokeRect(50,50,80,60);  
 bmp.Draw(Canvas,0,0);
 bmp.Free;

end; </delphi>

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.

The code above is equivalent to this javascript code : <javascript>

var canvas = document.getElementsByTagName("canvas")[0];
canvas.width = 200
canvas.height = 200
if (canvas.getContext){
  var ctx = canvas.getContext("2d"); 
  ctx.fillStyle = "rgb(240,128,0)";
  ctx.fillRect(30,30,80,60);
  ctx.strokeRect(50,50,80,60);
}

</javascript>

BGRATutorial14a.png

Complex shapes

To draw a complex shape, it is necessary to define a path : <delphi> procedure pave();

 begin
   ctx.fillStyle ('rgb(130,100,255)');
   ctx.strokeStyle ('rgb(0,0,255)');
   ctx.beginPath();
   ctx.lineWidth:=2;
   ctx.moveTo(5,5);ctx.lineTo(20,10);ctx.lineTo(55,5);ctx.lineTo(45,18);ctx.lineTo(30,50);
   ctx.closePath();
   ctx.stroke();
   ctx.fill();
 end;  

begin

 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
 ctx := bmp.Canvas2D;
 pave();
 bmp.Draw(Canvas,0,0);
 bmp.Free;

end; </delphi>

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 javascript documentation.

Tutorial14b.png

Using transformations

Now we can draw the triangle six times with a rotation by calling transformation functions.

<delphi> procedure six();

 var
   i: Integer;
 begin
    ctx.save();
    for i := 0 to 5 do
    begin
       ctx.rotate(2*PI/6);
       pave();
    end;
    ctx.restore();
 end;

begin

 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
 ctx := bmp.Canvas2D;
 ctx.translate(80,80);
 six;
 bmp.Draw(Canvas,0,0);
 bmp.Free;

end; </delphi>

BGRATutorial14c.png

More examples

Other examples are in testcanvas2d directory of BGRABitmap archive. Scripts are taken from Jean-Paul Davalan web site which contains HTML Canvas examples.

Previous tutorial (pixel coordinates)