.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/paint_to_texture.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_paint_to_texture.py: Paint to a texture ================== This example shows an application that allows the user to paint an image onto a texture. This also demonstrates a custom controller config, that only allows horizontal panning over the texture, using shift+mouse or scrolling. .. GENERATED FROM PYTHON SOURCE LINES 11-64 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_paint_to_texture_001.webp :alt: paint to texture :srcset: /_gallery/feature_demo/images/sphx_glr_paint_to_texture_001.webp :class: sphx-glr-single-img .. code-block:: Python from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx import numpy as np renderer = gfx.renderers.WgpuRenderer(RenderCanvas()) scene = gfx.Scene() scene.add(gfx.Background.from_color("#cde")) # Create texture to draw to. We set the force_contiguous flag because we will sync the texture regularly data = np.zeros((100, 500), np.uint8) tex = gfx.Texture(data, dim=2, force_contiguous=True) image = gfx.Image( gfx.Geometry(grid=tex), gfx.ImageBasicMaterial(clim=(0, 200), pick_write=True), ) scene.add(image) camera = gfx.OrthographicCamera() camera.show_rect(-5, 105, -5, 105) @image.add_event_handler("pointer_down", "pointer_move") def pick(event): if event.modifiers: return if 1 in event.buttons or 2 in event.buttons: info = event.pick_info if "index" in info: x, y = info["index"] if 1 in event.buttons: tex.data[y, x] = min(200, tex.data[y, x] + 50) else: tex.data[y, x] = max(0, tex.data[y, x] - 50) tex.update_indices(x, y, 0) renderer.request_draw() controller = gfx.PanZoomController(camera, register_events=renderer) controller.controls = { "shift+mouse1": ("pan", "drag", (1, 0)), "wheel": ("pan", "push", (-0.05, 0)), "mouse3": ("quickzoom", "peek", 2), } if __name__ == "__main__": renderer.request_draw(lambda: renderer.render(scene, camera)) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.197 seconds) .. _sphx_glr_download__gallery_feature_demo_paint_to_texture.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: paint_to_texture.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: paint_to_texture.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: paint_to_texture.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