0

我正在尝试提取图像(在这种情况下,/camera/frame是已知良好的 JPG),并将其加载为我的document.body.

到目前为止,这是我的代码:

var backgroundImage = new Image();
backgroundImage.onload = function ()
{
    console.log("onload");
    document.body.style.backgroundImage = this.src;
};

backgroundImage.src = 'camera/frame'; 

"onload"按预期打印,并且 this.src 打印出完整的 URL 到/camera/frame,但document.body.style.backgroundImage仍然存在""

4

2 回答 2

1

我相信你可能错过了两件事。

var path = 'path/to/image.jpg';
document.body.style.background='url('+path+')';
于 2012-07-12T01:59:47.687 回答
0

Canvas 2D 来救援!

var backgroundImage = new Image();
backgroundImage.onload = function ()
{    
    var canvas = document.getElementById('canvas');
    canvas.width = this.width;
    canvas.height = this.height;

    var canvasContext = canvas.getContext('2d');
    canvasContext.drawImage(this, 0, 0, this.width, this.height);
};

backgroundImage.src = 'camera/frame'; 
backgroundImage.width = $(window).width();
backgroundImage.height = $(window).height();

在背景中加载图像,然后将其无缝绘制到画布中!

于 2012-07-12T16:39:48.827 回答