function Piano(width, height, widthRatio, heightRatio) {
const numKeys = 88;
const isWhite = keyIndex => ![1, 4, 6, 9, 11].includes(keyIndex % 12);
const numLowerWhites = keyIndex => (
d3.range(keyIndex).reduce((acc, val) => (isWhite(val) ? acc+1 : acc), 0)
);
const numWhiteKeys = numLowerWhites(numKeys);
const whiteWidth = width / numWhiteKeys;
const blackWidth = whiteWidth * widthRatio;
return d3.range(numKeys).map(i => {
const offset = isWhite(i) ? 0 : -blackWidth/2;
return {
index: i,
type: isWhite(i) ? "white" : "black",
coord: {
x: {
min: whiteWidth * numLowerWhites(i) + offset,
max: whiteWidth * numLowerWhites(i) + offset + (isWhite(i) ? whiteWidth : blackWidth)
},
y: {
min: isWhite(i) ? 0 : (1 - heightRatio) * height,
max: height
}
}
};
})
}