Javascript SpriteSheet不起作用
Javascript SpriteSheet not working
我在简单的画布上显示精灵表时遇到了一些麻烦。出于调试目的,我从主代码中取出了代码,以便我可以只处理一个小文件。精灵表加载到画布旁边,但画布不显示精灵本身。任何帮助都非常感谢。谢谢
<canvas id="canvas" width="800" height="36" style="border:1px solid" onload="draw_hello()" ></canvas>
<img id="helloSprites" src="Tux-Hello.png">
<script>
//hello sprite
var x=0;
var y=0;
var f=0;
var w=32;
var h=32;
var mycanv=document.getElementById("canvas");
var ctx=mycanv.getContext("2d");
//helloImage.src = "images/Tux-Hello.png";
//var helloImage = new Image();
var hello=document.getElementById("helloSprites");
function draw_hello() {
setInterval(drawHello,200);
}
function drawHello() {
ctx.fillStyle="#292";
ctx.fillRect(0,0,mycanv.width, mycanv.height);
ctx.fillStyle="#aaa";
ctx.fillRect(0,25,mycanv.width, mycanv.height);
if (x>=(mycanv.width)) {
x=0;
}
x+=2;
f++;
// draw the car
if (f==2) {
f=4;
}
if (f==6) {
f=0;
}
ctx.drawImage(hello, w, f*h, w, h, x, y, w, h);
}
</script>
将
onload
附加到body
元素,而不是canvas
元素。
您可能正在调用 onload,因此在 img 元素完全加载之前调用 draw_hello()。
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- Array.length似乎不起作用;console.log则显示其他情况
- $ionicplatform内的$scope不;不起作用
- 我的javascript for循环不起作用
- Meteor-添加用户自定义字段的方法不起作用
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- Textarea必需的attribut在javascript中不起作用
- 为什么javascript:void(0)在Firefox中不起作用
- 退出全屏不起作用