私は次のコードを持っています:
<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>
この質問に問題があります。ユーザーが色を選択して正方形または三角形をクリックすると、選択した色で描画されます。何を変更して追加する必要がありますか?