42.17.11 Canvas Images

Canvas images can be updated and displayed efficiently with high frame rates, for example by document viewers, visualization tools, dynamically computed animations or games. Canvas images have an associated writeable pixel buffer in ARGB32 format. For portability the format is the same on all platforms. Unlike other image types supported in Emacs, canvas images do not require creating a new Emacs Lisp object every time they get updated.

An image specifier with :type canvas creates a canvas, for instance as follows:

(defvar my-canvas
  '(image :type canvas
          :id my-canvas
          :data-width 800
          :data-height 600))

The image specification object uniquely (with respect to eq) identifies a canvas image object. The properties :data-width, :data-height and :id are mandatory. The width and height must both be positive integers and the id must be a symbol.

Canvases can be created by either providing ARGB32 data as a vector of fixnums or as an unibyte string in :data. Path to a file containing the ARGB32 data in :file can also be provided. Furthermore create-image can create canvas images as follows, where the data is initially loaded from a file, or from a vector:

(setq my-file-canvas (create-image "/path/to/file"
                                   'canvas nil
                                   :data-width 800
                                   :data-height 600))
(setq my-vec (make-vector (* 800 600) #xFFFF0000))
(setq my-vec-canvas (create-image my-vec
                                  'canvas t
                                  :data-width 800
                                  :data-height 600))

If the values for dimensions change (i.e., :data-height or :data-width), then Emacs will automatically adjust the underlying pixel buffer and resize the canvas.

Other image properties like :scale, :margin, :relief, :transform-smoothing and :map are supported. See Image Descriptors, for further details on these properties.

In order to display the canvas, specify it as the display property of a string or overlay:

(insert (propertize "#" 'display my-canvas))
(overlay-put some-overlay 'display my-canvas)

Once a canvas image object has been created and displayed, it can be “refreshed” via canvas-refresh.

Function: canvas-refresh image &optional reload-data

Refresh canvas image and update it on screen. If the optional argument reload-data is non-nil, reload the :data or file from the image specification before redrawing the canvas.

The following code refreshes the canvas by reloading the data or file:

;; Draw pixels
(aset my-vec <index> <color>)
;; Reload and refresh canvas images
(canvas-refresh my-vec-canvas 'reload-data)

When refreshing the canvas in a loop, we must explicitly call redisplay:

(setq canvas (create-image (make-vector (* 100 100) 0) 'canvas t
			   :data-width 100 :data-height 100))
(insert (propertize "#" 'display canvas))
(dotimes (i (* 100 100))
  (aset (plist-get (cdr canvas) :data) i #xFF)
  (canvas-refresh canvas 'reload-data)
  (redisplay))

But, if we are refreshing the canvas via a timer or a command, redisplay is implicitly called after the timer or command. The following example updates the above canvas in a timer ~30 times per second:

(let ((i 0))
  (run-at-time nil 0.033 (lambda ()
			  (when (< i (* 100 100))
			    (aset (plist-get (cdr canvas) :data) i #xFF)
			    (incf i)
			    (canvas-refresh canvas 'reload-data)))))

Canvas images can be manipulated and refreshed via dynamic modules. See Using Canvas API With Modules, for further details.