문제

How do I center the pattern fill of a FabricJS object? It seems the pattern is always oriented to top/left. I have tried setting originX and OriginY to 'center' on the pattern, the filled object, the canvas, nothing seems to help. Any idea?

도움이 되었습니까?

해결책

I found the answer:

Patterns use offsets, and not origins. So, to simulate centering a pattern, consider the following requirements:

  1. The center is at half-width of the object.
  2. To be centered, the pattern object must be at the center point at half it's width.
pattern = new fabric.Pattern({
    source: function () {[...]},
    repeat: 'repeat',
    offsetX: [objectwidth/2] - [patternwidth/2],
    offsetY: [objectheight/2] - [patternheight/2]
});

That did it! If you changed the scale on either of the items, you will need to compensate for that as well.

다른 팁

In Pattern Example of Fabric.js there are only int values used as OriginX/Y.

Try to set it by number, to the half of your image size.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top