← Pixel Dreamer Studio

Place brush dots at the same spacing however fast the mouse moves

Walk along each mouse movement in fixed steps, and carry the leftover distance into the next movement.

With input accumulation on, which is the default, Godot merges mouse motion into about one event per frame. A dot at every event is then spaced by how far the mouse moved that frame: in our Godot 4.7 test of a 600 px stroke at 60 fps, 5 px apart at 300 px per second and 50 px apart at 3,000. Stepping each movement by 8 px but dropping the leftover drew nothing at all when slow. Carrying the leftover gave 76 dots, every one 8 px apart, at every speed.

The code

Save it as stroke_spacing.gd. On mouse press call begin(pos), on every InputEventMouseMotion while the button is held call add(pos), and put a dot at each point they return: a decal, a particle, a stamp in an Image, whatever your brush draws.

extends RefCounted
## Even dots along a mouse or brush stroke, however fast the mouse moves.
## begin(pos) on press, add(pos) on every motion event, place a dot at every point they return.

var spacing := 8.0
var _last := Vector2.ZERO
var _carry := 0.0  # distance walked since the last dot


func begin(pos: Vector2) -> PackedVector2Array:
	_last = pos
	_carry = 0.0
	return PackedVector2Array([pos])


func add(pos: Vector2) -> PackedVector2Array:
	var out := PackedVector2Array()
	var seg := pos - _last
	var length := seg.length()
	var d := spacing - _carry  # where the next dot falls on this segment
	while d <= length:
		out.append(_last + seg * (d / length))
		d += spacing
	_carry = length - (d - spacing)  # keep the leftover for the next segment
	_last = pos
	return out

What we measured

A straight 600 px stroke at 60 fps with one motion event per frame, at 300, 1,000 and 3,000 px per second, with 8 px spacing. Two runs with the same numbers.

Why it happens

The mouse does not report every pixel it passes. A fast flick covers 50 px between two events and a slow drag only a few. Spacing by distance instead of by event fixes both, but only if the distance already walked since the last dot is kept. Without it, every short move starts again from zero and never reaches the next dot.

What to change

One emitter for a whole level of painted points: ambient particles from emission points.

Paint particles in the editor: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.

More how-tos for VFX Painter Kit for Godot 4: Ambient particles in many places from one emitter · Nodes your editor tool adds that stay in the scene

More how-tos: UI and HUD