我有一个 ID 为“draw”的按钮,并且每次单击该绘制按钮时都想要处理下一个操作(即在我的情况下为形状)。我尝试了 style.visibility 但没有奏效。也许我做错了。但是,这里是代码:
window.onload = function() {
var canvas = document.getElementById('myCanvas'),
draw = document.getElementById('draw'),
clr = document.getElementById('clear'),
context = canvas.getContext('2d');
var grad = context.createLinearGradient(100,0,200,0);
function shapeOne(){
context.beginPath(); // circle
context.fillStyle = 'blue';
context.arc(200, 100, 100, 50 , 2 * Math.PI, false);
context.fill();
context.lineWidth = '1';
context.stroke();
context.closePath();
}
function shapeTwo(){
context.beginPath(); // rectangle
grad.addColorStop(0,"#0033cc");
grad.addColorStop(1,"#0066ff");
context.fillStyle=grad;
context.fillRect(120,40,160,120);
context.strokeStyle="black";
context.strokeRect(120,40,160,120);
context.closePath();
}
function shapeThree(){
context.beginPath(); // line
context.moveTo(280, 40);
context.lineTo(120, 160);
context.stroke();
context.closePath();
}
draw.addEventListener('click', shapeOne, false);
draw.addEventListener('click', shapeTwo, false);
draw.addEventListener('click', shapeThree, false);
clr.addEventListener('click', function(){
canvas.width = canvas.width;
}, false);
};