Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

405
Views
How do I add canvas drawings to an array and determine if an array value is an image or canvas drawing?

I need an array which can include canvas drawings and/or images.

Drawing example below:

function drawCircle() {
ctx.beginPath();
ctx.arc(Life.x, Life.y, Life.radius, 0, Math.PI * 2);
ctx.fillStyle = 'black;
ctx.fill();
ctx.strokeStyle = 'orange';
ctx.stroke();
ctx.closePath();
}

Image example below:

const LIFE_IMG=new Image();
LIFE_IMG.src="images/life.png"

Array;

let LIFE_ARRAY=[LIFE_IMG,LIFE_IMG,LIFE_IMG];

Draw:

function drawLife(imgY2,imgWidth,imgHeight){
LIFE_ARRAY.forEach(function(item, index) {

let offsetY,imgX,imgY;
offsetY=(imgHeight+5)*(index+1)
imgX=(SCOREBOARD_WIDTH/2)-(imgWidth/2)
imgY=imgY2
    }
ctx.drawImage(item, imgX, imgY+offsetY, imgWidth, imgHeight);
  })
}

I need to be able to

  1. push the canvas drawing to the array

  2. determine if the array value is an image or canvas drawing

  3. draw the canvas drawing or the image

Part 2 was answered on an old stack overflow thread with:

function isImage(i) {return i instanceof HTMLImageElement;}

but I can't figure out how to tie it into my code.

Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usually you just have one on-screen <canvas> element. So your sample code:

let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');

let Life = {
  x: 100,
  y: 100,
  radius: 50
};

function drawCircle() {
  ctx.beginPath();
  ctx.arc(Life.x, Life.y, Life.radius, 0, Math.PI * 2);
  ctx.fillStyle = 'black';
  ctx.fill();
  ctx.strokeStyle = 'orange';
  ctx.stroke();
  ctx.closePath();
}

drawCircle();
<canvas id="canvas"></canvas>

draws a circle on the on-screen canvas' context. This is just a drawing operation though, so there is no reference to it making it sort of an object. In the end it's just pixel data somewhere on the canvas.

Although I don't see an use-case, what you want to achieve can be done of course. The trick here is to create individual off-screen canvases for all canvas drawings you want to store in the array.

A <canvas> element can be created at runtime using:

document.createElement('canvas')

If we modify your drawCircle() function a little so it draws to an off-screen canvas and returns it, you can even call this function from within your array to push it's reference into.

function drawCircle() {
  let tempCanvas=document.createElement('canvas');
  let tempCtx=tempCanvas.getContext('2d');
    tempCtx.beginPath();
    tempCtx.arc(Life.x, Life.y, Life.radius, 0, Math.PI * 2);
    tempCtx.fillStyle = 'black';
    tempCtx.fill();
    tempCtx.strokeStyle = 'orange';
    tempCtx.stroke();
    tempCtx.closePath();
  return tempCanvas;
}

As you already figured yourself an image is an instance of HTMLImageElement. Likewise a canvas is of HTMLCanvasElement. This can be used in the forEach loop to distinct one from the other.

Here's an example:

let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');

let Life = {
    x: 100,
    y: 100,
    radius: 50
};

function drawCircle() {
    let tempCanvas = document.createElement('canvas');
    let tempCtx = tempCanvas.getContext('2d');
    tempCtx.beginPath();
    tempCtx.arc(Life.x, Life.y, Life.radius, 0, Math.PI * 2);
    tempCtx.fillStyle = 'black';
    tempCtx.fill();
    tempCtx.strokeStyle = 'orange';
    tempCtx.stroke();
    tempCtx.closePath();
    return tempCanvas;
}

drawCircle();

const LIFE_IMG = new Image();
LIFE_IMG.src = "images/life.png";

let LIFE_ARRAY = [LIFE_IMG, drawCircle(), LIFE_IMG];

LIFE_ARRAY.forEach(function(item, index) {
    if (item instanceof HTMLImageElement) {
        console.log("an Image");
    }
    if (item instanceof HTMLCanvasElement) {
        console.log("a Canvas");
    }
});
 <canvas id="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!