GraphicsContext 定義了繪圖操作環境。
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。繪圖參數狀態配置:
1. 線條:strokeThickness,strokeStyle,strokeColor,strokeColorSpace,strokePattern,strokeGradient;
2. 填充:fillRule,fillColor,fillColorSpace,fillPattern,fillGradient;
3. 品質:shouldAntialias,imageInterpolationQuality,textDrawingMode;
4. 狀態:paintingDisabled;
E。繪圖參數狀態堆棧操作:
我們可以看出,繪圖操作的大部分屬性參數都是通過GraphicsContext的狀態傳遞的。
那么,從性能的角度考慮,這樣做好么,還是直接通過函數參數傳遞更好呢?
通過參數傳遞會帶來一次參數出入堆棧操作;
通過GraphicsContext的狀態傳遞,則會在每次參數變更時,進行一次參數出入堆棧操作和保存參數到狀態的操作。
因此,當大多數繪圖操作共用相同參數時,GraphicsContext的狀態傳遞更加高效。
當然,還要考慮到GraphicsContext的狀態棧操作會在每次場景切換是帶來的額外開銷。