[英]Is it possible to reference multiple html5 canvases and variable names through a for loop?
I have 3 Picture "Names", "Xpositions", "Ypositions", "scale", and "color values" stored in various arrays, like so: 我有3个图片“名称”,“ Xpositions”,“ Ypositions”,“ scale”和“ color values”存储在各种数组中,如下所示:
pictureNames["blahblah1, "somePic2", "anotherPic3"];
pictureXcoordinates[0, 43, 56];
pictureYcoordinates[0, 10, 20];
pictureScales[100, 100, 100]; //meaning percent
pictureColorRvalue[0, 0, 0];
pictureColorGvalue[0, 0, 0];
pictureColorBvalue[0, 0, 0];
The PNG images are pre-loaded and assigned in variables as such: picture0, picture1, picture2. PNG图像已预加载并分配了变量,例如:picture0,picture1,picture2。
I'm trying to save many lines of coding by making a for loop to draw each picture, with its size and coordinates, into its OWN canvas element, and then colorize them by getting image data. 我试图通过制作一个for循环以将每张图片及其尺寸和坐标绘制到其OWN canvas元素中,然后通过获取图像数据来对其进行着色来节省很多代码行。 So like this: 像这样:
var counter;
for(counter = 0; counter <= 2; counter++){
firstCtx.drawImage(picture1 ,pictureXcoordinates[counter], pictureYcoordinates[counter], picture0.width, picture0.height);
firstCtx.restore();
var imgData = firstCtx.getImageData(pictureXcoordinates[counter], pictureYcoordinates[counter], canvasW, canvasH);
var i;
for (i = 0; i < imgData.data.length; i += 4) {
imgData.data[i] = pictureColorRvalue[counter];
imgData.data[i+1] = pictureColorGvalue[counter];
imgData.data[i+2] = pictureColorBvalue[counter];
}
firstCtx.putImageData(imgData, shapePositionX[counter], shapePositionY[counter]);
firstCtx.restore();
... but I don't know how to reference "firstCtx", secondCtx", "thirdCtx", using the counter variable.... should/can I rename the canvases contents "canvas1Ctx", "canvas2Ctx", etc.... and reference them with "canvas"+counter+"Ctx", or something like that? And reference "picture0", "picture1", and "picture2" as well. ...但是我不知道如何使用计数器变量来引用“ firstCtx”,secondCtx“,” thirdCtx“ ......应该/可以将画布内容重命名为” canvas1Ctx“,” canvas2Ctx“等。 ..并以“ canvas” + counter +“ Ctx”或类似的名称进行引用?并同时以“ picture0”,“ picture1”和“ picture2”进行引用。
This is how I would do it: Y would create a class of objects Picture
, each with it's x
, y
etc... and a function that handles the drawing process. 我就是这样做的:Y将创建一类对象Picture
,每个对象都有x
, y
等...以及一个处理绘制过程的函数。
let picturesArray = []
class Picture{
constructor(x,y,width,height,scale,color,pictureName,canvas){
this.x = x;
this.y = y;
this.w = width;
this.h = height;
this.scale = scale;
this.color = color;
this.picture = pictureName;
this.canvas = canvas;
this.ctx = this.canvas.getContext("2d")
}
draw(){
this.ctx.drawImage(this.picture ,this.x, this.y, this.w, this.h);
this.imgData = this.ctx.getImageData(this.x, this.y, this.canvas.width, this.canvas.height);
for (let i = 0; i < imgData.data.length; i += 4) {
this.imgData.data[i] = this.color.r;
this.imgData.data[i+1] = this.color.g;
this.imgData.data[i+2] = this.color.b;
}
this.canvas.putImageData(this.imgData, this.x, this.y);
}
}
Next I would create the objects picture and push the new picture into the pictures array: 接下来,我将创建对象图片并将新图片推入图片数组:
picturesArray.push(new Picture(x,y,width,height,scale,color,pictureName,canvas));
Finally you use a for
loop or a forEach
to draw all the pictures 最后,您使用for
循环或forEach
绘制所有图片
picturesArray.forEach(p=>{p.draw()})
I hope it helps. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.