This is the code that adds the Function (Canvas Drawing) To the sameTree array
let TreesI = 5;
let sameTree = [];
let randomValueX = mathR(-20, 20);
let randomValueY = mathR(-20, 20);
for (let i = 0; i < TreesI; i++) {
randomValueX = mathR(-20, 20);
randomValueY = mathR(-20, 20);
sameTree[TreesI] = Tree7(randomValueX, randomValueY)
}
But it didn't do anything,
The console.log output gave this
[5: undefined] (6) = $2
This is a sample of Tree7
function Tree7(startWidth, startHeight) {
ctx.save();
let treeObj = {
translateValues: {
startWidth: startWidth,
startHeight: startHeight,
endWidth: -startWidth,
endHeight: -startHeight
},
scaleR: mathR(0.8, 1)
}
ctx.translate(treeObj.translateValues.startWidth, treeObj.translateValues.startHeight);
ctx.scale(treeObj.scaleR,treeObj.scaleR);
//ctx.strokeStyle = 'rgb(0, 100, 1)';
ctx.strokeStyle = 'black';
ctx.globalAlpha = 0.5;
ctx.lineWidth = 0.2;
var reduce = 2;
//Tree drawing sample under
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(mathR(0,10),0);
ctx.stroke();
// Tree drawing sample above
ctx.translate(treeObj.translateValues.endWidth, treeObj.translateValues.endHeight);
ctx.restore();
}
Now after getting some answers I realised that I need to stop updating the random value ctx.lineTo(mathR(0,10),0);. The code I have currently just keeps the coordinates. Please help.
sameTree[i]instead ofsameTree[TreesI]?Tree7function looks like.Tree7function which is leadingundefinedreturn (value1, value2, ...). A function can only return one value. So you could put all your values you wish to return into an object or an array, and then return that one.