1 回答
TA贡献1878条经验 获得超4个赞
好吧,看来我是以完全错误的方式开始的,所以这里有一个足够不同的解决方案。这是放在绘制函数中,取自画布本身。
canvas.onclick = function mousePos(Event) {
var rect = canvas.getBoundingClientRect();
var posX = Event.clientX- rect.left;
var posY = Event.clientY - rect.height;
console.log("X = "+posX +" Y = "+ posY);
var barWidth= 500/barVals.length;
console.log("Bar Width = "+barWidth);
var barNum;
barNum = Math.floor((posX-60) / barWidth);
if(barNum>=0 && barNum < barVals.length) {
var position = barNum;
console.log(position);
var amount = prompt("Please enter the new value", "734");
//barVals[position] = amount;
if(amount >0) {
barVals.splice(position, 1, parseInt(amount));
console.log(amount);
draw(); // redraw
}
else if(amount = -1){
barVals.splice(position, 1);
colours.splice(position, 1)
draw(); // redraw
}else{
alert("Incorrect value entered");
}
}
}
添加回答
举报