Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:47:09+00:00 2026-06-17T04:47:09+00:00

I have asked a question before: How can I control z-index of canvas objects?

  • 0

I have asked a question before: How can I control z-index of canvas objects? and we reached to a solution that may not be a good one for complicated situations.

I found that canvas doesn’t have a z-index system, but a simple ordered drawing one. Now there is a new question: how can I simulate z-index system to make this problem fixed in complicated situations?

The good answer can solve a big problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:47:10+00:00Added an answer on June 17, 2026 at 4:47 am

    It’s not that canvas doesn’t have a z-index, it’s that canvas doesn’t keep objects drawn contrary to the HTML page. It just draws on the pixel matrix.

    There are basically two types of drawing models :

    • object ones (usually vector) : objects are kept and managed by the engine. They can usually be removed or changed. They have a z-index
    • bitmap ones : there are no objects. You just change a pixel matrix

    The Canvas model is a bitmap one. To have objects drawn over other ones, you must draw them after. This means you must manage what you draw.

    The canvas model is very fast, but if you want a drawing system managing your objects, maybe you need SVG instead.


    If you want to use a canvas, then the best is to keep what you draw as objects.
    Here’s an example I just made : I keep a square list and every second I randomize their zindex and redraw them :

    var c = document.getElementById('c').getContext('2d');
    function Square(x, y, s, color) {
       this.x = x; this.y = y; this.s = s; this.color = color;
       this.zindex=0;
    }
    Square.prototype.draw = function(c) {
      c.fillStyle = this.color;
      c.fillRect(this.x, this.y, this.s, this.s);  
    }
    var squares = [
      new Square(10, 10, 50, 'blue'), new Square(40, 10, 40, 'red'), new Square(30, 50, 30, 'green'),
      new Square(60, 30, 40, '#111'), new Square(0, 30, 20, '#444'), new Square(70, 00, 40, '#999')
    ];
    
    function draw() {
      c.fillStyle = "white";
      c.fillRect(0, 0, 1000, 500);
      for (var i=0; i<squares.length; i++) squares[i].draw(c);
    }
    setInterval(function(){
      // give all squares a random z-index
      squares.forEach(function(v){v.zindex=Math.random()});
      // sort the list accordingly to zindex
      squares.sort(function(a,b){return a.zindex-b.zindex});
      draw();
    }, 1000);
    

    Demonstration

    The idea is that the square array is sorted accordingly to zindex. This could be easily extended to other types of objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked this question before but did not get the satisfied answer as
This question may have been asked before, but I had trouble finding an answer,
I have asked a similar question before, but didn't get very good results. I've
Yes other people have asked this question before, but not in the same context
I know many have asked this question before, but as far as I can
Ok, I see that people have asked this question before, but I'm seeing some
Fellow coders, I have asked this question before but did not get a conclusive
I had asked the similar question before , understood that i can make it
I have asked this question before and it seemed that the code I was
I have asked (and answered) a very similar question before. That question was able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.