.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/text_snake.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_feature_demo_text_snake.py: Text snake ========== Example showing a text with picking and custom layout. In this example we use a little trick by splitting the text in one block for each word, and use ``direction="ltr-ltr"``, which means both text and blocks are layed out from left to right. Then in the animate function we update the y-positions to make the blocks move in a wave. .. GENERATED FROM PYTHON SOURCE LINES 13-77 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_text_snake_001.webp :alt: text snake :srcset: /_gallery/feature_demo/images/sphx_glr_text_snake_001.webp :class: sphx-glr-single-img .. code-block:: Python from time import perf_counter from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx import numpy as np renderer = gfx.renderers.WgpuRenderer(RenderCanvas()) scene = gfx.Scene() # Create the text s = "Lorem **ipsum** dolor sit amet, consectetur *adipiscing* elit, sed do eiusmod tempor ..." text1 = gfx.Text( markdown=s.split(), font_size=10, direction="ltr-ltr", paragraph_spacing=0.3, material=gfx.TextMaterial(color="#fff", pick_write=True), ) scene.add(text1) # Camera and controller camera = gfx.PerspectiveCamera(70, 16 / 9) camera.show_object(text1) controller = gfx.OrbitController(camera, register_events=renderer) # Put the scene in as box, with lights, for visual appeal. box = gfx.Mesh( gfx.box_geometry(1000, 1000, 1000), gfx.MeshPhongMaterial(), ) scene.add(box) scene.add(gfx.AmbientLight()) light = gfx.DirectionalLight(0.2) light.local.position = (0, 0, 1) scene.add(camera.add(light)) @renderer.add_event_handler("pointer_down") def handle_event(event): info = event.pick_info if info["world_object"] is None: print("miss") else: for key, val in info.items(): print(key, "=", val) def animate(): yy = text1.geometry.positions.data[:, 1] tt = np.linspace(0, 10, len(yy)) yy[:] = 10 * np.sin(tt + 2 * perf_counter()) text1.geometry.positions.update_full() renderer.render(scene, camera) renderer.request_draw() if __name__ == "__main__": renderer.request_draw(animate) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.155 seconds) .. _sphx_glr_download__gallery_feature_demo_text_snake.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: text_snake.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: text_snake.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: text_snake.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html