KonvaJS - 如何使用按钮触发器添加形状?

KonvaJS - How to add shape with button trigger?

抱歉语法错误-

我对如何使用按钮触发器添加形状有疑问。 但不工作

这是我的代码: HTML

<button id="btnCreateRectangle" class="btn-primary-blue">Button Text</button>

这是我的 js :


        function addRectangle(layer) {
            var scale = 1;

            var rectangle = new Konva.Rect({
                x: 12,
                y: 12,
                numPoints: 5,
                innerRadius: 30,
                outerRadius: 50,
                fill: "#89b717",
                opacity: 0.8,
                draggable: true,
                name: 'rect',
                width: 128,
                height: 50,
                scale: {
                    x: scale,
                    y: scale
                },
                shadowColor: "black",
                shadowBlur: 4,
                shadowOffset: {
                    x: 5,
                    y: 5
                },
                shadowOpacity: 0.6,
                // custom attribute
                startScale: scale
            });

            layer.add(rectangle);
        }
document
            .getElementById('btnCreateRectangle')
            .addEventListener('click', function () {
                addRectangle(layer)
            });

我是 javasrcipt 语言的新手, 任何建议或回答将不胜感激

谢谢

根据 KonvaJS 的文档,将矩形添加到层后,您应该将该层添加到 stage https://konvajs.org/docs/overview.html

stage.add(layer);