Prefer whole pixels during canvas edits

This commit is contained in:
Dandaedre 2026-11-19 11:42:20 +01:00
parent 3cdd303b45
commit c2ae6fc256
7 changed files with 85 additions and 22 deletions

2
Cargo.lock generated
View File

@ -602,7 +602,7 @@ dependencies = [
[[package]]
name = "canvas-pilot"
version = "0.10.0-rc.17"
version = "0.10.0-rc.18"
dependencies = [
"base64 0.22.1",
"craft-dwg",

View File

@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"]
resolver = "3"
[workspace.package]
version = "0.10.0-rc.17"
version = "0.10.0-rc.18"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot"

View File

@ -1,6 +1,15 @@
//! Proximity snapping for layer movement and anchored single/group resizing.
use crate::{Geometry, Layout, ResizeHandle, ResizeMode};
// Favor integer screen coordinates without turning the pixel option into a hard grid.
// A quarter-pixel capture zone leaves an equally wide free zone around half pixels.
const PIXEL_MAGNET_RADIUS: f64 = 0.25;
fn pixel_magnet(value: f64) -> Option<f64> {
let target = value.round();
((target - value).abs() <= PIXEL_MAGNET_RADIUS).then_some(target)
}
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Snapping {
@ -166,8 +175,10 @@ impl Layout {
{
target[axis] = edge;
guides[axis] = Some(edge);
} else if options.pixels {
target[axis] = point[axis].round();
} else if options.pixels
&& let Some(pixel) = pixel_magnet(point[axis])
{
target[axis] = pixel;
}
}
(target, guides)
@ -274,7 +285,9 @@ impl Layout {
&& let Some(i) = selected.iter().position(|v| *v)
{
let coordinate = edge(before.layers[i], axis)[0] + delta[axis];
delta[axis] += coordinate.round() - coordinate;
if let Some(pixel) = pixel_magnet(coordinate) {
delta[axis] += pixel - coordinate;
}
}
}
let mut next = before.layers;
@ -348,7 +361,9 @@ impl Layout {
grid: Option<f64>,
) -> [Option<f64>; 2] {
let r = self.resize.clone().expect("resize gesture");
self.drag_to_with_precision(point, grid.is_some());
// Pixel-edge snapping needs the precise pointer result so its small magnetic
// zone can favor integers without making fractional resizing unreachable.
self.drag_to_with_precision(point, grid.is_some() || options.pixels);
if self.gesture_error.is_some() || point.iter().any(|v| !v.is_finite()) {
return [None; 2];
}
@ -452,7 +467,9 @@ impl Layout {
if options.pixels {
let spacing = grid.unwrap_or(1.0);
let target = (current[axis] / spacing).round() * spacing;
candidates.push((2, (target - current[axis]).abs(), axis, target, None));
if grid.is_some() || pixel_magnet(current[axis]).is_some() {
candidates.push((2, (target - current[axis]).abs(), axis, target, None));
}
}
}
candidates.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1)));
@ -511,3 +528,32 @@ impl Layout {
[None; 2]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn pixels_only() -> Snapping {
Snapping {
enabled: true,
pixels: true,
screen_edges: false,
layer_edges: false,
mask_edges: false,
grid: false,
..Default::default()
}
}
#[test]
fn pixel_edges_are_magnetic_without_forbidding_fractional_resizes() {
let mut layout = Layout::default();
layout.start_handle_resize(1, ResizeHandle::Right, [1800.0, 360.0], false);
layout.drag_to_snapped([1800.2, 360.0], pixels_only(), 6.0);
assert_eq!(layout.layers()[1].x + layout.layers()[1].width, 1800.0);
layout.drag_to_snapped([1800.4, 360.0], pixels_only(), 6.0);
assert!((layout.layers()[1].x + layout.layers()[1].width - 1800.4).abs() < 1e-9);
}
}

View File

@ -250,9 +250,13 @@ fn pivot_placement_honors_screen_layer_pixel_and_grid_options() {
options.layer_edges = false;
options.pixels = true;
assert_eq!(
layout.snap_pivot(1, [833.3, 653.7], options, 6.0, None, true),
layout.snap_pivot(1, [833.2, 653.8], options, 6.0, None, true),
([833.0, 654.0], [None; 2])
);
assert_eq!(
layout.snap_pivot(1, [833.3, 653.7], options, 6.0, None, true),
([833.3, 653.7], [None; 2])
);
for spacing in [10.0, 100.0] {
let target = [833.3_f64, 653.7].map(|p| (p / spacing).round() * spacing);
assert_eq!(

View File

@ -76,25 +76,26 @@ fn group_snapping_preserves_spacing_and_cancel_restores_geometry() {
assert_eq!(layout.layers()[0].x, 3.0);
}
#[test]
fn pixel_snap_can_be_disabled_and_numeric_entry_bypasses_it() {
fn pixel_snap_is_soft_and_optional_while_numeric_entry_bypasses_it() {
let mut layout = Layout::default();
let mut g = layout.layers()[1];
g.x = 1160.25;
layout.apply(1, g).unwrap();
for pixels in [false, true] {
let options = |pixels| Snapping {
enabled: true,
pixels,
screen_edges: false,
layer_edges: false,
..Default::default()
};
for (pixels, pointer, expected) in [
(false, 1199.9, 1160.15),
(true, 1199.9, 1160.0),
(true, 1200.2, 1160.45),
] {
layout.start_drag([1200.0, 200.0]);
layout.drag_to_snapped(
[1200.2, 200.0],
Snapping {
enabled: true,
pixels,
screen_edges: false,
layer_edges: false,
..Default::default()
},
6.0,
);
assert!((layout.layers()[1].x - if pixels { 1160.0 } else { 1160.45 }).abs() < 1e-8);
layout.drag_to_snapped([pointer, 200.0], options(pixels), 6.0);
assert!((layout.layers()[1].x - expected).abs() < 1e-8);
layout.cancel_drag();
assert_eq!(layout.layers()[1], g);
}

View File

@ -4,6 +4,13 @@
## Unreleased
### v0.10.0-rc.18
- Make **Pixel edges** a soft whole-pixel preference for mouse movement and
resizing. Coordinates within one quarter pixel settle on the integer after
stronger screen, layer and grid snaps; farther fractional placement remains
available for precise work at high canvas zoom.
### v0.10.0-rc.17
- Keep extreme scroll zoom responsive by omitting camera-scaled decorative canvas

View File

@ -217,6 +217,11 @@ stay selected across selection changes and project loads in this session; they
are not saved to projects/settings and start off on launch. They affect layer
movement/resize, not numeric edits or crop-content handles.
With snapping enabled, **Pixel edges** gives whole output pixels a small magnetic
zone during layer movement and resizing. Screen, layer and grid alignment keeps
priority. Moving beyond one quarter pixel from the nearest integer retains the
fractional coordinate, so precise subpixel placement remains available at high zoom.
<!-- Screenshot recipe: ../images/capture.md#advanced-guide-captures -->
![Overlay grid controls with eight columns, six rows and separate visibility and snapping switches](../images/10-guide-grid.png)