使用Konva验证HTML编号输入中的值

时间:2016-10-24 01:44:21

标签: javascript html konvajs

我正在开发一个可以使用konva绘制图形的网站,但我目前遇到了这个奇怪的问题:

如果我将每个刻度的单位更改为*.5以外的任何值或整数,它将变为如下:

enter image description here

以下是代码:

https://codepen.io/ItsTheWolf/pen/jrdkNX

var stage, axeslayer, gridslayer, axes, grids, errTxt;
var w, h;
var linex, liney;
var centx, centy;
var maxx, maxy, miny;
var x, y;
var distx, disty;
var i, j;
var tg = 1, err = 0;
document.getElementById('togG').disabled = true; // disable button Toggle Grids

function draw() {
    getValues(); // get values from input box
    createStage(); // create stage
    validate();
    if (err === 0) {
        drawAxes(); // draw axes
        drawGrids(); // draw grids
    } else {
        displayErr(); // display error message if validation failed
    }
}

function getValues() {
    w = parseFloat(document.getElementById('w').value); // get value for Konva.Stage width, convert to float
    h = parseFloat(document.getElementById('h').value); // get value for Konva.Stage height, convert to float
    maxy = parseFloat(document.getElementById('by').value); // get value for maximum number of y, convert to float
    miny = parseFloat(document.getElementById('sy').value); // get value for minimum number of y, convert to float
    upty = parseFloat(document.getElementById('upty').value); // get value for unit per tick of y axis, convert to float
    maxx = parseFloat(document.getElementById('bx').value); // get value for maximum number of x, convert to float
    minx = parseFloat(document.getElementById('sx').value); // get the value for minimum number of x, convert to float
    uptx = parseFloat(document.getElementById('uptx').value); // get value for unit per tick of x axis, convert to float
}

function validate() {
    if (minx < 0 || upty <= 0 || uptx <= 0 || upty > maxy - miny || uptx > maxx - minx) {
        err = 1;
    } else {
        err = 0;
    }
}

function createStage() {
    stage = new Konva.Stage({
        container: 'container',
        width: w,
        height: h
    });
}

function drawAxes() {
    axeslayer = new Konva.Layer();

    axes = new Konva.Shape({
        sceneFunc: function (ctx) {
            linex = stage.getWidth() - 70; // length of x axis
            liney = stage.getHeight() - 40; // length of y axis

            //count the elements on x axis
            j = 0;
            for (x = minx; x <= maxx; x = x + uptx) {
                j++;
            }

            // count the elements on y axis
            i = 0;
            for (y = miny; y <= maxy; y = y + upty) {
                i++;
            }

            distx = linex / (j - 1); // distance between the elements on x axis
            disty = liney / (i - 1); // distance between the elements on y axis

            // draw y axis
            ctx.beginPath();
            ctx.strokeStyle = '#888';
            ctx.moveTo(20, 20);
            ctx.lineTo(20, stage.getHeight() - 20);
            ctx.stroke();
            ctx.closePath();

            // draw elements of y axis
            i = 0;
            for (y = maxy; y >= miny; y = y - upty) {
                i++; //count the elements on y axis
                posy = disty * i - (disty - 20); // position of elements on y axis

                ctx.fillText(y, 0, posy);

                // draw x axis at y = 0
                if (y === 0) {
                    ctx.beginPath();
                    ctx.strokeStyle = '#888';
                    ctx.moveTo(20, posy);
                    ctx.lineTo(stage.getWidth() - 50, posy);
                    ctx.stroke();
                    ctx.closePath();
                    j = 0;
                    for (x = minx; x <= maxx; x = x + uptx) {
                        j++; //count the elements on x axis
                        posx = distx * j - (distx - 20); // position of elements on y axis

                        ctx.fillText(x, posx + 5, posy + 10);
                    }
                }
            }
        }
    });
    axeslayer.add(axes);
    stage.add(axeslayer);
    document.getElementById('togG').disabled = false;
}

function drawGrids() {
    gridslayer = new Konva.Layer();

    grids = new Konva.Shape({
        sceneFunc: function (ctx) {
            ctx.strokeStyle = '#ddd';

            // draw lines of y axis
            i = 0;
            for (y = maxy; y >= miny; y = y - upty) {
                i++; //count the elements on y axis
                posy = disty * i - (disty - 20); // position of elements on y axis

                ctx.beginPath();
                ctx.moveTo(20, posy);
                ctx.lineTo(stage.getWidth() - 50, posy);
                ctx.stroke();
                ctx.closePath();
            }

            // draw lines of x axis
            j = 0;
            for (x = 0; x <= maxx; x = x + uptx) {
                j++; //count the elements on x axis
                posx = distx * j - (distx - 20); // position of elements on x axis

                ctx.beginPath();
                ctx.moveTo(posx, 20);
                ctx.lineTo(posx, stage.getHeight() - 20);
                ctx.stroke();
                ctx.closePath();
            }
        }
    });
    gridslayer.add(grids);
    stage.add(gridslayer);
}

function toggleGrids() {
    if (tg === 1) {
        gridslayer.clear();
        tg = 0;
    } else if (tg === 0) {
        gridslayer.add(grids);
        stage.add(gridslayer);
        tg = 1;
    }
}

function displayErr() {
    error = new Konva.Layer();

    errTxt = new Konva.Shape({
        sceneFunc: function(ctx) {
            ctx.fillText("There's an error in the input field, please recheck." , stage.getHeight() / 2, 50);
        }
    });
    error.add(errTxt);
    stage.add(error);
}

如果HTML5代码有帮助,请点击这里:

<body>
    <table width="100%">
        <tr>
            <td width="25%">Width : <input type="number" id="w" value ="500"/></td>
            <td width="25%">Height : <input type="number" id="h" value ="200"/></td>
            <td width="25%"></td>
            <td width="25%"></td>
        </tr>
        <tr>
            <td width="25%">Max y : <input type="number" id="by" value ="2.5"/></td>
            <td width="25%">Min y : <input type="number" id="sy" value ="-0.5"/></td>
            <td width="25%">Unit per ticks y : <input type="number" id="upty" value ="0.5"/></td>
            <td width="25%"></td>
        </tr>
        <tr>
            <td width="25%">Max x : <input type="number" id="bx" value ="400"/></td>
            <td width="25%">Min x : <input type="number" id="sx" value ="0" min="0"/></td>
            <td width="25%">Unit per ticks x : <input type="number" id="uptx" value ="100"/></td>
            <td width="25%"><button onclick="draw();">Change</button><br/></td>
        </tr>
    </table>
    <div id="container"></div>
    <button onclick="toggleGrids();" id="togG" disabled>Toggle Grid</button><br/>
</body>

另外,有没有办法检查输入type = "number"中的值是空的还是奇怪的(例如当用户输入0-5时)然后设置err = 1?我尝试了类似minx === nullminx.length === 0的内容,但它无法正常工作。

1 个答案:

答案 0 :(得分:1)

那是因为Javascript浮动除法。您所需要的只是转换您的&#34; y&#34;轴标签固定:

 ctx.fillText(y.toFixed(1), 0, posy);

https://codepen.io/lavrton/pen/LRqaJr

这些问题可能会对您有所帮助: