2016-11-07 4 views
0

入力の値を取得してグリッドの正方形のサイズにするにはどうすればよいですか?私は入力値を使用すると、私のグリッドが台無しですが、私はハードコーディンググリッドの正方形のサイズが正常に動作します。ここでHTML5キャンバスグリッド - 入力からグリッドの正方形のサイズを取得する

はcodepenです:https://codepen.io/ryan_pixelers/pen/NbqKMQ

、ここではコードです:

// get our canvas 
var grid = document.getElementById('grid'); 
hide(grid); 

var horizontalSpace = 0; 
var verticalSpace = 0; 

var button = document.getElementById('gridControls'); 
button.onclick = drawCanvas; 


function drawCanvas() { 

show(grid); 
var context = grid.getContext('2d'); 

// HERE: Why doesn't this work??? 
// var gridSize = document.getElementById('widthHeight').value; 
var gridSize = 10; 

// set the canvas style to positions absolute 
// and set z-index high to keep it on top 
grid.style.position = 'absolute'; 
grid.style.zIndex = '100000'; 

// get window width and height 
var w = window.innerWidth; 
var h = window.innerHeight; 

// set the canvas width and height to be the window w/h 
grid.width = w; 
grid.height = h; 

// create the horizontal lines 
for(var horizontal = 0; horizontal < w; horizontal++) { 
    context.beginPath(); 
    context.moveTo(0, horizontalSpace); 
    context.lineTo(w, horizontalSpace); 
    context.lineWidth = 1; 
    context.strokeStyle = "lightblue"; 
    context.stroke(); 
    horizontalSpace += gridSize; 
} 

// create the vertical lines 
for(var vertical = 0; vertical < h; vertical++) { 
    context.beginPath(); 
    context.moveTo(verticalSpace, 0); 
    context.lineTo(verticalSpace, h); 
    context.lineWidth = 1; 
    context.strokeStyle = "lightblue"; 
    context.stroke(); 
    verticalSpace += gridSize; 
} 

} 

function hide(element) { 
    element.style.display = 'none'; 
} 

function show(element) { 
    element.style.display = 'block'; 
} 

は、私は、入力フィールドの値としてグリッド寸法を設定し、その結果を見てみ行をコメントアウトしてみます。それはグリッドスペースを台無しにする!

これを解決する方法はありますか?

ありがとうございます!

答えて

0

OK、わかった:値にparseInt()を使う必要があります。

関連する問題