KIVY Canvas¶

2024-06-13 13:12
文章标签 canvas kivy

本文主要是介绍KIVY Canvas¶,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Canvas¶

Jump to API ⇓

Module: kivy.graphics.instructions

Added in 1.0.0

The Canvas is the root object used for drawing by a Widget. Check the class documentation for more information about the usage of Canvas.
画布是一个 基类对象 被用来以一个组件的方式画画。 检查  类文档来获取canvas画画使用的更多信息

APIHide Description ⇑

class kivy.graphics.instructions.Callback(callback=None**kwargs

Bases: kivy.graphics.instructions.Instruction

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 updating of your canvas does not occur 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.
如果你使用回调类来召唤渲染其他的工具箱方法, 你将有些问题伴随着OpenGL 内容。 OpenGL 的状态可以被其他的工具包操控, 并且项目一传播信号返回给Kivy,就会break。  你可以有一些小故障、猛撞、黑洞可能会出现等等。 为了避免那些, 你可以激活reset_context 选项。 它将设置重新设置OpenGL 内容状态来 确保Kivy渲染正确地在召唤你地回调信号之后。

Warning

The reset_context is not a full OpenGL reset. If you have issues regarding that, please contact us.
reset_context 不是一个完整的OpenGL 重新设定。 如果你有一些问题关于那, 请联系我们。

ask_update(self

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.

callback¶

Property for getting/setting func.
属性来获取/设定 功能。

reset_context¶

Set this to True if you want to reset the OpenGL context for Kivy after the callback has been called.
设置这到True 如果你想来重新设定OpenGL 内容为了Kivy在回调信号被接收后。

class kivy.graphics.instructions.Canvas(**kwargs

Bases: kivy.graphics.instructions.CanvasBase

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))

add(selfInstruction c

Append an Instruction to our list. If the canvas contains an after group, then this instruction is inserted just before the after group, which remains last. This is different from how insert() works, which can insert anywhere.

after¶

Property for getting the ‘after’ group.

ask_update(self

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.

clear(self

Clears every Instruction in the canvas, leaving it clean.

draw(self

Apply the instruction to our window.

has_after¶

Property to see if the after group has already been created.

New in version 1.7.0.

has_before¶

Property to see if the before group has already been created.

New in version 1.7.0.

opacity¶

Property to 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 has an opacity of 0.5 and a child has an opacity of 0.2, the real opacity of the child 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);

remove(selfInstruction c

class kivy.graphics.instructions.CanvasBase¶

Bases: kivy.graphics.instructions.InstructionGroup

CanvasBase provides the context manager methods for the Canvas.

class kivy.graphics.instructions.ContextInstruction(**kwargs

Bases: kivy.graphics.instructions.Instruction

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.Instruction(**kwargs

Bases: kivy.event.ObjectWithUid

usage only, don’t use it directly.

flag_data_update(self

flag_update(selfint do_parent=1

group¶

group: unicode

proxy_ref¶

Return a proxy reference to the Instruction i.e. without creating a reference of the widget. See weakref.proxy for more information.

New in version 1.7.2.

class kivy.graphics.instructions.InstructionGroup(**kwargs

Bases: kivy.graphics.instructions.Instruction

of graphics instructions. It can be used directly as follows:

blue = InstructionGroup() blue.add(Color(0, 0, 1, 0.2)) blue.add(Rectangle(pos=self.pos, size=(100, 100)))

green = InstructionGroup() green.add(Color(0, 1, 0, 0.4)) green.add(Rectangle(pos=(100, 100), size=(100, 100)))

# Here, self should be a Widget or subclass [self.canvas.add(group) for group in [blue, green]]

add(selfInstruction c

Add a new Instruction to our list.

children¶

children: list

clear(self

Remove all the Instructions.

get_group(selfunicode groupname

Return an iterable for all the Instructions with a specific group name.

indexof(selfInstruction c

insert(selfint indexInstruction c

Insert a new Instruction into our list at index.

length(self

remove(selfInstruction c

Remove an existing Instruction from our list.

remove_group(selfunicode groupname

Remove all Instructions with a specific group name.

class kivy.graphics.instructions.RenderContext(*args**kwargs

Bases: kivy.graphics.instructions.Canvas

  • The vertex shader

  • The fragment shader

  • The default texture

  • The state stack (color, texture, matrix…)

shader¶

Return the shader attached to the render context.

use_parent_frag_modelview¶

If True, the parent fragment modelview matrix will be used.

New in version 1.10.1: rc = RenderContext(use_parent_frag_modelview=True)

use_parent_modelview¶

If True, the parent modelview matrix will be used.

New in version 1.7.0.

Before:

rc['modelview_mat'] = Window.render_context['modelview_mat']

Now:

rc = RenderContext(use_parent_modelview=True)

use_parent_projection¶

If True, the parent projection matrix will be used.

New in version 1.7.0.

Before:

rc['projection_mat'] = Window.render_context['projection_mat']

Now:

rc = RenderContext(use_parent_projection=True)

class kivy.graphics.instructions.VertexInstruction(**kwargs

Bases: kivy.graphics.instructions.Instruction

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: 'mylogo.png'pos: self.possize: self.size

Note

The filename will be searched for using 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, v + h, u, v + h]

You can pass your own texture coordinates if you want to achieve fancy effects.

Warning

The default values just mentioned 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 Imagetexture = 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.

这篇关于KIVY Canvas¶的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1057399

相关文章

影响画布微信小程序canvas及skyline和webview用户界面布局的关键流程

影响微信小程序画布canvas及skyline和webview用户界面布局的关键流程 目录 影响微信小程序画布canvas及skyline和webview用户界面布局的关键流程 一、微信小程序canvas开发流程 1.1、官方指南 1.2、客制化开发 第一步:在 WXML 中添加 canvas 组件 第二步:获取 Canvas 对象和渲染上下文 第三步 画布#ID选择器执行回调——

Android canvas save restore saveLayer的异同点

一、基础操作 drawText、drawRect、drawColor等 对于这些基础操作,相信每一个安卓开发者都能说上个一二点出来,这些就不多做介绍,api 工程师必备技能之一。 在进阶之前,先回答这个问题:    问:canvas既然大家都理解为画布,那如果先在画布上绘制了某些内容,然后再canvas.rotate旋转了画布,为什么这些已经绘制在画布上的内容不会跟随着旋转?    答:由此可

自定义View-Canvas

转载自:https://www.jianshu.com/p/f69873371763 Android Canvas 方法总结 简介 在自定义 View的时候,我们经常需要绘制一些自己想要的效果。 这里就需要使用Canvas对象。 下面将Canvas对象常用方法做个笔记,方便记忆。 对Canvas进行操作 对Canvas的一系列操作,是指对Canvas进行旋转、平移、缩放等操作。 这些操作可以

canvas-实现放大镜效果

canvas-实现放大镜效果 目录 文章目录 前言推荐阅读代码展示结果展示 前言 本文为canvas实现放大镜逻辑简单,适合作为基础项目练手 推荐阅读 《H5 canvas核心技术》 代码展示 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Comp

canvas-原生js实现时钟绘图效果

canvas-原生js实现时钟绘图效果 目录 文章目录 前言代码效果查看 前言 本文为canvas实现时钟效果可直接复制使用 代码 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta n

js-基于AudioContext在canvas上显示声音波形

js-基于AudioContext在canvas上显示声音波形 目录 文章目录 前言效果展示代码展示`index.html``Aud.js` 前言 从ES7后开始启用AudioContex常用API是:createScriptProcessor, onaudioprocess, getChannelData注意:onaudioprocess已经废弃,开始改用Analyse

【Canvas与纹饰】环形小蜜蜂纹饰

【成图】 【代码】 <!DOCTYPE html><html lang="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><head><title>环形小蜜蜂纹饰</title><style type="text/css">.centerlize{margin:0 auto;

Web前端 lucky-canvas【大转盘 九宫格 老虎机】抽奖插件(适用JS/TS、Vue、React、微信小程序、Uniapp和Taro)

Web前端 lucky-canvas 抽奖插件(JS/TS、Vue、React、微信小程序、Uniapp和Taro) 基于 JS + Canvas 实现的【大转盘 & 九宫格 & 老虎机】抽奖,致力于为 WEB 前端提供一个功能强大且专业可靠的营销组件,只需要通过简单配置即可实现自由化定制,帮助你快速的完成产品需求 自由配置 奖品 / 文字 / 图片 / 颜色 / 按钮均可自由配置;支持同步

Canvas加动画,实现火柴人跳绳效果

效果 涉及到的知识 1、canvas 2、path和二阶贝塞尔曲线 3、bitmap绘制 canvas 先引用google官方: The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas t

html5学习canvas实例之时钟

<html><head><title>html5-01</title><meta http-equiv="content-type" content="text/html" charset="utf-8"><script language="javascript">window.οnlοad=function (){var canvas=document.getElementById("canva