简体   繁体   English

无法将一个画布放在另一个画布下

[英]Unable to put one canvas under another

I want to put a canvas under another. 我想在另一个下面放一块画布。 I am pretty new at javascript so i apologize in advance if the following code looks a bit messy. 我对javascript很陌生,因此如果以下代码看起来有些混乱,我会提前道歉。 I want to make the canvas called "myCanvas" appear behind "coinAnimation". 我想使名为“ myCanvas”的画布出现在“ coinAnimation”的后面。 All responses are extremely appreciated. 非常感谢所有回复。

HTML HTML

<div id="wrapper">
<canvas id="coinAnimation" height="500" width="500"></canvas>
<canvas id="myCanvas" height="500" width="600"></canvas>
</div>

Javascript 使用Javascript

<script type="text/javascript">
(function () {
var coin,
    coinImage,
    canvas;                 

function gameLoop () {
    window.requestAnimationFrame(gameLoop);
    coin.update();
    coin.render();
    }

function sprite (options) {
var that = {},
frameIndex = 0,
    tickCount = 0,
    ticksPerFrame = options.ticksPerFrame || 0,
    numberOfFrames = options.numberOfFrames || 1;

    that.context = options.context;
    that.width = options.width;
    that.height = options.height;
    that.image = options.image;

    that.update = function () {

    tickCount += 1;

    if (tickCount > ticksPerFrame) {

    tickCount = 0;

// If the current frame index is in range
if (frameIndex < numberOfFrames - 1) {  

// Go to the next frame
frameIndex += 1;
} else {
frameIndex = 0;
}
}
};
that.render = function () {

// Clear the canvas
that.context.clearRect(0, 0, that.width, that.height);

// Draw the animation
that.context.drawImage(
    that.image,
    frameIndex * that.width / numberOfFrames,
    -110,
    that.width / numberOfFrames,
    that.height,
    0,
    0,
    that.width / numberOfFrames,
    that.height);
    };

return that;
}

// Get canvas
canvas = document.getElementById("coinAnimation");
canvas.width = 600;
canvas.height = 300;

// Create sprite sheet
coinImage = new Image();    

// Create sprite
coin = sprite({
    context: canvas.getContext("2d"),
    width: 1000,
    height: 300,
    image: coinImage,
    numberOfFrames: 10,
    ticksPerFrame: 7
    });

// Load sprite sheet
coinImage.addEventListener("load", gameLoop);
coinImage.src = "images/test2.png";

} ());
</script>

<script type="text/javascript">
window.addEventListener('load', function () {
  var
    img = new Image,
    ctx = document.getElementById('myCanvas').getContext('2d');

  img.src = "images/back.png";
  img.addEventListener('load', function () {

    var interval = setInterval(function() {
      var x = 0, y = 0;

      return function () {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
        ctx.drawImage(img, x, y);

        x += -1;
        if (x > ctx.canvas.width) {
          x = 0;
        }
      };
    }(), 1000/40);
  }, false);
}, false);
</script>

See here: jsfilddle.net . 看到这里: jsfilddle.net I added the background colors to demonstrate positioning and z-index. 我添加了背景色以演示定位和z-index。

First, make the wrapper div position relative. 首先,使包装器div位置相对。 This is the position that the container divs will be based off. 这是容器div所基于的位置。 Then make the container divs absolute positioned to (0,0). 然后将容器div绝对定位到(0,0)。 Then, set the z-index so that myCanvas is behind coinAnimation. 然后,设置z-index,以使myCanvas位于coinAnimation的后面。

CSS: CSS:

#wrapper
{
    position:relative;    
}

#coinAnimation
{
    background-color:blue; 
    position:absolute;
    z-index:1;
}
#myCanvas
{
    background-color:red;
    position:absolute;
    top:0px;
    left:0px;
    z-index:0;
}

HTML: HTML:

<div id="wrapper">
    <canvas id="coinAnimation" height="500" width="500"></canvas>
    <canvas id="myCanvas" height="500" width="600"></canvas>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM