-
Notifications
You must be signed in to change notification settings - Fork 45
Document GraphicsContext state #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| Kiva State | ||
| ========== | ||
| Kiva is a "stateful" drawing API. What this means is that the graphics context | ||
| has a collection of state which affects the results of its drawing actions. | ||
| Furthermore, Kiva enables this state to be managed with a stack such that state | ||
| can be "pushed" onto the stack before making some temporary changes and then | ||
| "popped" off the stack to restore the state to a version which no longer | ||
| includes those changes. | ||
|
|
||
| State Components | ||
| ---------------- | ||
| Here is a list of all the pieces of state tracked by a Kiva graphics context, | ||
| along with the methods which operate on them: | ||
|
|
||
| * Affine transformation (:py:meth:`translate_ctm`, :py:meth:`rotate_ctm`, | ||
| :py:meth:`scale_ctm`, :py:meth:`concat_ctm`, :py:meth:`set_ctm`, | ||
| :py:meth:`get_ctm`) | ||
| * Clipping (:py:meth:`clip_to_rect`, :py:meth:`clip_to_rects`, :py:meth:`clip`, | ||
| :py:meth:`even_odd_clip`) | ||
| * Fill color (:py:meth:`set_fill_color`, :py:meth:`get_fill_color`, | ||
| :py:meth:`linear_gradient`, :py:meth:`radial_gradient`) | ||
| * Stroke color (:py:meth:`set_stroke_color`, :py:meth:`get_stroke_color`) | ||
| * Line width (:py:meth:`set_line_width`) | ||
| * Line join style (:py:meth:`set_line_join`) | ||
| * Line cap style (:py:meth:`set_line_cap`) | ||
| * Line dashing (:py:meth:`set_line_dash`) | ||
| * Global transparency (:py:meth:`set_alpha`, :py:meth:`get_alpha`) | ||
| * Anti-aliasing (:py:meth:`set_antialias`, :py:meth:`get_antialias`) | ||
| * Miter limit (:py:meth:`set_miter_limit`) | ||
| * Flatness (:py:meth:`set_flatness`) | ||
| * Image interpolation (:py:meth:`set_image_interpolation`, :py:meth:`get_image_interpolation`) | ||
|
jwiggins marked this conversation as resolved.
|
||
|
|
||
| State Stack Management | ||
| ---------------------- | ||
| Graphics context instances have two methods for saving and restoring the state, | ||
| :py:meth:`save_state` ("push") and :py:meth:`restore_state` ("pop"). That said, | ||
| it isn't recommended practice to call the methods directly. Instead, you can | ||
| treat the graphics context object as a | ||
| `context manager <https://docs.python.org/3/library/stdtypes.html#typecontextmanager>`_ | ||
| and use the ``with`` keyword to create a block of code where the graphics state | ||
| is temporarily modified. Using the context manager approach provides safety from | ||
| "temporary" modifications becoming permanent if an uncaught exception is raised | ||
| while drawing. | ||
|
|
||
| In Enable and Chaco, it is frequently the case that a graphics context instance | ||
| will be passed into a method for the purpose of some drawing. Because it is not | ||
| reasonable to push the responsibility of state management "up" the call stack, | ||
| the onus is on the code making state modifications to do them safely so that | ||
| other changes don't leak into other code. | ||
|
|
||
| **Well behaved code should take care to only modify graphics state inside a** | ||
| ``with`` **block**. | ||
|
|
||
| Example | ||
| ------- | ||
| .. image:: images/state_ex.png | ||
| :width: 300 | ||
| :height: 300 | ||
|
|
||
| First, the whole example: | ||
|
|
||
| .. literalinclude:: state_ex.py | ||
| :linenos: | ||
|
|
||
| The first part sets up the default graphics state. Here, that includes a scale | ||
| of 2 in X and Y, a translation of (150, 150) which is affected by the | ||
| preceeding scale transformation, and some line properties: stroke color, width, | ||
| join, and cap: | ||
|
|
||
| .. literalinclude:: state_ex.py | ||
| :lines: 7-13 | ||
| :linenos: | ||
| :lineno-match: | ||
|
|
||
| Then in a loop, we draw twice (the two :py:meth:`stroke_path` calls). The first | ||
| draw uses a ``with`` block to temporarily modify the drawing state. It adds more | ||
| affine transformations: a rotate and a translate. It also changes some line | ||
| properties: stroke color, width, and cap. A rectangle is then added to the | ||
| current path and stroked. | ||
|
|
||
| .. literalinclude:: state_ex.py | ||
| :lines: 17-24 | ||
| :linenos: | ||
| :lineno-match: | ||
|
|
||
| After leaving the first ``with`` block, the state is now restored to its | ||
| default. A new ``with`` block is entered and the current transformation matrix | ||
| is modified with the same rotation as the first drawing block, but a | ||
| *different* translation is applied. The line properties are unchanged | ||
| and so use the defaults set at the top. | ||
|
|
||
| .. literalinclude:: state_ex.py | ||
| :lines: 26-31 | ||
| :linenos: | ||
| :lineno-match: | ||
|
jwiggins marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import math | ||
| from kiva import CAP_ROUND, CAP_SQUARE, JOIN_ROUND | ||
| from kiva.image import GraphicsContext | ||
|
|
||
| gc = GraphicsContext((600, 600)) | ||
|
|
||
| gc.scale_ctm(2, 2) | ||
| gc.translate_ctm(150, 150) | ||
|
|
||
| gc.set_stroke_color((0.66, 0.88, 0.66)) | ||
| gc.set_line_width(7.0) | ||
| gc.set_line_join(JOIN_ROUND) | ||
| gc.set_line_cap(CAP_SQUARE) | ||
|
|
||
| for i in range(0, 12): | ||
| theta = i*2*math.pi / 12.0 | ||
| with gc: | ||
| gc.rotate_ctm(theta) | ||
| gc.translate_ctm(105, 0) | ||
| gc.set_stroke_color((1 - (i / 12), math.fmod(i / 6, 1), i / 12)) | ||
| gc.set_line_width(10.0) | ||
| gc.set_line_cap(CAP_ROUND) | ||
| gc.rect(0, 0, 25, 25) | ||
| gc.stroke_path() | ||
|
|
||
| with gc: | ||
| gc.rotate_ctm(theta) | ||
| gc.translate_ctm(20, 0) | ||
| gc.move_to(0, 0) | ||
| gc.line_to(80, 0) | ||
| gc.stroke_path() | ||
|
|
||
| gc.save("state_ex.png") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the changes to this file seem unrelated to the main purpose of this PR, but even so the changes make sense as something we want to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it's not related. I had made a similar change in the new state example (which was copied from the compiled path example)