问答详情
源自:2-1 绘制背景

我的背景没出来,哪位大神帮我看看?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

<style type="text/css">

body{

padding-top:10px;

}

.main{

width:800px;

height:600px;

margin:0 auto;

}

#allcanvas{

width:800px;

height:600px;

margin:0 auto;

position:relative;

}

#canvas1{

position:absolute;

bottom:0;

left:0;

z-index:1;

}

#canvas2{

position:absolute;

bottom:0;

left:0;

z-index:1;


}



</style>

</head>


<body>

<div class="main">

      <div id="allcanvas">

           <div id="canvas1" height='600' width='800'></div>

           <div id="canvas2" height='600' width='800'></div>

      </div>

</div>

<script type="text/javascript" src="js/main.js"></script>

<script type="text/javascript" src="js/commonFunctions.js"></script>

<script type="text/javascript" src="js/bg.js"></script>

</body>

</html>

// JavaScript Document

var can1;

var can2;

var ctx1;

var ctx2;

var lastTime;

var deltaTime;

var canWidth;

var canHeight;

var bgPic=new Image();

document.body.onload=game;

function game(){

init();

lastTime=Date.now();

deltaTime=0;

gameloop();

}

function init(){

//获得canvas context

can1=document.getElementById('canvas1');//fish dust UI circle

ctx1=can1.getContext('2d');

can2=document.getElementById('canvas2');//background one fruits

ctx2=can2.getContext('2d');

bgPic.src="./src/background.jpg";

canWidth=can1.width;

canHeight=can2.height;

}

function gameloop(){

window.requestAnimFrame(gameloop);//循环

var now=Date.now();

deltaTime=now-lastTime;

lastTime=now;

}

// JavaScript Document

function drawBackground(){

ctx2.drawImage(bgPic,0,0,canWidth,canHeight);

}


提问者:xiao55555 2015-12-11 21:46

个回答

  • 慕粉13261604892
    2016-11-30 18:12:07

    http://img.mukewang.com/583ea5e500015c1a06090181.jpg

    应该是canvas 不是div

  • 且听风吟cy
    2016-08-02 22:49:09

    window.requestAnimFrame(gameloop);     这里也写错了、

    应该是window.requestAnimationFrame(gameloop);

  • 姑娘有点太傲娇
    2016-01-08 23:29:19

    你的问题解决了吗?我出现了和你一样的问题 你是怎么解决的

  • 慕无忌5517827
    2015-12-31 13:24:19

    我们是一样的问题,但是,我去js文件下看到我的background.js根本就是0KB,所以,你看一下你的会不会是在编辑器下没有保存你的background.js,我的是这个问题,保存了背景就出来了

  • 尤快快
    2015-12-17 14:53:18

    var can1;

    var can2;


    var ctx1;

    var ctx2;


    var canWidth;

    var canHeight;


    var lastTime;

    var deltaTime;


    var bgPic=new Image();

    document.body.onload=game;

    function game () {

    init();

    lastTime=Date.now();

    deltaTime=0;

    gameloop();

    }

    function init () {

    can1=document.getElementById("canvas1");

    ctx1=can1.getContext("2d");

    can2=document.getElementById("canvas2");

    ctx2=can2.getContext("2d");


    canWidth=can1.width;

    canHeight=can1.height;

    bgPic.src="./src/background.jpg";

    }

    function gameloop () {

    requestAnimationFrame(gameloop);//setInterval setTimeout

    var now=Date.now(0);

    deltaTime=now-lastTime;

    lastTime=now;


    drawBackground();

    }

    就是加下划线的地方报错 好烦

  • 尤快快
    2015-12-16 22:17:43

    http://img.mukewang.com/5671725e0001834402880145.jpg这里老是报错can1.getContext is not a function 不知道为什么 求帮助

  • echo_kinchao
    2015-12-14 10:22:03

    最好写成相对路径

  • xiao55555
    2015-12-11 22:15:01

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>无标题文档</title>

    <style type="text/css">

    body{

    padding-top:10px;

    }

    .main{

    width:800px;

    height:600px;

    margin:0 auto;

    }

    #allcanvas{

    width:800px;

    height:600px;

    margin:0 auto;

    position:relative;

    }

    #canvas1{

    position:absolute;

    bottom:0;

    left:0;

    z-index:1;

    }

    #canvas2{

    position:absolute;

    bottom:0;

    left:0;

    z-index:1;


    }



    </style>

    </head>


    <body>

    <div class="main">

          <div id="allcanvas">

               <div id="canvas1" height='600' width='800'></div>

               <div id="canvas2" height='600' width='800'></div>

          </div>

    </div>

    <script type="text/javascript" src="js/main.js"></script>

    <script type="text/javascript" src="js/commonFunctions.js"></script>

    <script type="text/javascript" src="js/bg.js"></script>

    </body>

    </html>

    // JavaScript Document

    var can1;

    var can2;

    var ctx1;

    var ctx2;

    var lastTime;

    var deltaTime;

    var canWidth;

    var canHeight;

    var bgPic=new Image();

    document.body.onload=game;

    function game(){

    init();

    lastTime=Date.now();

    deltaTime=0;

    gameloop();

    }

    function init(){

    //获得canvas context

    can1=document.getElementById('canvas1');//fish dust UI circle

    ctx1=can1.getContext('2d');

    can2=document.getElementById('canvas2');//background one fruits

    ctx2=can2.getContext('2d');

    bgPic.src="./src/background.jpg";

    canWidth=can1.width;

    canHeight=can2.height;

    }

    function gameloop(){

    window.requestAnimFrame(gameloop);//循环

    var now=Date.now();

    deltaTime=now-lastTime;

    lastTime=now;

    drawBackground();

    }

    // JavaScript Document

    function drawBackground(){

    ctx2.drawImage(bgPic,0,0,canWidth,canHeight);

    }


  • 李晓健
    2015-12-11 22:04:13

    没看到你调用 drawBackground 这个画背景的方法呀