Canvas¶
The Canvas is the root object used for drawing by a Widget. Check the class documentation for more information about the usage of Canvas.
- class kivy.graphics.instructions.Instruction¶
Bases: object
Represents the smallest instruction available. This class is for internal usage only, don’t use it directly.
- class kivy.graphics.instructions.InstructionGroup¶
Bases: kivy.graphics.instructions.Instruction
Group of Instruction. Adds the possibility of adding and removing graphics instruction.
- add()¶
Add a new Instruction to our list.
- clear()¶
Remove all the Instruction.
- get_group()¶
Return an iterable with all the Instruction with a specific group name.
- insert()¶
Insert a new Instruction in our list at index.
- remove()¶
Remove an existing Instruction from our list.
- remove_group()¶
Remove all Instruction with a specific group name.
- class kivy.graphics.instructions.ContextInstruction¶
Bases: kivy.graphics.instructions.Instruction
The ContextInstruction class is the base for the creation of instructions that don’t have a direct visual representation, but instead modify the current Canvas’ state, e.g. texture binding, setting color parameters, matrix manipulation and so on.
- class kivy.graphics.instructions.VertexInstruction¶
Bases: kivy.graphics.instructions.Instruction
The VertexInstruction class is the base for all graphics instructions that have a direct visual representation on the canvas, such as Rectangles, Triangles, Lines, Ellipse and so on.
- source¶
This property represents the filename to load the texture from. If you want to use an image as source, do it like this:
with self.canvas: Rectangle(source='mylogo.png', pos=self.pos, size=self.size)
Here’s the equivalent in Kivy language:
<MyWidget>: canvas: Rectangle: source: 'myfilename.png' pos: self.pos size: self.size
Note
The filename will be searched with the kivy.resources.resource_find() function.
- tex_coords¶
This property represents the texture coordinates used for drawing the vertex instruction. The value must be a list of 8 values.
A texture coordinate has a position (u, v), and a size (w, h). The size can be negative, and would represent the ‘flipped’ texture. By default, the tex_coords are:
[u, v, u + w, v, u + w, y + h, u, y + h]
You can pass your own texture coordinates, if you want to achieve fancy effects.
Warning
The default value as mentioned before can be negative. Depending on the image and label providers, the coordinates are flipped vertically, because of the order in which the image is internally stored. Instead of flipping the image data, we are just flipping the texture coordinates to be faster.
- texture¶
Property that represents the texture used for drawing this Instruction. You can set a new texture like this:
from kivy.core.image import Image texture = Image('logo.png').texture with self.canvas: Rectangle(texture=texture, pos=self.pos, size=self.size)
Usually, you will use the source attribute instead of the texture.
- class kivy.graphics.instructions.Canvas¶
Bases: kivy.graphics.instructions.CanvasBase
The important Canvas class. Use this class to add graphics or context instructions that you want to be used for drawing.
Note
The Canvas supports Python’s with statement and its enter & exit semantics.
Usage of a canvas without the with statement:
self.canvas.add(Color(1., 1., 0)) self.canvas.add(Rectangle(size=(50, 50)))
Usage of a canvas with Python’s with statement:
with self.canvas: Color(1., 1., 0) Rectangle(size=(50, 50))
- after¶
Property for getting the ‘after’ group.
- ask_update()¶
Inform the canvas that we’d like it to update on the next frame. This is useful when you need to trigger a redraw due to some value having changed for example.
- before¶
Property for getting the ‘before’ group.
- draw()¶
Apply the instruction on our window.
- opacity¶
Property for get/set the opacity value of the canvas.
New in version 1.4.1.
The opacity attribute controls the opacity of the canvas and its children. Be careful, it’s a cumulative attribute: the value is multiplied to the current global opacity, and the result is applied to the current context color.
For example: if your parent have an opacity of 0.5, and one children have an opacity of 0.2, the real opacity of the children will be 0.5 * 0.2 = 0.1.
Then, the opacity is applied on the shader as:
frag_color = color * vec4(1.0, 1.0, 1.0, opacity);
- class kivy.graphics.instructions.RenderContext¶
Bases: kivy.graphics.instructions.Canvas
The render context stores all the necessary information for drawing, i.e.:
- The vertex shader
- The fragment shader
- The default texture
- The state stack (color, texture, matrix...)
- class kivy.graphics.instructions.Callback¶
Bases: kivy.graphics.instructions.Instruction
New in version 1.0.4.
A Callback is an instruction that will be called when the drawing operation is performed. When adding instructions to a canvas, you can do this:
with self.canvas: Color(1, 1, 1) Rectangle(pos=self.pos, size=self.size) Callback(self.my_callback)
The definition of the callback must be:
def my_callback(self, instr): print 'I have been called!'
Warning
Note that if you perform many and/or costly calls to callbacks, you might potentially slow down the rendering performance significantly.
The drawing of your canvas can not happen until something new happens. From your callback, you can ask for an update:
with self.canvas: self.cb = Callback(self.my_callback) # then later in the code self.cb.ask_update()
If you use the Callback class to call rendering methods of another toolkit, you will have issues with the OpenGL context. The OpenGL state may have been manipulated by the other toolkit, and as soon as program flow returns to Kivy, it will just break. You can have glitches, crashes, black holes might occur, etc. To avoid that, you can activate the reset_context option. It will reset the OpenGL context state to make Kivy’s rendering correct, after the call to your callback.
Warning
The reset_context is not a full OpenGL reset. If you have issues regarding that, please contact us.
- ask_update()¶
Inform the parent canvas that we’d like it to update on the next frame. This is useful when you need to trigger a redraw due to some value having changed for example.
New in version 1.0.4.
- reset_context¶
Set this to True if you want to reset the OpenGL context for Kivy after the callback has been called.