.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/panzoom_camera.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_panzoom_camera.py: Panzoom Camera ============== Example showing orbit camera controller. Press 's' to save the state, and press 'l' to load the last saved state. .. GENERATED FROM PYTHON SOURCE LINES 10-74 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_panzoom_camera_001.webp :alt: panzoom camera :srcset: /_gallery/feature_demo/images/sphx_glr_panzoom_camera_001.webp :class: sphx-glr-single-img .. code-block:: Python import imageio.v3 as iio from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx import numpy as np canvas = RenderCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() axes = gfx.AxesHelper(size=250) scene.add(axes) dark_gray = np.array((169, 167, 168, 255)) / 255 light_gray = np.array((100, 100, 100, 255)) / 255 background = gfx.Background.from_color(light_gray, dark_gray) scene.add(background) im = iio.imread("imageio:astronaut.png") tex = gfx.Texture(im, dim=2) geometry = gfx.plane_geometry(512, 512) material = gfx.MeshBasicMaterial(map=tex) plane = gfx.Mesh(geometry, material) scene.add(plane) camera = gfx.OrthographicCamera(512, 512) camera.show_object(scene) controller = gfx.PanZoomController(camera, register_events=renderer) # initial camera state camera_state = camera.get_state() def on_key_down(event): if event.key not in ["s", "l", "r"]: return global camera_state # without disabling the controller any in-progress action # during this function call will overwrite the camera state controller.enabled = False if event.key == "s": camera_state = camera.get_state() elif event.key == "l": camera.set_state(camera_state) elif event.key == "r": camera.show_object(scene) controller.enabled = True canvas.request_draw() # not required if you are continuously rendering renderer.add_event_handler(on_key_down, "key_down") if __name__ == "__main__": canvas.request_draw(lambda: renderer.render(scene, camera)) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.292 seconds) .. _sphx_glr_download__gallery_feature_demo_panzoom_camera.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: panzoom_camera.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: panzoom_camera.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: panzoom_camera.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