GraphicsContext 定義了繪圖操作環(huán)境。
A。基本功能是繪圖操作:
1. 簡單幾何圖形:drawRect,drawLine,drawEllipse,drawConvexPolygon
,strokeArc,fillRect,fillRoundedRect,strokeRect,clearRect。
2. 路徑:drawPath,fillPath,strokePath。
3. 圖像:drawImage,drawTiledImage,drawImageBuffer。
4. 文本:drawText,drawBidiText,drawHighlightForText,drawLineForText。
B。路徑操作:
C。剪切操作:
D。繪圖參數(shù)狀態(tài)配置:
1. 線條:strokeThickness,strokeStyle,strokeColor,strokeColorSpace,strokePattern,strokeGradient;
2. 填充:fillRule,fillColor,fillColorSpace,fillPattern,fillGradient;
3. 品質(zhì):shouldAntialias,imageInterpolationQuality,textDrawingMode;
4. 狀態(tài):paintingDisabled;
E。繪圖參數(shù)狀態(tài)堆棧操作:
我們可以看出,繪圖操作的大部分屬性參數(shù)都是通過GraphicsContext的狀態(tài)傳遞的。
那么,從性能的角度考慮,這樣做好么,還是直接通過函數(shù)參數(shù)傳遞更好呢?
通過參數(shù)傳遞會帶來一次參數(shù)出入堆棧操作;
通過GraphicsContext的狀態(tài)傳遞,則會在每次參數(shù)變更時,進(jìn)行一次參數(shù)出入堆棧操作和保存參數(shù)到狀態(tài)的操作。
因此,當(dāng)大多數(shù)繪圖操作共用相同參數(shù)時,GraphicsContext的狀態(tài)傳遞更加高效。
當(dāng)然,還要考慮到GraphicsContext的狀態(tài)棧操作會在每次場景切換是帶來的額外開銷。