gpt4 book ai didi

javascript - phonegap JavaScript Canvas 不起作用

转载 作者:行者123 更新时间:2023-11-29 17:56:31 24 4
gpt4 key购买 nike

我找到了一个简单的 JavaScript 弹跳球动画,它适用于 chrome 浏览器 (PC)当我运行它时,使用 Phonegap eclipse android 模拟器编译但 Canvas 是空白的并且显示以下消息“不幸的是系统 ui 已停止”

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

<script>
var context;
var x = 100;
var y = 100;
var radius = 20;
var dx = 5;
var dy = 5;

function init(){
context = myCanvas.getContext('2d');

//Set an interval for the canvas to be refreshed.
// Basically call the draw function every 10 ms
setInterval(draw, 10);
}

function draw(){
//Clear the old circle before we draw the new one
context.clearRect(0,0, myCanvas.width, myCanvas.height); //This erases the entire canvas

context.beginPath();
context.fillStyle = "#0000ff";

//Draw a circle of radius 20 at the current coordinates on the canvas.
context.arc(x, y, radius, 0, Math.PI*2, true);
context.closePath();
context.fill();

//Check boundaries and negate if necessary
if (x + radius <= 0 || x - radius <= 0 || x + radius >= myCanvas.width || x - radius >= myCanvas.width)
dx = -dx;
if (y + radius <= 0 || y - radius <= 0 || y + radius >= myCanvas.height || y - radius >= myCanvas.height)
dy = -dy;

//Increment dx,dy
x+=dx;
y+=dy;
}
</script>


<title>Ball Bouncing</title>

</head>
<body onLoad = "init();"> <!-- when the body is loaded, call the init() function -->
<canvas id = "myCanvas" width ="500" height = "400">
</canvas>
</body>
</html>

同样出于某种原因,如果我删除以下内容,它会起作用:

  <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

好的,我删除了

  <body onLoad = "init();"> 

并添加了

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

我得到以下 logcat:

     09-23 21:53:30.886: E/Web Console(796): Uncaught SyntaxError: "Unexpected token < at   file:///android_asset/www/index.html:7" whats is wrong ?

请帮忙

最佳答案

更新

<script type="text/javascript" charset="utf-8">

<script>
var context;

也许是第二个脚本标签导致了问题?


少了一行,如:

myCanvas = document.getElementById("myCanvas");

关于javascript - phonegap JavaScript Canvas 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com