如何使用Phonegap将图像加载到HTML5 Canvas上

时间:2011-02-26 03:38:56

标签: android html5 canvas cordova

尝试将图像加载到html5画布上,然后使用Phonegap在Android上运行html5。这是我的HTML。

<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
    var c=document.getElementById("myCanvas");
    var cxt=c.getContext("2d");
    var img=new Image()
    img.src="img_flwr.png"
    cxt.drawImage(img,0,0);
</script>
<img src="img_flwr.png"/>
</body>
</html>

我已经包含了标准的img标记来演示问题。

在Firefox下,此页面正确显示在画布上和标准img标记中呈现的图像。

result under Firefox

当我使用Phonegap部署到Android模拟器时,只有标准img显示图片。

result on Android emulator via Phonegap

html和.png文件都在我的phonegap项目的assets / www文件夹中。

如何在画布上正确渲染图像?

编辑..修正(感谢Avinash)..它的所有关于计时..你需要等到img加载之后才能画到画布上..vis

var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
var img=new Image()
img.src="img_flwr.png";
img.onload = function() { 
    cxt.drawImage(img,0,0);
};

3 个答案:

答案 0 :(得分:9)

这可能是加载图像的延迟(这只是我的猜测,我不确定。如果它有帮助我会很高兴)...
试试这段代码(等待页面完全加载)

<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
    window.onload = function() {  
        var c=document.getElementById('myCanvas');
        var cxt=c.getContext('2d');
        var img=new Image();
        img.src='img_flwr.png';
        cxt.drawImage(img,0,0);
    };
</script>
<img src="img_flwr.png"/>
</body>
</html>

或者您可以在按如下方式加载图像后执行

var c=document.getElementById('myCanvas');
var cxt=c.getContext('2d');
var img=new Image();
img.onload = function() { 
    cxt.drawImage(img,0,0);
};
img.src='img_flwr.png';

如果问题仍然存在,请告诉我......

答案 1 :(得分:0)

我没有机会试试这个,但试着将你的形象称为:

文件://android_asset/www/img_flwr.png

答案 2 :(得分:0)

我尝试没有用于PhoneGap 1.1.0 Xcode 4.2的img标签,而且Image()类onload事件触发,但图像报告零宽度和高度。如果添加了img html标签,则可以正常工作。因此,我认为设备就绪不是这样做的地方,或者这表明与js程序员可能期望事件和排序工作的方式不兼容。让图像在屏幕上浮动,打开多个分辨率的问题。

这是存根代码。

var tileLoaded = true;
var dirtLoaded = true;
function onBodyLoad()
{       
    document.addEventListener("deviceready", onDeviceReady, false);
}

/* When this function is called, PhoneGap has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */

function onDeviceReady()
{
    // do your thing!
    //navigator.notification.alert("PhoneGap is working");
    console.log("device on ready started");

    canvas = document.getElementById('myCanvas');
    c = canvas.getContext('2d');
    canvas.addEventListener('mousedown', handleMouseDown, false);
    window.addEventListener('keydown', handleKeyDown, false);

    tileMap = [];
    tile.src = "img/tile.png";
    //tile.onLoad = tileImageLoaded();
    dirt.src = "img/dirt.png";
    //dirt.onLoad = dirtImageLoaded();
    console.log("device on ready ended");
    draw();

}
function tileImageLoaded()
{
    console.log("tile loaded");
    console.log("draw - tile.width:" + tile.width);
    console.log("draw - tile.height:" + tile.height);
    tileLoaded = true;
    draw();
}
function dirtImageLoaded()
{
    console.log("dirt loaded");
    console.log("draw - dirt.width:" + dirt.width);
    console.log("draw - dirt.height:" + dirt.height);
    dirtLoaded = true;
    draw();
}