Skip to content

Guideline to align object to canvas center (vertical/horizontal) #10375

Closed Answered by zhe-he
camel113 asked this question in Q&A
Discussion options

You must be logged in to vote

If the canvas is immovable, drawing a straight line at -1px around the edges of the canvas—would that achieve the result you want?

If the canvas is movable, you need to upgrade. The following method can resolve this.

  1. Update the code here and include it in your project.
  2. Customize getObjectsByTarget to return the list of elements you want to align. Here’s an example:
initAligningGuidelines(canvas, {
    getObjectsByTarget(target) {
        const objects = new Set<FabricObject>();
        const canvas = target.canvas!;
        // The example aligns only with elements of the same type.
        for (const item of canvas.getObjects()) {
               if (item.type == target.type) {

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by camel113
Comment options

You must be logged in to vote
1 reply
@zhe-he
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants