给konva加个刻度尺

给konva加个刻度尺

最近在用konva做一些,一开始写了不少辅助函数。帮助自己给物体定位 ,现在贡献出来给大家用。

给图层增加刻度尺

顾名思义就是加个刻度显示,效果如下:

image-20230424160401359

代码:
第一个参数时layer,第二个是精度,返回一个layer

   stage.add(HelperLayer(deviceUpLayer, 50));

全部代码如下:

export function HelperLayer(layer, precision = 50) {
    const height = layer.height();
    const width = layer.width();
    const offset = {x: layer.offsetX(), y: layer.offsetY()}
    const littleLine = precision / 10;

    //辅助网格层
    let helperLayer = new Konva.Layer();
    const x = new Konva.Arrow({
        points: [offset.x, offset.y, width - offset.y, offset.y],
        stroke: 'red',
        strokeWidth: 2,
        lineCap: 'round',
        lineJoin: 'round',
        fill: 'black',
    })
    for (let j = 1; j < 10; j++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x + j * littleLine, offset.y, offset.x + j * littleLine, j === 5 ? offset.x + 15 : offset.x + 10],
            stroke: '#48f3cc',
            strokeWidth: 1,
        }))
    }
    const xLabel = new Konva.Text({
        x: width - 20,
        y: 0,
        text: 'x',
        fontSize: 20,
        fill: 'yellow',
    })
    const y = new Konva.Arrow({
        points: [offset.x, offset.y, offset.x, height - offset.x],
        stroke: 'red',
        fill: 'black',
        strokeWidth: 2,
        lineCap: 'round',
        lineJoin: 'round',
    })
    for (let j = 1; j < 9; j++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x, offset.y + j * littleLine, j === 5 ? offset.y + 15 : offset.y + 10, offset.y + j * littleLine],
            stroke: '#48f3cc',
            strokeWidth: 1,
        }))
    }
    const yLabel = new Konva.Text({
        x: 10,
        y: height - 20,
        text: 'y',
        fontSize: 20,
        width: 100,
        fill: 'yellow',
    })
    helperLayer.add(x, y, xLabel, yLabel);

    helperLayer.add(new Konva.Text({
        x: offset.x,
        y: offset.y,
        text: "0,0",
        fontSize: 10,
        fill: 'yellow',
    }))

    // y轴刻度
    for (let i = 1; i < Math.floor(height / precision); i++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x, i * precision + offset.y, 25, i * precision + offset.y],
            stroke: 'red',
            strokeWidth: 1,
        }))
        for (let j = 1; j < 9; j++) {
            helperLayer.add(new Konva.Line({
                points: [offset.x, i * precision + offset.y + j * littleLine, j === 5 ? offset.y + 15 : offset.y + 10, i * precision + offset.y + j * littleLine],
                stroke: '#48f3cc',
                strokeWidth: 1,
            }))
        }

        helperLayer.add(new Konva.Text({
            x: offset.x,
            y: i * precision + offset.y - 10,
            text: '' + i * precision,
            fontSize: 10,
            fill: 'yellow',
        }))
    }

    // x轴刻度
    for (let i = 1; i < Math.floor(width / precision); i++) {
        helperLayer.add(new Konva.Line({
            points: [i * precision + offset.x, offset.y, i * precision + offset.x, 25],
            stroke: 'red',
            strokeWidth: 1,
        }))
        for (let j = 1; j < 9; j++) {
            helperLayer.add(new Konva.Line({
                points: [offset.x + i * precision + j * littleLine, offset.y, offset.x + i * precision + j * littleLine, j === 5 ? offset.x + 15 : offset.x + 10],
                stroke: '#48f3cc',
                strokeWidth: 1,
            }))
        }
        helperLayer.add(new Konva.Text({
            x: i * precision + offset.x - 10,
            y: offset.y,
            text: '' + i * precision,
            fontSize: 10,
            fill: 'yellow',
        }))
    }

    return helperLayer;
}

拖动显示坐标

效果如下,当你做拖动元素的时候会自动显示该元素的坐标。

image-20230424160611856

实现如下:

第一个参数是需要拖动的元素,第二个参数是元素所在的图层,无返回值

function showCoord(node, layer) {
    node.on('dragmove', function (event) {
        if (event.target.coord) {
            event.target.coord.destroy()
        }
        event.target.coord = new Konva.Text({
            x: event.target.attrs.x,
            y: event.target.attrs.y + 10,
            text: `(${event.target.attrs.x},${event.target.attrs.y})`,
            fontSize: 20,
            fill: '#bcef63',
        });
        layer.add(event.target.coord);
    });
}

总结

代码很简单,其实也可以更近一步,但是我犯懒了。# 给konva加个刻度尺

