官术网_书友最值得收藏!

Chapter 2. Graphics – The Canvas

Any Kivy Widget contains a Canvas object. Be careful with the name because it might be confusing:

Note

A Canvas is not the place where we draw. Instead, a Canvas contains all the drawing instructions that will render the graphical representation of the Widget.

The coordinate space refers to the place where we draw, what you might have thought to be the canvas is in the first place. In this chapter, you are going to learn how to draw and manipulate the representation of the widgets through the instructions that we add to the Canvas instances:

  • Draw basic geometric shapes (straight and curve lines, ellipses and polygons) through vertex instructions
  • Using colors and rotating, translating, and scaling the coordinate space through the context instructions
  • The difference between vertex and context instructions and how they complement each other
  • The three different sets of instructions of the Canvas that we can use to modify the order of execution
  • Storing and retrieving the current coordinate space context through PushMatrix and PopMatrix

Any Widget contains its own Canvas but all of them share the same coordinate space. This has consequences; for example, if we add a Rotate instruction to a specific Canvas (let's say the Canvas of a Button); it will also affect all the subsequent graphic instructions that are going to be displayed in the coordinate space. It doesn't matter if the graphics belong to canvases of different widgets. A context instruction changes the context of the coordinate space and we are going to learn strategies to control this.

Another important concept related to this is the Widget. A Widget is pretty much a place marker (with its position and size), but not necessarily a placeholder because the instructions of the Canvas of a Widget are not restricted to the specific area of the Widget, but to the whole coordinate space. At the same time, the coordinate space is not even restricted to the visual space of the Kivy window and this might also create some difficulties.

In this chapter, you will learn the available statements to draw and change the context of the coordinate space. More importantly, you will understand the canvas as a set of instructions, the implications of sharing a coordinate space, and the concept of widgets as place markers. In the last section of the chapter, we will illustrate the acquired knowledge within the comic creator. Here, you will also learn the most common technique to deal with the problem of sharing the same coordinate space. By the end, we will be on complete control of the graphics that are displayed on the screen.

主站蜘蛛池模板: 嘉峪关市| 和田县| 彭泽县| 霸州市| 昭苏县| 育儿| 张家口市| 东城区| 辉县市| 鄂伦春自治旗| 铁岭县| 阿鲁科尔沁旗| 麻江县| 桂林市| 太康县| 宜良县| 开原市| 武夷山市| 德江县| 稷山县| 苍山县| 保山市| 万州区| 共和县| 长丰县| 瑞昌市| 乐安县| 铁岭县| 大丰市| 湖口县| 岢岚县| 济阳县| 湘阴县| 龙门县| 余江县| 绥中县| 和硕县| 宁晋县| 西吉县| 泗阳县| 汝州市|