简体   繁体   中英

How to give a unique “id” for each canvas?

Canvas is new to me - so I need help.

I have a function that draws data retrieved using getImageData () - on an existing canvas or newly created.

I need to make it possible for a newly created canvas to give it a unique id. However, when I try the following - the canvas is created but the id is undefined.

function drawImageData(img, t1, t2, cc, scale, targetCanvas, userId) {
var canvas = targetCanvas || document.createElement('canvas');
if(!targetCanvas){
    canvas.setAttribute('id', userId); 

   // the rest of the code
   }

How to properly assign userId - to "id" canvas?

Your code seems to be working fine.

在此处输入图片说明

jsfiddle.net/qsekzt9x/

@gurvinder372 is right about the if condition in his answer below

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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