最近在用konva做一些,一开始写了不少辅助函数。帮助自己给物体定位 ,现在贡献出来给大家用。

给图层增加刻度尺

顾名思义就是加个刻度显示,效果如下:

image-20230424160401359

代码:
第一个参数时layer,第二个是精度,返回一个layer

   stage.add(HelperLayer(deviceUpLayer, 50));

全部代码如下:

export function HelperLayer(layer, precision = 50) {
    const height = layer.height();
    const width = layer.width();
    const offset = {x: layer.offsetX(), y: layer.offsetY()}
    const littleLine = precision / 10;

    //辅助网格层
    let helperLayer = new Konva.Layer();
    const x = new Konva.Arrow({
        points: [offset.x, offset.y, width - offset.y, offset.y],
        stroke: 'red',
        strokeWidth: 2,
        lineCap: 'round',
        lineJoin: 'round',
        fill: 'black',
    })
    for (let j = 1; j < 10; j++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x + j * littleLine, offset.y, offset.x + j * littleLine, j === 5 ? offset.x + 15 : offset.x + 10],
            stroke: '#48f3cc',
            strokeWidth: 1,
        }))
    }
    const xLabel = new Konva.Text({
        x: width - 20,
        y: 0,
        text: 'x',
        fontSize: 20,
        fill: 'yellow',
    })
    const y = new Konva.Arrow({
        points: [offset.x, offset.y, offset.x, height - offset.x],
        stroke: 'red',
        fill: 'black',
        strokeWidth: 2,
        lineCap: 'round',
        lineJoin: 'round',
    })
    for (let j = 1; j < 9; j++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x, offset.y + j * littleLine, j === 5 ? offset.y + 15 : offset.y + 10, offset.y + j * littleLine],
            stroke: '#48f3cc',
            strokeWidth: 1,
        }))
    }
    const yLabel = new Konva.Text({
        x: 10,
        y: height - 20,
        text: 'y',
        fontSize: 20,
        width: 100,
        fill: 'yellow',
    })
    helperLayer.add(x, y, xLabel, yLabel);

    helperLayer.add(new Konva.Text({
        x: offset.x,
        y: offset.y,
        text: "0,0",
        fontSize: 10,
        fill: 'yellow',
    }))

    // y轴刻度
    for (let i = 1; i < Math.floor(height / precision); i++) {
        helperLayer.add(new Konva.Line({
            points: [offset.x, i * precision + offset.y, 25, i * precision + offset.y],
            stroke: 'red',
            strokeWidth: 1,
        }))
        for (let j = 1; j < 9; j++) {
            helperLayer.add(new Konva.Line({
                points: [offset.x, i * precision + offset.y + j * littleLine, j === 5 ? offset.y + 15 : offset.y + 10, i * precision + offset.y + j * littleLine],
                stroke: '#48f3cc',
                strokeWidth: 1,
            }))
        }

        helperLayer.add(new Konva.Text({
            x: offset.x,
            y: i * precision + offset.y - 10,
            text: '' + i * precision,
            fontSize: 10,
            fill: 'yellow',
        }))
    }

    // x轴刻度
    for (let i = 1; i < Math.floor(width / precision); i++) {
        helperLayer.add(new Konva.Line({
            points: [i * precision + offset.x, offset.y, i * precision + offset.x, 25],
            stroke: 'red',
            strokeWidth: 1,
        }))
        for (let j = 1; j < 9; j++) {
            helperLayer.add(new Konva.Line({
                points: [offset.x + i * precision + j * littleLine, offset.y, offset.x + i * precision + j * littleLine, j === 5 ? offset.x + 15 : offset.x + 10],
                stroke: '#48f3cc',
                strokeWidth: 1,
            }))
        }
        helperLayer.add(new Konva.Text({
            x: i * precision + offset.x - 10,
            y: offset.y,
            text: '' + i * precision,
            fontSize: 10,
            fill: 'yellow',
        }))
    }

    return helperLayer;
}

拖动显示坐标

效果如下,当你做拖动元素的时候会自动显示该元素的坐标。

image-20230424160611856

实现如下:

第一个参数是需要拖动的元素,第二个参数是元素所在的图层,无返回值

function showCoord(node, layer) {
    node.on('dragmove', function (event) {
        if (event.target.coord) {
            event.target.coord.destroy()
        }
        event.target.coord = new Konva.Text({
            x: event.target.attrs.x,
            y: event.target.attrs.y + 10,
            text: `(${event.target.attrs.x},${event.target.attrs.y})`,
            fontSize: 20,
            fill: '#bcef63',
        });
        layer.add(event.target.coord);
    });
}

总结

代码很简单,其实也可以更近一步,但是我犯懒了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值