HTML5 Canvas设置z-index

Eli*_*one 37 javascript html5

是否可以在HTML5画布中设置绘制对象的z-index?

我试图得到它所以一个对象可以在"玩家"面前,另一个对象在"玩家"背后

mar*_*rkE 51

是的......是的.您可以使用合成来"绘制"现有像素.

ctx.globalCompositeOperation='destination-over';
Run Code Online (Sandbox Code Playgroud)

这是一个示例和一个演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var cx=100;

drawCircle()
cx+=20;

ctx.globalCompositeOperation='destination-over';

$("#test").click(function(){
  drawCircle();
  cx+=20;
});

function drawCircle(){
  ctx.beginPath();
  ctx.arc(cx,150,20,0,Math.PI*2);
  ctx.closePath();
  ctx.fillStyle=randomColor();
  ctx.fill();
}

function randomColor(){ 
  return('#'+Math.floor(Math.random()*16777215).toString(16));
}
Run Code Online (Sandbox Code Playgroud)
body{ background-color: ivory; }
canvas{border:1px solid red;}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button id="test">Draw new circle behind.</button><br>
<canvas id="canvas" width=300 height=300></canvas>
Run Code Online (Sandbox Code Playgroud)

  • 我说"有点",因为合成不像重构图纸那样灵活,首先看起来是正确的z-layered.合成只涉及2个"层".一层是画布上的现有像素,第二层是新绘制的像素.重构以绘制适当的分层图纸可以根据需要生成尽可能多的图层. (4认同)
  • 喜欢使用```toString(16)``` - 你教我一个新技巧:) (2认同)

nic*_*ous 7

我发现对我有用的解决方案(并且消除了闪烁,万岁!)是使用两个画布。(或者更多)

我假设您正在制作某种游戏(因为您提到了玩家)并以此为例。

您可以借鉴 Windows 的工作方式,首先将一个画布作为背景,再将另一个画布放在其上作为播放器画布。您可以将播放器画布标记为“脏”或在更改时进行更改,并且仅在需要时重新绘制更改的图层。这意味着您仅在玩家移动或采取操作时更新第二个画布。

此方法可以更进一步,您可以为 HUD 添加第三个画布,其中包含游戏统计数据,仅当玩家的统计数据发生变化时才会更改。

html 可能看起来像这样:

<canvas id="background-canvas" height="500" width="1000" style="border:1px solid #000000;"></canvas>
<canvas id="player-canvas" height="500" width="1000" style="border:1px solid #000000;"></canvas>
<canvas id="hud-canvas" height="500" width="1000" style="border:1px solid #000000;"></canvas>
Run Code Online (Sandbox Code Playgroud)


Joe*_*rry 5

只需先在其后面绘制事物,然后再绘制事物,然后再绘制其他对象。

要进行命中测试,您可能需要在显示列表上向后迭代,测试每个对象。如果您真的很了解对象边界,这将起作用。

或者,您可能想尝试一些标准的图形技巧,例如将相同的对象绘制到另一个内存画布上,并对每个绘制的对象使用唯一的颜色:要进行测试,只需检查内存画布上的像素颜色即可。整洁;-)


Sky*_*Key 5

或者,您可以简单地使用包含要绘制对象的数组,然后使用zIndex每个子级的属性对该数组进行排序。然后,您迭代该数组并绘制。

var canvas = document.querySelector('#game-canvas');
var ctx = canvas.getContext('2d');

// Define our shape "class".
var Shape = function (x, y, z, width, height) {
  this.x = x;
  this.y = y;
  this.zIndex = z;
  this.width = width;
  this.height = height;
};
// Define the shape draw function.
Shape.prototype.draw = function () {
  ctx.fillStyle = 'lime';
  ctx.fillRect(this.x, this.y, this.width, this.height);
};

// let's store the objects to be drawn.
var objects = [
  new Shape(10, 10, 0, 30, 30), // should be drawn first.
  new Shape(50, 10, 2, 30, 30), // should be drawn third.
  new Shape(90, 10, 1, 30, 30)  // should be drawn second.
];

// For performance reasons, we will first map to a temp array, sort and map the temp array to the objects array.
var map = objects.map(function (el, index) {
  return { index : index, value : el.zIndex };
});

// Now we need to sort the array by z index.
map.sort(function (a, b) {
  return a. value - b.value;
});

// We finaly rebuilt our sorted objects array.
var objectsSorted = map.map(function (el) {
  return objects[el.index];
});

// Now that objects are sorted, we can iterate to draw them.
for (var i = 0; i < objectsSorted.length; i++) {
  objectsSorted[i].draw();
}

// Enjoy !
Run Code Online (Sandbox Code Playgroud)

请注意,我没有测试该代码并将其写在我的手机上,因此可能会有错别字,但我希望这应该可以理解该原理。