Mouse and Digitizer Input
In this small post, we create a short demo of a vector drawing board. It uses low-level "pointer*" event listeners to capture mouse, digitizer, or touch events on a Graphics object. Most importantly, unlike "click" and other events, pointer events are not throttled and can capture every small detail
There are 4 types of events:
"pointerup""pointerdown""pointermove""pointerrawupdate"
These events send the following properties:
<|"Position"->{x,y}, "Tilt"->{e,w}, "Pressure"->p, "Buttons"->i|> For basic mouse input, pressure and tilt are fixed at constant values, but if you have a Wacom tablet, you can take advantage of both as well. A reliable drawing cycle includes:
pointerdown - begin a new stroke and add the first point
pointermove - append new points
pointerup - stop appending new pointspointerrawupdate is similar to pointermove, but it can be fired with much higher rate than some typical browser event. We will use it later in the raster canvas example.
For strokes, we can rely on the Line primitive, since it supports multiple lines within a single primitive. Therefore, we don't need to continually append new objects to Graphics. Normally, Graphics attempts to interpolate changes to the point coordinates, but for freehand drawing, we don't need that, so we disable it using Directive.
Directive["TransitionType"->None] One more important thing: we need to disable panning and zooming for obvious reasons by setting "Controls"->False. Let's assemble it!
Module[{
segments = {},
paint = False, pressure = 0.0
},
Row[{
EventHandler[
Graphics[{Black,
Directive["TransitionType"->None], Line[segments//Offload]
}, PlotRange->1, ImageSize->{500,500}, "Controls"->False], {
"pointerdown" -> Function[data,
segments = Append[segments, {data["Position"]}];
paint = True
],
"pointerup" -> Function[data,
paint = False;
],
"pointermove" -> Function[data,
If[paint,
segments[[-1]] = Append[segments[[-1]], data["Position"]];
segments = segments
];
pressure = data["Pressure"];
]
}]//Framed,
VerticalGauge[pressure//Offload, {0,1}]
}]
] // Deploy Here is a demonstration
Raster Canvas
To make full use of pressure data, we need to switch to raster graphics. The most efficient and straightforward approach is to use the Canvas2D package. It bridges the GPU-accelerated HTML Canvas API to Wolfram Language through an opcode pipeline. You specify where to move the pen, set its parameters, and dispatch the queue of commands to the frontend.
Let's import it first.
Needs%5B%22Canvas2D%60%22-%3E%22ctx%60%22%5D%3B Here is a simple example that draws a few strokes:
context%20%3D%20ctx%60Canvas2D%5B%5D%3B%0AImage%5Bcontext%2C%20ImageResolution-%3E%7B200%2C200%7D%5D (*VB[*)(FrontEndRef["Ff2f78767-1528-4703-92c8-1a6d99b5a58a"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgUJpxmlmVuYm5nrGpoaWeiamBsY61oaJVvoGiaapVhaJpkmmlokAgCJ2xVD"*)(*]VB*) ctx%60BeginPath%5Bcontext%5D%3B%0Actx%60MoveTo%5Bcontext%2C%20%7B100%2C100%7D%5D%3B%0Actx%60LineTo%5Bcontext%2C%20%7B150%2C150%7D%5D%3B%0Actx%60LineTo%5Bcontext%2C%20%7B50%2C150%7D%5D%3B%0Actx%60LineTo%5Bcontext%2C%20%7B100%2C100%7D%5D%3B%0Actx%60Stroke%5Bcontext%5D%3B%0Actx%60Dispatch%5Bcontext%5D%3B Now we need to do the same for every "pointermove" event. One caveat is that whenever the pressure changes, we have to create a subpath. This costs a few extra bytes, but since we send only a few strokes per second, it will not noticeably affect performance. One more thing to consider is to switch from "pointermove" to "pointerrawupdate" which gives us 1.6 - 6 ms latency for the input events (tested on Mac Air M1). This should feel much nicer with a real digitizer.
aContext%20%3D%20ctx%60Canvas2D%5B%5D%3B%0Actx%60SetFillStyle%5BaContext%2C%20Blend%5B%7BWhite%2C%20Yellow%7D%2C%200.1%5D%5D%3B%0Actx%60FillRect%5BaContext%2C%20%7B0%2C0%7D%2C%20%7B500%2C%20500%7D%5D%3B%0Actx%60Dispatch%5BaContext%5D%3B%0A%0AdownQ%20%3D%20False%3B%0AlastPressure%20%3D%200%3B%0A%0AEventHandler%5B%0A%20%20Image%5BaContext%2C%20ImageResolution-%3E%7B500%2C500%7D%2C%20Epilog-%3E%7B%0A%20%20%20%20%20%20EventHandler%5BAnimationFrameListener%5BaContext%5D%2C%20Function%5BNull%2C%0A%20%20%20%20%20%20%20%20ctx%60Dispatch%5BaContext%5D%3B%0A%20%20%20%20%20%20%20%20cursorPos%20%3D%20%7Bcursor%5B%5B1%5D%5D%2C500-cursor%5B%5B2%5D%5D%7D%3B%0A%20%20%20%20%20%20%5D%5D%0A%20%20%20%20%7D%5D%2C%20%7B%0A%20%20%20%20%20%20%22pointerdown%22%20-%3E%20Function%5Bdata%2C%20%0A%20%20%20%20%20%20%20%20downQ%20%3D%20True%3B%0A%20%20%20%20%20%20%20%20ctx%60BeginPath%5BaContext%5D%3B%0A%20%20%20%20%20%20%20%20ctx%60SetLineWidth%5BaContext%2C%20lastPressure%20%3D%2010%20data%5B%22Pressure%22%5D%5D%3B%0A%20%20%20%20%20%20%20%20ctx%60MoveTo%5BaContext%2C%20data%5B%22Position%22%5D%5D%3B%0A%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%22pointermove%22%20-%3E%20Function%5Bdata%2C%20%0A%20%20%20%20%20%20%20%20If%5BdownQ%2C%20With%5B%7Bp%20%3D%2010%20data%5B%22Pressure%22%5D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20If%5BAbs%5Bp%20-%20lastPressure%5D%20%3E%200.1%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60LineTo%5BaContext%2C%20data%5B%22Position%22%5D%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60Stroke%5BaContext%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60BeginPath%5BaContext%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60SetLineWidth%5BaContext%2C%20p%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60MoveTo%5BaContext%2C%20data%5B%22Position%22%5D%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60LineTo%5BaContext%2C%20data%5B%22Position%22%5D%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx%60Stroke%5BaContext%5D%3B%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%5D%3B%0A%20%20%20%20%20%20%20%20%20%20lastPressure%20%3D%20p%3B%0A%20%20%20%20%20%20%20%20%5D%5D%3B%0A%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%22pointerup%22%20-%3E%20Function%5Bdata%2C%20%0A%20%20%20%20%20%20%20%20downQ%20%3D%20False%3B%0A%20%20%20%20%20%20%20%20ctx%60Stroke%5BaContext%5D%3B%0A%20%20%20%20%20%20%5D%0A%20%20%7D%0A%5D%20 Try it out! Even with a mouse