An Introduction to Canvas in GNU Emacs
GNU Emacs has introduced a new feature called canvas, which enables users to draw and manipulate images within an Emacs buffer without negatively impacting performance. This canvas feature has been merged into the upstream GNU Emacs and will be part of the upcoming Emacs 32 release cycle.
The canvas patch was merged after a lengthy 8-month journey, during which various hacks were attempted to address limitations in updating and manipulating images in Emacs. The idea of exposing a pixel buffer from within Emacs was suggested by Daniel (aka minad), and it has now been implemented as a feature in the new canvas.
It is essential to clarify that the canvas feature is not related to any web framework or technology. Rather, it is named "canvas" as it allows users to draw arbitrarily on a surface within an Emacs buffer. The canvas API integrates with the existing image API and provides access to the underlying pixel buffer via the dynamic module API. This is why it is not a direct comparison to SVG, which has been supported in Emacs for a long time.
Unlike SVG, which is affected by Emacs' redisplay engine, the canvas feature has its own redrawing path that only updates the specific glyphs in the buffer related to the canvas. This means that canvas-refresh, a function to update the canvas, does not utilize or call Emacs' redisplay, resulting in improved performance. Even with double-buffering in Emacs 25, performance remains unaffected by the canvas feature.
The canvas feature is hardware-accelerated by allowing users to get their pixels hardware-accelerated outside of Emacs and then put them into the canvas' pixel buffer. This means that one can still benefit from hardware acceleration for pixel calculation, texture math, etc., without relying on a GPU-powered redisplay within Emacs itself.
Although Emacs has supported images for decades using Text Properties and Overlay Properties, the canvas feature provides a new way to access and manipulate the pixel buffer of images displayed in the Emacs buffer. This enables users to update images in-place by directly manipulating pixels without passing strings or Emacs Lisp objects through the garbage collector, which is particularly beneficial when dealing with complex graphics at high refresh rates.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.