fabricjs / fabric.js

Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser
http://fabricjs.com
Other
28.61k stars 3.48k forks source link

[Feature]: Toggle visibility of items in clipPath #10009

Open bitttttten opened 1 month ago

bitttttten commented 1 month ago

CheckList

Description

We are using clipRect to have a kind of "artboard", where the user can have a full screen canvas.

For example, in the following 3 screenshots I can add some text, drag them to the edge my artboard, and have it cropped.

Screenshot 2024-07-16 at 15 54 59 Screenshot 2024-07-16 at 15 55 21 Screenshot 2024-07-16 at 15 55 24

This works great for us. However if we drag the text further out, and release, then the item seems to dissappear:

Screenshot 2024-07-16 at 15 55 15 Screenshot 2024-07-16 at 15 55 29

We would love to be able to have some kind of transparency effect or toggle the visibility of the clipPath here.

Does fabric support this.

We are doing:

    const clipRect = new Rect({
      width: this.width,
      height: this.height,
      absolutePositioned: true,
    })
    const frame = new Group([], {
      layoutManager: new LayoutManager(new ClipPathLayout()),
      selectable: this.selectable,
      subTargetCheck: true,
      interactive: true,
      clipPath: clipRect,
    })

    // and now we add items to our frame

Any suggestions is appreciated!

Current State

The current behaviour is that the clipPath is hiding all items, however, for our use case we want to show the items or have them at half transparency.

In other editors, for example, you can have this effect:

Screenshot 2024-07-16 at 15 59 55

Additional Context

No response

asturur commented 1 month ago

Instead of a clipPath for the canvas, you can use a custom overlay, It works way easier and has not the performance degradation of the clipPath.

Maybe i can make this an official example.

bitttttten commented 1 month ago

Maybe i can make this an official example.

That sounds great! If you need any help with it let me know @asturur

For now I can't get this working.. I have tried

    const overlayImage = new FabricImage(new window.Image())
    overlayImage.set({ backgroundColor: 'rgba(255,255,255,.25)', width, height })
    const canvas = new Canvas(this.canvasElem, {
      overlayColor: 'rgba(0,0,0,.25)',
      overlayImage,
      width,
      height,
    })

Although this makes all the elements appear faded in the frame. It's a lot closer than the clipRect, which is what I am looking for.

You can see here for example that the text is faded in the canvas, including the controls, because the overlayImage is white with 25% opacity.

Screenshot 2024-07-16 at 21 37 13