canvas3 = {
const scaleFactor = size;
const width = 400;
const height = scaleFactor * 200;
const context = DOM.context2d(width, height);
const canvas = context.canvas;
const layerOptions = [
["2% reduced fat", "1% reduced fat", "Soy milk", "Almond milk", "Oat milk"],
["Zero Calorie sugar", "Sugar(Normal)", "Honey", "Maple Syrup"],
["Coffee Type 1", "Coffee Type 2", "Coffee Type 3"],
["Cream", "No Cream"]
];
const layerSizeMultipliers = [1,1,1,1];
context.clearRect(0, 0, width, height);
const cupWidthTop = 60 * scaleFactor;
const cupWidthBottom = 40 * scaleFactor;
const cupHeight = 120 * scaleFactor;
const cupX = width / 2;
const cupY = 60;
const colors = ["grey", "#f5f5dc", "brown", "#fffdd0"];
const layerHeight = cupHeight / 4;
const layers = [];
for (let i = 0; i < 4; i++) {
const topY = cupY + i * layerHeight;
const bottomY = topY + layerHeight;
const topWidth = cupWidthTop - (i * (cupWidthTop - cupWidthBottom) / 4);
const bottomWidth = topWidth - ((cupWidthTop - cupWidthBottom) / 4);
layers.push({
index: i,
top: topY,
bottom: bottomY,
left: cupX - bottomWidth / 2,
right: cupX + bottomWidth / 2,
color: colors[i]
});
context.fillStyle = colors[i];
context.beginPath();
context.moveTo(cupX - topWidth / 2, topY);
context.lineTo(cupX + topWidth / 2, topY);
context.lineTo(cupX + bottomWidth / 2, bottomY);
context.lineTo(cupX - bottomWidth / 2, bottomY);
context.closePath();
context.fill();
}
context.strokeStyle = "rgb(0, 128, 0)";
context.lineWidth = 2;
context.beginPath();
context.moveTo(cupX - cupWidthTop / 2, cupY);
context.lineTo(cupX + cupWidthTop / 2, cupY);
context.lineTo(cupX + cupWidthBottom / 2, cupY + cupHeight);
context.lineTo(cupX - cupWidthBottom / 2, cupY + cupHeight);
context.closePath();
context.stroke();
function redrawCanvas() {
context.clearRect(0, 0, width, height);
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
const scaleFactor = layerSizeMultipliers[i]; // Use the multiplier for the layer size
const heightAdjustment = layerHeight * scaleFactor;
const topY = cupY + (i * heightAdjustment);
const bottomY = topY + heightAdjustment;
const topWidth = cupWidthTop - (i * (cupWidthTop - cupWidthBottom) / 4) * scaleFactor;
const bottomWidth = topWidth - ((cupWidthTop - cupWidthBottom) / 4) * scaleFactor;
context.fillStyle = layer.color;
context.beginPath();
context.moveTo(cupX - topWidth / 2, topY);
context.lineTo(cupX + topWidth / 2, topY);
context.lineTo(cupX + bottomWidth / 2, bottomY);
context.lineTo(cupX - bottomWidth / 2, bottomY);
context.closePath();
context.fill();
}
context.strokeStyle = "rgb(0, 128, 0)";
context.lineWidth = 2;
context.beginPath();
context.moveTo(cupX - cupWidthTop / 2, cupY);
context.lineTo(cupX + cupWidthTop / 2, cupY);
context.lineTo(cupX + cupWidthBottom / 2, cupY + cupHeight);
context.lineTo(cupX - cupWidthBottom / 2, cupY + cupHeight);
context.closePath();
context.stroke();
}
// Setup dropdown for layer options
const dropdown = document.createElement('select');
dropdown.style.position = 'absolute';
dropdown.style.display = 'none';
document.body.appendChild(dropdown);
canvas.addEventListener('click', event => {
const { offsetX, offsetY } = event;
const clickedLayer = layers.find(layer =>
offsetX >= layer.left && offsetX <= layer.right &&
offsetY >= layer.top && offsetY <= layer.bottom
);
if (clickedLayer) {
dropdown.innerHTML = layerOptions[clickedLayer.index].map((opt, index)=> `<option value="${index +1}">${opt}</option>`).join('');
dropdown.style.left = `${event.pageX}px`;
dropdown.style.top = `${event.pageY}px`;
dropdown.style.display = 'block';
dropdown.onchange = () => {
layerSizeMultipliers[clickedLayer.index] = parseFloat(dropdown.value);
redrawCanvas();
};
} else {
dropdown.style.display = 'none';
}
});
return canvas;
}