简体   繁体   English

从数据 url 设置 fabric.js canvas 背景

[英]Set fabric.js canvas background from a data url

I am trying to set the fabric.js canvas background from a URL when clicking on an image.单击图像时,我试图从 URL 设置 fabric.js canvas 背景。

I am trying to get this working when clicking on an image with the following code, which passes the photoURL from an element:我试图在使用以下代码单击图像时使它正常工作,该代码从元素传递photoURL

//Change background using Image
export function selectCanvasEdit(photoURL) {
    // replace the map on select
    let theMap = document.getElementById('google-map')
    let theCanvas = document.getElementById('canvasContainer')


    let canH = theMap.offsetHeight
    let canW = theMap.offsetWidth

    theCanvas.style.display = ''
    theMap.style.display = 'none'

    canvas.setBackgroundColor('', canvas.renderAll.bind(canvas));
    canvas.setBackgroundImage(0, canvas.renderAll.bind(canvas));


    var request = new XMLHttpRequest();
    request.open('GET', photoURL, true);
    request.responseType = 'blob';
    request.onload = function () {
        var reader = new FileReader();
        reader.readAsDataURL(request.response);
        reader.onload = function (e) {
            console.log('DataURL:', e.target.result);
            fabric.Image.fromURL(e.target.result, function (img) {
                canvas.setHeight(canH);
                canvas.setWidth(canW);
                let imgWidth = img.width;
                let imgHeight = img.height;
                let canvasWidth = canvas.getWidth();
                let canvasHeight = canvas.getHeight();


                img.scaleToWidth(canvasWidth);

                canvas.setBackgroundImage(photoURL, canvas.renderAll.bind(canvas), {
                    scaleX: canvas.width / img.width,
                    scaleY: canvas.height / img.height
                });
            });
        };
    };
    request.send();
};

I have also tried passing the photoURL argument directly to the fabric.Image.fromURL() function, with and without the following onload() event.我还尝试将photoURL参数直接传递给 fabric.Image.fromURL fabric.Image.fromURL() function,有和没有以下onload()事件。

var img = new Image();

img.onload = function() {
    var f_img = new fabric.Image(img);

    canvas.setBackgroundImage(f_img);

    canvas.renderAll();
};

img.src = photoURL;

This does not work either, all I get is a blank canvas.这也不起作用,我得到的只是一个空白 canvas。

Now when I use this code:现在当我使用这段代码时:

//Change background using Image
document.getElementById('bg_image').addEventListener('change', function (e) {

    // replace the map on select
    let theMap = document.getElementById('google-map')
    let theCanvas = document.getElementById('canvasContainer')


    let canH = theMap.offsetHeight
    let canW = theMap.offsetWidth

    theCanvas.style.display = ''
    theMap.style.display = 'none'

    canvas.setBackgroundColor('', canvas.renderAll.bind(canvas));
    canvas.setBackgroundImage(0, canvas.renderAll.bind(canvas));
    var file = e.target.files[0];
    var reader = new FileReader();
    reader.onload = function (f) {
        var data = f.target.result;
        fabric.Image.fromURL(data, function (img) {
            debugger;
            canvas.setHeight(canH);
            canvas.setWidth(canW);
            let imgWidth = img.width;
            let imgHeight = img.height;
            let canvasWidth = canvas.getWidth();
            let canvasHeight = canvas.getHeight();

            let imgRatio = imgWidth / imgHeight;
            let canvasRatio = canvasWidth / canvasHeight;

            img.scaleToWidth(canvasWidth);
            //img.scaleToHeight(canvasHeight);

            canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
                scaleX: canvas.width / img.width,
                scaleY: canvas.height / img.height
            });
        });
    };
    reader.readAsDataURL(file);
});

Everything works great, even though the img._element.attributes.src from fabric.Image.fromURL() is still a base64 data url and will load the image when copied to the browser, just like the selectCanvasEdit() .一切都很好,即使来自img._element.attributes.src fabric.Image.fromURL()的 img._element.attributes.src 仍然是 base64 数据 url 并且将在复制到浏览器时加载图像,就像selectCanvasEdit()一样。

Any help is greatly appreciated, I'm not understanding how the same type of img.src being passed to the fabric.js canvas is handled differently, even though the resulting data url can be loaded in the browser regardless of which function receives the data.非常感谢任何帮助,我不明白传递给 fabric.js canvas 的相同类型的 img.src 如何以不同方式处理,即使结果数据 url 可以加载到浏览器中,而不管哪个 function 接收数据.

After trying everything, nothing is wrong with the code within the function which makes this a little confusing.在尝试了一切之后,function 中的代码没有任何问题,这让这有点混乱。

It hit me that I was exporting this function and the canvas was being defined var canvas = new fabric.Canvas('canvas-tools');我正在导出这个 function 并且 canvas 被定义为var canvas = new fabric.Canvas('canvas-tools'); outside of this function.在这个 function 之外。

If I grab hold of that element again within the function, everything works as it should.如果我在 function 中再次抓住该元素,一切都会正常进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM