Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sort these canvas elements by their z-index?

I want to merge these canvas into one with the largest z-index on top.

<div id="sketchpad_container">
  <canvas id="sketchypad_layer_0" style="z-index: 20; " width="800" height="600">Your browser does not support canvas</canvas>
  <canvas id="sketchypad_layer_1" style="z-index: 10; " width="800" height="600">Your browser does not support canvas</canvas>
  <canvas id="sketchypad_layer_2" style="z-index: 30; " width="800" height="600">Your browser does not support canvas</canvas>
</div>

Given an arbitrary number of such canvas layers I want to sort by the z-index and then use drawImage to combine them into one image in order to export it. Is there an easy by to sort by z-index?

EDIT - I tried the sort function as suggested but doesn't seem to work.

Here is my console output:

$("#sketchpad_container canvas"); //see what's in the collection
//console output gives back (you'll notice z-indices are 40, 20, 10, 30)
[
  <canvas id=​"sketchypad_interactive_layer" class=​"sketchypad_sketch_layer" style=​"z-index:​40" width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​, 
  <canvas id=​"sketchypad_layer_0" class=​"sketchypad_sketch_layer" style=​"z-index:​ 20;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​,
  <canvas class=​"sketchypad_sketch_layer" width=​"800" height=​"600">​,
  <canvas id=​"sketchypad_layer_1" class=​"sketchypad_sketch_layer" style=​"z-index:​ 10;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​,
  <canvas id=​"sketchypad_layer_2" class=​"sketchypad_sketch_layer" style=​"z-index:​ 30;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​
]

 //next try to sort the collection
 $("#sketchpad_container canvas").toArray().sort(function(a,b){a.style.zIndex - b.style.zIndex});
 //but array is still the same order of z-index 40, 20, 10, 30.
 //  I was expecting 40,30,20,10
 [
   <canvas id=​"sketchypad_interactive_layer" class=​"sketchypad_sketch_layer" style=​"z-index:​40" width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​,
   <canvas id=​"sketchypad_layer_0" class=​"sketchypad_sketch_layer" style=​"z-index:​ 20;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​,
   <canvas class=​"sketchypad_sketch_layer" width=​"800" height=​"600">​,
   <canvas id=​"sketchypad_layer_1" class=​"sketchypad_sketch_layer" style=​"z-index:​ 10;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​,
   <canvas id=​"sketchypad_layer_2" class=​"sketchypad_sketch_layer" style=​"z-index:​ 30;​ " width=​"800" height=​"600">​Your browser does not support canvas​</canvas>​
  ]

What am I doing wrong?

EDIT AGAIN -- oh nevermind. I need to include a "return" statement. gah!

like image 800
Homan Avatar asked Jan 17 '23 20:01

Homan


1 Answers

Assuming all your canvas elements have an assigned style value that sets their z-index, you can get an array of all the canvas elements sorted by z-index like this:

var items = $("#sketchpad_container canvas").toArray();
items.sort(function(a, b) {
    return(Number(a.style.zIndex) - Number(b.style.zIndex));
});

You can then presumably iterate through that array calling drawImage() on each. For proper z order rendering, you will want to draw the back images first (the lowest z-index items) which per the sort above will mean you draw from the beginning of the array to the end.

like image 186
jfriend00 Avatar answered Jan 28 '23 00:01

jfriend00