How to use the vispy.app.Canvas function in vispy

To help you get started, we’ve selected a few vispy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vispy / vispy / examples / tutorial / gloo / textured_quad.py View on Github external
uniform sampler2D texture;
    varying vec2 v_texcoord;
    void main()
    {
        gl_FragColor = texture2D(texture, v_texcoord);
    } """


def checkerboard(grid_num=8, grid_size=32):
    row_even = grid_num // 2 * [0, 1]
    row_odd = grid_num // 2 * [1, 0]
    Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
    return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)


class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, size=(512, 512), title='Textured quad',
                            keys='interactive')

        # Build program & data
        self.program = Program(vertex, fragment, count=4)
        self.program['position'] = [(-1, -1), (-1, +1),
                                    (+1, -1), (+1, +1)]
        self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]
        self.program['texture'] = checkerboard()

        gloo.set_viewport(0, 0, *self.physical_size)

        self.show()

    def on_draw(self, event):
github vispy / vispy / examples / tutorial / gloo / colored_quad.py View on Github external
def __init__(self):
        app.Canvas.__init__(self, size=(512, 512), title='Colored quad',
                            keys='interactive')

        # Build program & data
        self.program = Program(vertex, fragment, count=4)
        self.program['color'] = [(1, 0, 0, 1), (0, 1, 0, 1),
                                 (0, 0, 1, 1), (1, 1, 0, 1)]
        self.program['position'] = [(-1, -1), (-1, +1),
                                    (+1, -1), (+1, +1)]

        gloo.set_viewport(0, 0, *self.physical_size)

        self.show()
github vispy / vispy / examples / tutorial / app / simple_wx.py View on Github external
# Copyright (c) 2015, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
This is a very minimal example that opens a window and makes the background
color to change from black to white to black ...

The wx backend is used to embed the canvas in a simple wx Frame with
a menubar.
"""

import wx
import math
from vispy import app, gloo


class Canvas(app.Canvas):
    def __init__(self, *args, **kwargs):
        app.Canvas.__init__(self, *args, **kwargs)
        self._timer = app.Timer('auto', connect=self.on_timer, start=True)
        self.tick = 0

    def on_draw(self, event):
        gloo.clear(color=True)

    def on_timer(self, event):
        self.tick += 1 / 60.0
        c = abs(math.sin(self.tick))
        gloo.set_clear_color((c, c, c, 1))
        self.update()


class TestFrame(wx.Frame):
github sjdv1982 / seamless / lib / hive / canvashive.py View on Github external
def __init__(self, *args, **kwargs):
        self._program = None
        self._hive = ref(hive.get_run_hive())
        self._hive()._canvas = ref(self)
        app.Canvas.__init__(self,*args, **kwargs)

        # Enable blending
        gloo.set_state(blend=True, clear_color='black',
                       blend_func=('src_alpha', 'one'))

        gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])
        self._timer = app.Timer('auto', connect=self.update, start=True)
        self._working = True
github vispy / vispy / examples / demo / gloo / spacy.py View on Github external
{
    float x = 2.0*gl_PointCoord.x - 1.0;
    float y = 2.0*gl_PointCoord.y - 1.0;
    float a = 0.9 - (x*x + y*y);
    a = a * min(1.0, v_pointsize/1.5);
    gl_FragColor = vec4(1.0, 1.0, 1.0, a);
}
"""

N = 100000  # Number of stars
SIZE = 100
SPEED = 4.0  # time in seconds to go through one block
NBLOCKS = 10


class Canvas(app.Canvas):

    def __init__(self):
        app.Canvas.__init__(self, title='Spacy', keys='interactive',
                            size=(800, 600))

        self.program = gloo.Program(vertex, fragment)
        self.view = np.eye(4, dtype=np.float32)
        self.model = np.eye(4, dtype=np.float32)

        self.activate_zoom()

        self.timer = app.Timer('auto', connect=self.update, start=True)

        # Set uniforms (some are set later)
        self.program['u_model'] = self.model
        self.program['u_view'] = self.view
github vispy / vispy / examples / basics / gloo / client_buffers.py View on Github external
def __init__(self):
        app.Canvas.__init__(self, keys='interactive')

        # Time
        self._t = time.time()
        self._pos = 0.0, 0.0
        self._button = None

        # Create program
        self.program = Program(VERT_SHADER, FRAG_SHADER)
        self.program['color'] = VertexBuffer(particles['color'].copy(),
                                             client=True)
        self.program['size'] = VertexBuffer(particles['size'].copy(),
                                            client=True)
github alifelab / alife_book_src / chap06_evolution / ant_simulator.py View on Github external
# -*- coding: utf-8 -*-
from os import path
import numpy as np
from vispy import app, gloo, visuals
from vispy.visuals import transforms
from vispy.util.transforms import perspective
import ipdb
from PIL import Image

GLSL_PATH = path.join(path.dirname(path.abspath(__file__)), 'glsl')
ENV_MAP_PATH = path.join(path.dirname(path.abspath(__file__)), 'img')

DEBUG_ENV = False


class AntSimulator(app.Canvas):

    def __init__(self, N, decay_rate=1.0, secretion=False):
        super(AntSimulator, self).__init__(title='Title', size=(600, 600), resizable=False, position=(0, 0), keys='interactive')

        # simulation settings
        self._N = N
        self._potential_init = np.array(Image.open(path.join(ENV_MAP_PATH, 'envmap01.png'))).astype(np.float32) / 255.
        self._potential_grid_size = self._potential_init.shape
        self._potential_decay_rate = decay_rate
        self._hormone_secretion = secretion

        self.reset()

        vert = open(path.join(GLSL_PATH, 'color_map_vert.glsl'), 'r').read()
        frag = open(path.join(GLSL_PATH, 'color_map_frag.glsl'), 'r').read()
        self.potential_render = gloo.Program(vert, frag)
github vispy / vispy / examples / demo / gloo / glsl_sandbox_cube.py View on Github external
float ty = v_texcoord.y;
    float tx = sin(ty*50.0)*0.01 + v_texcoord.x;
    gl_FragColor = texture2D(u_texture, vec2(tx, ty));
}
"""


# Read cube data
positions, faces, normals, texcoords = \
    read_mesh(load_data_file('orig/cube.obj'))
colors = np.random.uniform(0, 1, positions.shape).astype('float32')

faces_buffer = gloo.IndexBuffer(faces.astype(np.uint16))


class Canvas(app.Canvas):

    def __init__(self, **kwargs):
        app.Canvas.__init__(self, size=(400, 400), **kwargs)

        self.program = gloo.Program(VERT_CODE, FRAG_CODE)

        # Set attributes
        self.program['a_position'] = gloo.VertexBuffer(positions)
        self.program['a_texcoord'] = gloo.VertexBuffer(texcoords)

        self.program['u_texture'] = gloo.Texture2D(load_crate())

        # Handle transformations
        self.init_transforms()

        self.apply_zoom()
github vispy / vispy / examples / basics / visuals / image_transforms.py View on Github external
def __init__(self):
        vispy.app.Canvas.__init__(self, keys='interactive', size=(800, 800))
        
        # Create 4 copies of an image to be displayed with different transforms
        image = get_image()
        self.images = [visuals.ImageVisual(image, method='impostor')
                       for i in range(4)]
        
        # Transform all images to a standard size / location (because
        # get_image() might return unexpected sizes)
        s = 100. / max(self.images[0].size)
        tx = 0.5 * (100 - (self.images[0].size[0] * s))
        ty = 0.5 * (100 - (self.images[0].size[1] * s))
        base_tr = STTransform(scale=(s, s), translate=(tx, ty))
        
        self.images[0].transform = (STTransform(scale=(30, 30),
                                                translate=(600, 600)) *
                                    SineTransform() *
github vispy / vispy / examples / demo / gloo / graph.py View on Github external
def __init__(self, **kwargs):
        # Initialize the canvas for real
        app.Canvas.__init__(self, keys='interactive', size=(512, 512),
                            **kwargs)
        ps = self.pixel_scale
        self.position = 50, 50

        n = 100
        ne = 100
        data = np.zeros(n, dtype=[('a_position', np.float32, 3),
                                  ('a_fg_color', np.float32, 4),
                                  ('a_bg_color', np.float32, 4),
                                  ('a_size', np.float32, 1),
                                  ('a_linewidth', np.float32, 1),
                                  ])
        edges = np.random.randint(size=(ne, 2), low=0,
                                  high=n).astype(np.uint32)
        data['a_position'] = np.hstack((.25 * np.random.randn(n, 2),
                                       np.zeros((n, 1))))