BGRABitmap tutorial 14/de: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | [[BGRABitmap tutorial/de|**Home**]] | [[BGRABitmap tutorial 1/de|**Tutorial 1**]] | [[BGRABitmap tutorial 2/de|**Tutorial 2**]] | [[BGRABitmap tutorial 3/de|**Tutorial 3**]] | [[BGRABitmap tutorial 4/de|**Tutorial 4**]] | [[BGRABitmap tutorial 5/de|**Tutorial 5**]] | [[BGRABitmap tutorial 6/de|**Tutorial 6**]] | [[BGRABitmap tutorial 7/de|**Tutorial 7**]] | [[BGRABitmap tutorial 8/de|**Tutorial 8**]] | [[BGRABitmap tutorial 9/de|**Tutorial 9**]] | [[BGRABitmap tutorial 10/de|**Tutorial 10**]] | [[BGRABitmap tutorial 11/de|**Tutorial 11**]] | [[BGRABitmap tutorial 12/de|**Tutorial 12**]] | [[BGRABitmap tutorial 13/de|**Tutorial 13**]] | **Tutorial 14** | [[BGRABitmap tutorial 15/de|**Tutorial 15**]] | [[BGRABitmap tutorial 16/de|**Tutorial 16**]] | [Edit](<http://wiki.lazarus.freepascal.org/index.php?title=Template:BGRABitmap_tutorial_index/de&action=edit>) | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | Dieses Tutorial zeigt Ihnen, wie Sie die Eigenschaft Canvas2D von BGRABitmap einsetzen können. Canvas2D arbeitet als Javascript-2D-Kontext des HTML-Canvas-Objektes. | |
| 4 | 4 | ||
| 5 | - | ||
| 5 | + | ## Contents | |
| 6 | 6 | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 7 | + | * 1 Erstes Programm | |
| 8 | + | * 2 Komplexe Figuren | |
| 9 | + | * 3 Verwendung von Transformationen | |
| 10 | + | * 4 Gradienten und Speicherfreigabe | |
| 11 | + | * 5 Weitere Beispiele | |
| 13 | 12 | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 13 | + | ## Erstes Programm | |
| 18 | 14 | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 15 | + | Hier ist ein sehr einfaches Beispiel: | |
| 22 | 16 | ``` | |
| 23 | - | ||
| 24 | - | ||
| 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 | + | ``` | |
| 25 | 32 | ||
| 26 | - | ||
| 33 | + | Die Bitmap hat eine Eigenschaft 'Canvas2D', die die Funktionen 'fillRect' und 'strokeRect' zur Verfügung stellt. Der Füllstil (fillStyle) ist festgelegt auf Orange durch die Angabe eines CSS-Farbstrings. Zum Füllen der Figur wird 'fillstyle' verwendet und zum Zeichnen des Randes 'strokestyle'. | |
| 27 | 34 | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 35 | + | Der obige Code ist äquivalent zu diesem Javascript-Code : | |
| 38 | 36 | ``` | |
| 37 | + | var canvas = document.getElementsByTagName("canvas")[0]; | |
| 38 | + | canvas.width = 200 | |
| 39 | + | canvas.height = 200 | |
| 40 | + | if (canvas.getContext){ | |
| 41 | + | var ctx = canvas.getContext("2d"); | |
| 42 | + | ctx.fillStyle = "rgb(240,128,0)"; | |
| 43 | + | ctx.fillRect(30,30,80,60); | |
| 44 | + | ctx.strokeRect(50,50,80,60); | |
| 45 | + | } | |
| 46 | + | ``` | |
| 39 | 47 | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 48 | + | [](</File:BGRATutorial14a.png>) | |
| 44 | 49 | ||
| 45 | 50 | ## Komplexe Figuren | |
| 46 | 51 | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 52 | + | Um komplexe Figuren zu zeichnen, ist es notwendig, einen Pfad zu definieren: | |
| 69 | 53 | ``` | |
| 54 | + | procedure pave(); | |
| 55 | + | begin | |
| 56 | + | ctx.fillStyle ('rgb(130,100,255)'); | |
| 57 | + | ctx.strokeStyle ('rgb(0,0,255)'); | |
| 58 | + | ctx.beginPath(); | |
| 59 | + | ctx.lineWidth:=2; | |
| 60 | + | ctx.moveTo(5,5);ctx.lineTo(20,10);ctx.lineTo(55,5);ctx.lineTo(45,18);ctx.lineTo(30,50); | |
| 61 | + | ctx.closePath(); | |
| 62 | + | ctx.stroke(); | |
| 63 | + | ctx.fill(); | |
| 64 | + | end; | |
| 65 | + | ||
| 66 | + | begin | |
| 67 | + | bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210)); | |
| 68 | + | ctx := bmp.Canvas2D; | |
| 69 | + | pave(); | |
| 70 | + | bmp.Draw(Canvas,0,0); | |
| 71 | + | bmp.Free; | |
| 72 | + | end; | |
| 73 | + | ``` | |
| 70 | 74 | ||
| 71 | - | ||
| 75 | + | Beachten Sie, dass die Linienbreite durch die Eigenschaft 'LineWidth' festgelegt ist und dass der Pfad mit dem Aufruf von 'BeginPath' anfängt. Falls Sie mehr Information über die Funktionsweise von 'Path' benötigen, siehe: [javascript documentation](<https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes>). | |
| 72 | 76 | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | + | [](</File:Tutorial14b.png>) | |
| 77 | 78 | ||
| 78 | 79 | ## Verwendung von Transformationen | |
| 79 | 80 | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 81 | + | Jetzt können wir das Dreieck sechs Mal mit einer Drehung zeichnen durch Aufruf der Transformationsfunktionen. | |
| 103 | 82 | ``` | |
| 83 | + | procedure six(); | |
| 84 | + | var | |
| 85 | + | i: Integer; | |
| 86 | + | begin | |
| 87 | + | ctx.save(); | |
| 88 | + | for i := 0 to 5 do | |
| 89 | + | begin | |
| 90 | + | ctx.rotate(2*PI/6); | |
| 91 | + | pave(); | |
| 92 | + | end; | |
| 93 | + | ctx.restore(); | |
| 94 | + | end; | |
| 95 | + | begin | |
| 96 | + | bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210)); | |
| 97 | + | ctx := bmp.Canvas2D; | |
| 98 | + | ctx.translate(80,80); | |
| 99 | + | six; | |
| 100 | + | bmp.Draw(Canvas,0,0); | |
| 101 | + | bmp.Free; | |
| 102 | + | end; | |
| 103 | + | ``` | |
| 104 | 104 | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 105 | + | [](</File:BGRATutorial14c.png>) | |
| 109 | 106 | ||
| 110 | 107 | ## Gradienten und Speicherfreigabe | |
| 111 | 108 | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 109 | + | Um Farbverläufe zu ermöglichen, besitzt Canvas2D die Funktionen **createLinearGradient** und **createPattern**. Diese Funktionen geben ein Objekt mit Referenzzählung zurück. Sie müssen dieses nicht explizit freigeben. Es wird freigegeben, wenn keine weitere Referenz auf dieses Objekt mehr vorhanden ist. Zum Beispiel: | |
| 110 | + | ``` | |
| 115 | 111 | var | |
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 132 | - | ||
| 133 | - | ||
| 112 | + | grad: IBGRACanvasGradient2D; | |
| 113 | + | begin | |
| 114 | + | grad := ctx.createLinearGradient(0,0,320,240); | |
| 115 | + | grad.addColorStop(0.3, '#ff0000'); | |
| 116 | + | grad.addColorStop(0.6, '#0000ff'); | |
| 117 | + | ctx.fillStyle(grad); | |
| 118 | + | ||
| 119 | + | grad := ctx.createLinearGradient(0,0,320,240); | |
| 120 | + | grad.addColorStop(0.3, '#ffffff'); | |
| 121 | + | grad.addColorStop(0.6, '#000000'); | |
| 122 | + | ctx.strokeStyle(grad); | |
| 123 | + | ctx.lineWidth := 5; | |
| 124 | + | ||
| 125 | + | ctx.moveto(160,120); | |
| 126 | + | ctx.arc(160,120,100,Pi/6,-Pi/6,false); | |
| 127 | + | ctx.fill(); | |
| 128 | + | ctx.stroke(); | |
| 129 | + | end; | |
| 134 | 130 | ``` | |
| 135 | 131 | ||
| 136 | - | ||
| 132 | + | Der Variablen 'grad' sind die Gradientenobjekte zugeordnet, aber es erfolgt kein Aufruf von Free. | |
| 137 | 133 | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 134 | + | [](</File:BGRATutorial14d.png>) | |
| 142 | 135 | ||
| 143 | 136 | ## Weitere Beispiele | |
| 144 | 137 | ||
| 145 | - | ||
| 138 | + | Andere Beispiele finden Sie im Verzeichnis 'testcanvas2d' des BGRABitmap-Archivs. Die Skripte wurden entnommen von [Jean-Paul Davalan Webseite](<https://web.archive.org/web/20190207062328/http://jean-paul.davalan.pagesperso-orange.fr/lang/jsc/js09.html>) (Internet Archive link), die auch Beispiele zur HTML-Canvas enthält. | |
| 146 | 139 | ||
| 147 | - | ||
| 140 | + | [[BGRABitmap tutorial 13/de|Voriges Tutorial (Pixelkoordinaten)]] | [[BGRABitmap tutorial 15/de|Nächstes Tutorial (3D)]] | |
| 148 | 141 | ||
| 149 | 142 | --- | |
| 150 | - | ||
| 143 | + | *From the Free Pascal wiki, page "BGRABitmap tutorial 14/de", as the Internet Archive kept it on 2024-08-07. 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).* |
