Pure-Elisp Interactive Canvas
egfx-canvas is the primary pure-Elisp abstraction for interactive diagrams, code maps, charts, visualizers, and lightweight 2D animations inside Emacs.
Mounting a Canvas
Section titled “Mounting a Canvas”Create and mount a live canvas in one call with egfx-canvas:
(require 'egfx)
;; Inline at point (default)(egfx-canvas :columns 64 :rows 18 :background "#0f172a")
;; Fill an Emacs window with margins, pan, and zoom(egfx-canvas :in (selected-window) :margins '(8 8 8 8) :background "#0f172a" :pan t :zoom t)
;; Pin to an overlay, frame, or custom layout callback(egfx-canvas :in my-overlay)(egfx-canvas :in (list :rect-fn #'my-rect-fn :buffer (current-buffer)))Use egfx-with-canvas for scoped teardown, or (egfx-close cv) when done. Killing the owner buffer closes the canvas and frees all native OS resources automatically.
Retained Items and Auto-Routing Connectors
Section titled “Retained Items and Auto-Routing Connectors”Wrap mutations in egfx-batch to coalesce all changes into a single GPU commit:
(let ((cv (egfx-canvas :in (selected-window) :background "#0f172a" :pan t :zoom t))) (egfx-batch cv (egfx-add cv 'parse :box :at '(60 80) :size '(180 68) :radius 10 :fill "#1e293b" :stroke "#38bdf8" :line-width 2 :text "Parser" :subtext "parser.el" :draggable t :hover-style '(:fill "#263449" :stroke "#7dd3fc") :focus-style '(:stroke "#f59e0b" :line-width 3) :on-click (lambda (ev) (message "Clicked %s" (plist-get ev :id)))) (egfx-add cv 'render :box :at '(340 120) :size '(180 68) :radius 10 :fill "#1e293b" :stroke "#22c55e" :line-width 2 :text "GPU Render" :subtext "egfx-canvas" :draggable t :hover-style '(:fill "#263449")) (egfx-connect cv 'e1 'parse 'render :curve t :arrow t)))Supported item types: :box, :circle, :line, :polyline, :curve, :triangle, :polygon, :arc, :pie, :text, and :image (:file path or raw :rgba bytes with :img-size, optional :radius, and :opacity).
Camera & Emacs-First Focus
Section titled “Camera & Emacs-First Focus”- Pass
:pan tand:zoom tfor drag-to-pan and cursor-anchored wheel/pinch zoom. - Programmatically query or animate camera coordinates via
(egfx-camera cv),(egfx-camera cv :x 0 :y 0 :zoom 1.0), or(egfx-camera cv :center-on 'parse). - Keep Emacs keybindings working everywhere with
:key-mode 'emacs(default). BindTABandS-TABto(egfx-focus-next cv)and(egfx-focus-next cv t)for full keyboard navigation across items.
0% Idle to 60 FPS Animations
Section titled “0% Idle to 60 FPS Animations”- Property tweens:
(egfx-animate cv 'parse :at '(120 180) :fill "#22c55e" :duration 0.3 :easing 'spring)runs smoothly at 60 FPS and stops its timer automatically on completion. - Custom frame loops:
(egfx-on-frame cv (lambda (c elapsed dt) (egfx-draw c ...)) 60)drives dynamic visualizers and games. Call(egfx-on-frame cv nil)to return immediately to 0% idle CPU.
Direct Pixels and Snapshots
Section titled “Direct Pixels and Snapshots”- Present raw pixel buffers (
rgba8,bgra8,rgb24,gray8) from memory or directly from a raw file (:file) with zero Elisp heap allocation viaegfx-present-pixels. - Capture rendered frames in memory without disk I/O via
(egfx-snapshot cv :format 'image)(or'rgba8/'tiff).