File: text_scatter.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (80 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# vispy: gallery 30
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
"""
Demonstration of ability to scatter text parts and assign different colors.
"""

import sys
from vispy import app, gloo, visuals
import numpy as np


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

        self.n_iterations = 0

        self.text_parts = [
            'This',
            'is',
            'a',
            'multicolored',
            'scattered',
            'text']

        self.x = 100 + np.arange(len(self.text_parts))*100
        self.y = 400 + (np.sin(2 * np.pi * (self.x/self.x[-1]))*100)

        self.text_positions = np.c_[self.x, self.y]

        color = np.ones((len(self.text_parts), 4), dtype=np.float32)
        color[:, 0] = np.linspace(0, 1, len(self.text_parts))
        color[:, 1] = color[::-1, 0]

        self.colors = color

        self.text = visuals.TextVisual(self.text_parts, bold=True,
                                       pos=self.text_positions,
                                       color=self.colors)

    def on_draw(self, event):
        gloo.clear(color='white')
        gloo.set_viewport(0, 0, *self.physical_size)
        self.text.draw()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)
        self.text.transforms.configure(canvas=self, viewport=vp)


if __name__ == '__main__':
    c = Canvas()
    c.show()

    def update(ev):
        x = c.text.pos[:, 0]
        y = 400 + (np.sin(2 * np.pi * ((x/x[-1]))+(c.n_iterations/5))*100)

        color = np.ones((len(c.text_parts), 4), dtype=np.float32)
        color[:, 0] = np.linspace(0, 1, len(c.text_parts))
        color[:, 1] = color[::-1, 0]
        color = np.roll(color, c.n_iterations // len(color), axis=0)
        c.text.pos = np.c_[x, y]
        c.text.color = color
        c.update()
        c.n_iterations += 1

    timer = app.Timer()
    timer.connect(update)
    timer.start(0.1)

    if sys.flags.interactive != 1:
        app.run()