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
});
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.