2013-03-08 223 views
0

我想繪製一個畫布上的圖像數組,但沒有任何繪製!以下是我的代碼!還我試圖瞭解的PhoneGap和JavaScript 的index.html:在畫布上繪製陣列圖像?

<!DOCTYPE html> 
<html> 
<head> 
<title>Minecraft Background Check</title> 
</head> 
<body> 
<canvas id="mycanvas" style="position:fixed; top:0; left:0; border:1px solid #c3c3c3; width: 100%; height: 100%;"></canvas> 
<script type="text/javascript" src="cordova-2.5.0.js"></script> 
<script type="text/javascript"> 
document.addEventListener("deviceready",ondeviceReady,false); 
function onDeviceReady() {} 
    //get element by id 
var c = document.getElementById("mycanvas"); 
var ctx = c.getContext("2d"); 
    //array image which need to draw in canvas 
var image = ["img/image12","img/image13","img/image14","img/image15","img/image10"]; 
var m_arrImages = {}; 
for (var src in image) { 
    m_arrImages[src] = new Image(); 

    m_arrImages[src].src = image[src]; 
} 
var imagePositionsX = [20, 80, 140, 200, 260, 320, 380, 440, 500, 560]; 
var imagePositionsY = [20, 60, 100, 140, 180, 220, 260, 300, 340, 380]; 
var i, x, y; 
for (i = 0; i < m_arrImages.length; i++) { 
    x = imagePositionsX[ Math.floor(Math.random()*10) ]; 
    y = imagePositionsY[ Math.floor(Math.random()*10) ]; 

    ctx.drawImage(m_arrImages[i], x, y, 50, 50); 
} 
</script> 
</body> 
</html> 

回答

2

您已設置的功能ondeviceReady到在deviceready事件中運行,但沒有定義的功能。你已經定義了一個叫做onDeviceReady的函數。 Javascript變量和函數名稱區分大小寫,因此在deviceready事件中不會運行。

變化:

document.addEventListener("deviceready",ondeviceReady,false); 

document.addEventListener("deviceready",onDeviceReady,false); 
+0

我修復錯誤!但有同樣的問題! hix hix! – 2013-03-08 09:05:06

1

可能是我錯了,但.. 「m_arrImages」 - 這是用字符串,而不是圖像只是陣列,是不是?

我的東西,它是前人的精力類似的東西:

var m_arrImages = {}; 
for (var src in image) { 
    m_arrImages[src] = new Image(); 
    m_arrImages[src].src = image[src]; 
} 
+0

我修好了!但同樣的問題! hixhix!感謝幫助! – 2013-03-08 09:16:45