私はjavascriptの初心者です。収集された入力値(幅と高さ)の取得方法がわからないので、widthとheightパラメータを取るupdateFormというメソッドを作成する必要があります。関数の本体では、入力フォーム上の幅と高さのフィールドを設定し、その長方形をキャンバスに描画するメソッドを呼び出すことができます。入力値を取得し、2つのパラメータで関数に挿入して矩形を作成しますか?
JSフィドル - https://jsfiddle.net/rtomino/hbgmd4sg/2/
<form id="areaform">
<label for="wid">Width:</label>
<input id="wid" type="number">
<label for="hgt">Height:</label>
<input id="hgt" type="number">
<button onclick="draw()" type="button">Draw Rectangle</button>
</form>
<br>
<div id="drawRectangle">
<canvas id="rectCanvas" width=500 height=500></canvas>
</div>
JS
var canvas,
context,
widthValue = document.getElementById('wid'),
heightValue = document.getElementById('hgt');
canvas = document.getElementById("rectCanvas");
context = canvas.getContext("2d");
function updateForm(width, height) {
'use strict';
}
function draw() {
context.clearRect(0, 0, canvas.width, canvas.height);
context.rect(0, 0, width, height);
context.fillStyle = "#EA7B00";
context.fill();
}
関数には引数がありますが、それらを呼び出すと引数が渡されます –