I want to fill a canvas with a color that's an element of an array inside an object and the element index should be i but the code seems to be wrong. The variables inside color1 are already declared and contain a string which is the hex value for the color.
var colorsObj = {
color1: [orange,amber,apricot,tangerine,bittersweet,persimmon,salmon,peach,pumpkin]
}
function drawCanvas(color) {
for(var i = 1; i < 10; i++){
$('.app').append('<canvas class="shadescolors" id="shade'+i+'" width="100" height="100">');
var canvas = document.getElementById('shade'+i);
var context = canvas.getContext('2d');
canvas.width = window.innerWidth / 3;
cc = canvas.width;
radius = cc/2-10;
canvas.height = canvas.width;
context.beginPath();
context.arc(cc/2, cc/2, cc/2-10, 0, Math.PI*2, true);
alert(colorsObj.color[i]);
context.fillStyle = colorsObj.color[i];
context.fill();
context.lineWidth = 2;
context.strokeStyle = '#8A8A8A';
context.stroke();
}
}
drawCanvas('color1');
The alert doesn't fire either.
colorsObj.coloris not the same ascolorsObj.color1.color1. You are trying to access the index of an array namedcolorwhich doesn't exist. Secondly, your array contains a bunch of values which are probably all undefined. I think you mean to have them as stringscolors: ['orange, 'amber' ... etc ]Because you are passing a string into yourdrawCanvasfunction, it looks like you're trying to access the colorObjects array, but you're using the wrong syntax.colorObj['color1']would work. But I think you kinda have this all wrong, you may want to look up JavaScript objects & arrays