Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(dia.ElementView): extract snap-to-grid logic into dedicated method #2872

Merged
merged 2 commits into from
Feb 4, 2025

Conversation

kumilingus
Copy link
Contributor

@kumilingus kumilingus commented Feb 4, 2025

Description

This change enables customization of the snap-to-grid behavior, allowing for:

  • Snapping to the element's center (or to any point)
  • Using key modifiers to adjust snapping behavior
import { dia, g } from '@joint/core';

const paper = new dia.Paper({
    /* ... */
    elementView: jdia.ElementView.extend({
        snapToGrid: function(_evt, x, y) {
            const gridSize = this.paper.options.gridSize;
            const { width, height } = this.model.size();
            // snap to the center of the element to the grid
            const snapX = g.snapToGrid(x + width / 2, gridSize);
            const snapY = g.snapToGrid(y + height / 2, gridSize);
            return { x: snapX - width / 2, y: snapY - height / 2 };
        }
    })
});

@zbynekstara zbynekstara changed the title refactor(dia.ElementView): extract the snap-to-grid logic into a dedicat… refactor(dia.ElementView): extract snap-to-grid logic into dedicated method Feb 4, 2025
@zbynekstara zbynekstara merged commit 4f1110e into clientIO:master Feb 4, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants