グラフィックス(HasDrawing)
HasDrawing トレイトは、線、矩形、円、多角形、曲線、矢印、扇形のためのベクター描画プリミティブを提供します。すべてのメソッドは static を返すため、すべての呼び出しをチェーンできます。
クイックリファレンス
| メソッド | 図形 |
|---|---|
line() | 2点間の直線 |
rect() | 矩形 |
roundedRect() | 角丸矩形 |
circle() | 円 |
ellipse() | 楕円 |
polygon() | 点配列による任意の多角形 |
regularPolygon() | 正多角形(n辺) |
starPolygon() | 星形 |
arrow() | 矢印付きの線 |
pieSector() | チャート用の扇形 |
curve() | 3次ベジェ曲線 |
polyCurve() | 複数セグメントのベジェ曲線 |
基本例
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setDrawColor(255, 0, 0)
->setFillColor(200, 220, 255)
->line(10, 10, 100, 10)
->rect(10, 20, 80, 40, 'DF')
->roundedRect(10, 70, 80, 40, 5, 'DF')
->circle(150, 40, 30, 'DF')
->ellipse(150, 100, 40, 20, 'DF')
->arrow(10, 140, 100, 140)
->regularPolygon(150, 160, 25, 6, 'DF')
->starPolygon(50, 200, 25, 5, 3, 'DF');スタイルパラメータ
ほとんどの描画メソッドは、レンダリングを制御する $style 文字列を受け取ります:
| 値 | 意味 |
|---|---|
S | ストローク(アウトラインのみ) — デフォルト |
F | 塗りつぶしのみ |
DF または B | 描画と塗りつぶし(両方) |
線と矩形
php
$pdf->line(float $x1, float $y1, float $x2, float $y2);
$pdf->rect(float $x, float $y, float $w, float $h, string $style = '');
$pdf->roundedRect(float $x, float $y, float $w, float $h, float $r, string $style = '');line() は点(x1, y1)から点(x2, y2)に線を描画します。rect() は標準的な矩形を描画します。roundedRect() は半径 $r の角丸を追加します。
円と楕円
php
$pdf->circle(float $x0, float $y0, float $r, string $style = '');
$pdf->ellipse(float $x0, float $y0, float $rx, float $ry, string $style = '');circle() は中心点と半径を取ります。ellipse() は水平半径と垂直半径を別々に指定します。
多角形
php
$pdf->polygon(array $points, string $style = '');
$pdf->regularPolygon(float $x0, float $y0, float $r, int $ns, string $style = '');
$pdf->starPolygon(float $x0, float $y0, float $r, int $nv, int $ng, string $style = '');polygon() はフラットな座標配列 [x1, y1, x2, y2, ...] を受け取ります。regularPolygon() は半径 $r の円に内接するn辺の多角形を描画します。starPolygon() は $nv 個の頂点とギャップファクター $ng を持つ星形を描画します。
矢印と扇形
php
$pdf->arrow(float $x0, float $y0, float $x1, float $y1);
$pdf->pieSector(float $xc, float $yc, float $r, float $a, float $b, string $style = '');arrow() は目的地に矢じりのある線を描画します。pieSector() は角度 $a から $b(度単位)の扇形を描画し、チャートに便利です:
php
$pdf->setFillColor(255, 100, 100)->pieSector(100, 100, 40, 0, 120, 'F')
->setFillColor(100, 255, 100)->pieSector(100, 100, 40, 120, 250, 'F')
->setFillColor(100, 100, 255)->pieSector(100, 100, 40, 250, 360, 'F');ベジェ曲線
php
$pdf->curve(float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3);
$pdf->polyCurve(array $points);curve() は制御点(x1, y1)と(x2, y2)を使用して、(x0, y0)から(x3, y3)への3次ベジェを描画します。polyCurve() は点配列から複数のベジェセグメントを連結します。
線のスタイル
php
$pdf->setLineStyle(array $style);$style 配列は以下のキーをサポートします:width(float)、cap(butt、round、square)、join(miter、round、bevel)、dash(文字列または配列パターン)、color(RGB配列)。
php
$pdf->setLineStyle([
'width' => 0.5,
'cap' => 'round',
'join' => 'round',
'dash' => '3,2',
'color' => [0, 0, 200],
])->line(10, 10, 190, 10);トンボとレジストレーションマーク
プロ印刷用のマークを追加します:
php
$pdf->cropMark(20, 20, 10, 10)
->registrationMark(105, 10)
->colorRegistrationBar(20, 280, 170, 5);