定義變數:
[javascript]
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = document.getElementById("myCanvas"); = canvas.getContext("2d");
var select = document.getElementsByTagName("select");
var startX;
var startY;
var endX;
var; 🎜>var radius;
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var select = document.getElementsByTagName("select");
函數部分:
[javascript]
複製程式碼
複製程式碼
複製程式碼
複製程式碼
複製程式碼
複製程式碼
複製碼程式碼如下:
window.onload=function() {
canvas.onmousedown = function(e) {
;
startY = e.clientY;
if(select[0].value == "arc") {
canvas.onmousemove = moveShowRect;
}
}
canvas.onmouseup = function() {
canvas.onmousem = ""); {
context.clearRect(0, 0, 500, 300);
endX = e.clientX - startX;
endY = e.clientY - startYgin;
endY = e.clientY - startYgin;
endY = e.clientY - startYgin; rect(startX, startY, endX, endY);
context.fillStyle = "#8ED6FF";
context.fill();
";
context.stroke();
}
function moveShowArc(e) {
context.clearRect(0, 0, 500, 300); endY = e.clientY - startY;
radius = Math.sqrt(Math.pow(endX,2) Math.pow(endY,2));
context.begin(endY,2));
context.begin(); > context.arc(startX, startY,radius,0,2 * Math.PI,false);
context.fillStyle = "#8ED6FF";
context.fill(); 3;
context.strokeStyle = "black";
context.stroke();
}
window.onload=function() { . 🎜> e = e || event;
startX = e.clientX;
startY = e.clientY;
if(select[0].value == "arcc") { if(select[0].value == "arcc") { if(select. onmousemove = moveShowArc;
} else {
canvas.onmousemove = moveShowRect;
}
= "";
}
}
function moveShowRect(e) {
context.clearRect(0, 0, 500, 300);
c - startY;
context.beginPath();
context.rect(startX, startY, endX, endY);
context.fillStyle = "#8ED6"; 🎜> context.lineWidth = 3;
context.strokeStyle = "black";
context.stroke(); endX = e.clientX - startX;
endY = e.clientY - startY;
endY = e.clientY - startY;
radius = Math.sqrt(Math.A. endY,2));
context.beginPath();
context.arc(startX, startY,radius,0,2 * Math.PI,false);
context.fillStyle = "#86le ; context.fill(); context.lineWidth = 3; context.strokeStyle = "black"; context.strokeStyle = "black"; >