Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Diagram Community Logo
  1. Home
  2. GoJS
  3. Fixing Unexpected Margins in GoJS Line Shapes

Fixing Unexpected Margins in GoJS Line Shapes

Scheduled Pinned Locked Moved GoJS
1 Posts 1 Posters 55 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • KubaSCK Offline
    KubaSCK Offline
    KubaSC
    wrote on last edited by
    #1

    The problem

    While working on a node template in GoJS, I encountered an unexpected issue when using a Shape element with the figure 'LineH'. I noticed that there were strange gaps, preventing other shapes from aligning correctly.

    import * as go from "gojs";
    
    const $ = go.GraphObject.make;
    
    const rectangleShape = () =>
      $(go.Shape, "RoundedRectangle", {
        fill: null,
        width: 50,
        height: 50,
      });
    
    const lineShape = () => $(go.Shape, "LineH", { width: 100, height: 2 });
    
    export const nodeTemplate = () =>
      $(
        go.Node,
        go.Node.Horizontal,
        rectangleShape(),
        lineShape(),
        rectangleShape()
      );
    

    Zrzut ekranu 2025-03-21 113525.png

    After investigating with our debugging tools, I discovered that an unwanted margin existed on both the left and right sides of the line.

    Zrzut ekranu 2025-03-21 113930.png

    Debugging the Issue

    Initially, I tried adjusting the stroke width and other properties, hoping to eliminate the spacing issue. However, despite my efforts, the gaps remained. It took me some time to identify the root cause, but the solution turned out to be much simpler than expected.

    The Cause: strokeCap property

    The problem stemmed from the strokeCap property, which determines how the ends of a stroke line are rendered. By default, this property is set to 'butt', which was causing the unwanted margins around the line.

    The Solution

    To resolve this issue, simply set the strokeCap property to 'square'. This change removes the unintended margins and allows the shapes to align correctly.

    const lineShape = () =>
      $(go.Shape, "LineH", { width: 100, height: 2, strokeCap: "square" });
    

    After making this change, the gaps disappeared, and the alignment issues were fixed.

    Zrzut ekranu 2025-03-21 120250.png

    Additional Considerations

    This issue is not exclusive to the 'LineH' figure. Any shape defined using geometry in GoJS may exhibit similar behavior if strokeCap is set to its default value. Therefore, when working with custom geometries, it is worth keeping this property in mind to avoid unexpected margins.

    1 Reply Last reply
    0

    • Login

    • Don't have an account? Register

    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups