Skip to content

Instantly share code, notes, and snippets.

@almarklein
Created August 28, 2014 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almarklein/0e3f2c7916ce8a3921b4 to your computer and use it in GitHub Desktop.
Save almarklein/0e3f2c7916ce8a3921b4 to your computer and use it in GitHub Desktop.
Vispy pres minimal
import itertools
import os
import hashlib
import numpy as np
import vispy
from vispy import app
from vispy import scene
from vispy import gloo
# Shorthands
from vispy.scene.visuals import Text
from vispy.scene.visuals import Image
class Presentation(scene.SceneCanvas):
""" Presentation canvas.
"""
def __init__(self, title='A presentation', **kwargs):
self._current_slide = 0
self._slides = []
scene.SceneCanvas.__init__(self, title=title, bgcolor='white', **kwargs)
self.size = 800, 600
refresh = lambda x: self.update()
self._timer = app.Timer(1/30, connect=refresh, start=True)
# Create toplevel viewbox that we can use to see whole presentation
self._top_vb = scene.widgets.ViewBox(parent=self.scene, border_color=None)
#self._top_vb.camera = scene.cameras.PerspectiveCamera('perspective')
#self._top_vb.camera.fov = 80
self._top_vb.camera = scene.cameras.TurntableCamera()
self._top_vb.camera.center = -2000, -200, -2000
self._top_vb.camera.width = 15000
self._top_vb.camera.azimuth = -60
self._top_vb.camera._update_camera_pos() # todo: camera must do this
self._top_vb.size = self.size
self.show_text = True
def on_initialize(self, event):
gloo.set_state('translucent',
#blend=True,
#blend_func=('src_alpha', 'one_minus_src_alpha'),
depth_test=False)
def on_resize(self, event):
gloo.set_viewport(0, 0, *self.size)
for slide in self._slides:
expand = self.size[0] / self.size[1]
slide.camera.rect = -0.5*(expand-1), 0, expand, 1
slide.size = self.size
def add_slide(self, title=None, cls=None):
# todo: make _children a list or ordered set
# Create super viewbox
super_vb = scene.widgets.ViewBox(parent= self._top_vb.scene, border_color='b')
super_vb.transform = scene.transforms.AffineTransform()
for i in range(len(self._slides)):
super_vb.transform.rotate(-10, (0, 1, 0))
super_vb.transform.translate((1000, 0))
super_vb.size = self.size
super_vb.camera = scene.cameras.BaseCamera() # Null camera
# Create slide itself
cls = cls or Slide
slide = cls(parent=super_vb.scene, border_color=None)
slide.camera.interactive = False
slide.camera.invert_y = False
expand = self.size[0] / self.size[1]
slide.camera.rect = 0, 0, 1, 1 # overwritten on resize
self._slides.append(slide)
slide.size = self.size
slide.clip_method = 'viewport'
slide.events.add(key_press=None)
slide.scene._systems['draw'] = DrawingSystem()
if title:
slide.set_title(title)
# Select this slide now?
if len(self._slides) == 1:
self.scene = self._slides[-1].superviewbox.scene
return slide
def on_key_press(self, event):
if event.key == vispy.keys.HOME:
self.scene = self._top_vb.parent
elif event.key in (vispy.keys.LEFT, vispy.keys.RIGHT, vispy.keys.END, vispy.keys.SPACE):
for child in self._slides[self._current_slide].scene.children:
if hasattr(child, 'on_leave_slide'): child.on_leave_slide()
M = {vispy.keys.LEFT:-1, vispy.keys.RIGHT:1, vispy.keys.SPACE:1}
self._current_slide += M.get(event.key, 0)
self._current_slide = max(min(self._current_slide, len(self._slides)-1), 0)
self.scene = self._slides[self._current_slide].superviewbox.scene
for child in self._slides[self._current_slide].scene.children:
if hasattr(child, 'on_enter_slide'): child.on_enter_slide()
elif event.text.lower() == 'f':
self.fullscreen = not self.fullscreen
self.on_resize(None)
else:
self._slides[self._current_slide].events.key_press(event)
self.show_text = self.scene is not self._top_vb.parent
class Slide(scene.widgets.ViewBox):
""" Representation of a slide in the presentation in the form of a viewbox.
"""
@property
def superviewbox(self):
return self.parent.parent
def set_title(self, text):
text = Text(text, parent=self.scene, pos=(0.5, 0.1),
font_size=26, bold=True, color='#002233',
font_manager=font_manager)
return text
def add_text(self, text, pos, size=20, anchor_x='left', **kwargs):
text = Text(text, parent=self.scene, pos=pos, font_size=size,
anchor_x=anchor_x, anchor_y='center',
font_manager=font_manager, **kwargs)
return text
class DrawingSystem(scene.systems.DrawingSystem):
def _process_entity(self, event, entity, force_recurse=False):
if (not pres.show_text) and isinstance(entity, scene.visuals.Text):
return
else:
scene.systems.DrawingSystem._process_entity(self, event, entity, force_recurse)
# For more efficient fonts
font_manager = scene.visuals.text.text.FontManager()
pres = Presentation(show=True)
## Introduction
slide = pres.add_slide()
# slide.add_image('vispylogo.png', (0.42, 0.05), 0.16)
slide.add_text("Introducing Vispy's high level modules:", (0.5, 0.5), 24, 'center', color='k', bold=True)
slide.add_text("easy yet powerful visualization for everyone", (0.5, 0.6), 24, 'center', color='k', bold=True)
slide.add_text("EuroScipy - August 29, 2014", (0.5, 0.8), 20, 'center', color='b', bold=True)
t1 = slide.add_text("Almar Klein", (0.5, 0.9), 20, 'center', color='k')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment