Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set repeating background image for a canvas in Fabric JS?

Tags:

fabricjs

I would like to set an image as the background image for the canvas.

It is possible with the canvas.setBackgroundImage method.

But it is not repeating throughout the background.

How to make it repeatable?

I have seen demos where pattern is used to set repeating background image for text and shapes.

How to use this pattern for canvas background.?

like image 680
Nitha Avatar asked Jun 11 '14 07:06

Nitha


2 Answers

According to the tests, bundled with the Fabric.JS, it is possible via backgroundColor setter even without passing fabric.Pattern

var src = 'img/checker.png';

canvas.setBackgroundColor({source: src, repeat: 'repeat'}, function () {
  canvas.renderAll();
});
like image 96
Serj Zaharchenko Avatar answered Jan 01 '23 21:01

Serj Zaharchenko


Use canvas.backgroundColor and pattern instead of background image:

canvas.backgroundColor = new fabric.Pattern({source: image})
like image 27
ubriaco Avatar answered Jan 01 '23 21:01

ubriaco