我想要繪製在畫布上的繪畫中的橢圓選項。我已經部分實現了這一點。問題是我不能得到圓的半徑,目前我已經硬編碼到15。我也想繪製橢圓(與油漆相同)不是確切的圓。 這是我使用鼠標事件在畫布上繪製圓的代碼。請幫我用代碼實現我上面提到的要求。使用鼠標事件在HTML5畫布上繪製圓/橢圓
function tool_circle() {
var tool = this;
this.started = false;
this.mousedown = function (ev) {
tool.started = true;
tool.x0 = ev._x;
tool.y0 = ev._y;
};
this.mousemove = function (ev) {
if (!tool.started) {
return;
}
context.fillStyle = 'red';
var distance = Math.sqrt(Math.pow(tool.x0 - ev._x, 2) + Math.pow(tool.y0 - ev._y));
context.beginPath();
context.arc(tool.x0, tool.y0,15, 0, Math.PI * 2, false);
context.stroke();
context.fill();
};
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
img_update();
}
};
}
@markE你好,我想做同樣的事情了移動應用程序(Phonegap包裹)觸摸事件。我試過你的代碼,只是通過替換mouseup,mousemove與touchstart,touchmove事件。這些事件正在觸發,因爲我可以看到日誌,但不會繪製橢圓/圓。我需要爲觸摸事件做些什麼特別的事情?在此先感謝(我第一次使用畫布:)) – hashcoder