简体   繁体   中英

Can I use an array to reference the context of layered canvases?

I have written a function that's capable of creating multiple layered canvases and I want to reference the context of each with an array (ctx[n]) but I am really struggling to understand what I'm doing wrong. I'm fairly new to javascript so would appreciate any help.

I've separated out my code into a test document(see below). I'm using [0] as a testing base. If I substitute ctx[0] with any other variable name the code works. Am I doing something really stupid? or is it just not possible to use an array for this purpose?

var ctx = [];
var canv = [];
txt.innerHTML = "Paragraph text has been changed";
createCanv(0, 0, 0, 250, 100, 2);
textFade(1, "Hello!");

function textFade(ctx, text){
    ctx[0].clearRect(0, 0, canv[0].width, canv[0].height);
    ctx[0].font = "bold 25px verdana";
    ctx[0].fillStyle = "rgba(255, 0, 0, 0.9)";
    ctx[0].fillText(text,canv[0].width/2,canv[0].height/2);
}

function createCanv(c, x, y, w, h, z) {
    canv[0] = document.createElement("canvas");
    canv[0].id = "canv" + c;
    document.body.appendChild(canv[0]);
    canv[0].style.background = "rgba(196, 196, 255, 0.8)";
    canv[0].style.position = "absolute";
    canv[0].style.top = y + "px";
    canv[0].style.left = x + "px";
    canv[0].style.zIndex = z;
    canv[0].width = w;
    canv[0].height = h;
}

You call:

textFade(1,"Hello!");

But the signature is:

function textFade(ctx,text)

So you pass the index not the object or array.

You should change your code like so:

function textFade(ctx,text){
    canv[ctx].clearRect(0,0,canv[0].width,canv[0].height);
    canv[ctx].font="bold 25px verdana";
    canv[ctx].fillStyle="rgba(255,0,0,0.9)";
    canv[ctx].fillText(text,canv[0].width/2,canv[0].height/2);
}

I do not know if you simply did not supply the relevant code, or you haven't implemented it, but I image it should look like this.

var ctx=[];
var canv=[];
txt.innerHTML="Paragraph text has been changed";
var canvas = createCanv(0, 0, 0, 250, 100, canv.length); // this assumes each new canvas will sit on top of the previous
canv.push(canvas);
ctx.push(canvas.getContext("2d"));
textFade(ctx.length - 1,"Hello!");

function textFade(ctxInd,text){
   ctx[ctxInd].clearRect(0,0,canv[ctxInd].width,canv[ctxInd].height);
   ctx[ctxInd].font="bold 25px verdana";
   ctx[ctxInd].fillStyle="rgba(255,0,0,0.9)";
   ctx[ctxInd].fillText(text,canv[ctxInd].width/2,canv[ctxInd].height/2);
}

function createCanv(c, x, y, w, h, z) {
   var can = document.createElement("canvas");
   can.id = "canv" + c;
   document.body.appendChild(can);
   can.style.background = "rgba(196,196,255,0.8)";
   can.style.position = "absolute";
   can].style.top = y + "px";
   can.style.left = x + "px";
   can.style.zIndex = z;
   can.width = w;
   can.height = h;
   return can;
}

I would also not set all those css properties this way. Use a style class to modify all canvases with the common settings and then manually set the z index.

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