Reputation: 482
I am using fabric.js library to set an image background but can not get references
canvas.setBackgroundImage('http://fabricjs.com/assets/honey_im_subtle.png',
canvas.renderAll.bind(canvas), {
width: canvas.width,
height: canvas.height,
// Needed to position backgroundImage at 0/0
originX: 'left',
originY: 'top'
});
I am using this function but there is not a property for canvas.width while I get a canvas.getWidth() but it is not working like this .. as well as this is also not working with my case :(
canvas.setBackgroundImage('images/download.jpg', canvas.renderAll.bind(canvas),
{
backgroundImageOpacity: 0.5,
backgroundImageStretch: true
// above both option are not valid with my case
});
Upvotes: 1
Views: 1558
Reputation: 15604
You need to set opacity and scale
of image object instead of backgroundImageOpacity, backgroundImageStretch
.
var canvas = window._canvas = new fabric.Canvas('c');
fabric.Image.fromURL('https://picsum.photos/200/300', (img) => {
img.set({
opacity: 0.5,
scaleX: canvas.width/img.width,
scaleY: canvas.height/img.height,
});
canvas.setBackgroundImage(img, canvas.requestRenderAll.bind(canvas));
});
canvas {
border: 1px solid #999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.4.0/fabric.js"></script>
<canvas id="c" width="600" height="600"></canvas>
Upvotes: 2