我有以下代码:
<html>
<head>
</head>
<body>
<canvas id="canvasId" width="300" height="300"></canvas>
<script>
function square() {
var ctx = document.getElementById('canvasId').getContext('2d');
var col= document.getElementById("clr");
ctx.beginPath();
ctx.rect(10, 10, 70, 70);
ctx.fillStyle = 'yellow';
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = 'black';
ctx.stroke();
}
function triangle () {
var canvas = document.getElementById('canvasId');
var context = canvas.getContext('2d');
var col= document.getElementById("clr");
context.beginPath();
context.rect(85, 80, 200, 100);
context.fillStyle = 'yellow';
context.fill();
context.lineWidth = 2;
context.strokeStyle = 'black';
context.stroke();
}
</script>
<p>Color <input type="color" id="clr" value="" /></p>
<input type="button" value="square" onclick="square()" />
<input type="button" value="triangle" onclick="triangle()" />
</body>
</html>
我对这个问题有疑问,我想当用户选择颜色并单击正方形或三角形时,它会用他/她选择的颜色绘制它,我应该更改和添加什么?