动态js中的可编辑文本选项

时间:2012-11-07 14:09:11

标签: javascript text kineticjs

我想添加Textbox或可编辑元素,以便为用户提供编辑文字的选项。

这是我目前的代码:

var text = new Kinetic.Text({
        text: "Sample Text", ---> i want to edit this text 
        x: 50,
        y: 10,
        fill: "transparent",
        fontSize: 10,
        fontFamily: "Helvetica Neue",
        textFill: "#000",
        align: "center",
        verticalAlign: "middle",
        name:'TEXT'
    });

3 个答案:

答案 0 :(得分:11)

目前似乎没有任何方法可以使用Kinetic JS创建可编辑的文本(在stackoverflow上查看有关此内容的几个主题),有些人建议使用画布旁边的输入字段来编辑文本,但我的解决方案将是以下内容:

  • 使用您的代码创建文本
  • 在文本上单击[text.on(“click”,function ...],在鼠标光标处创建一个输入字段

嗯,这是计划。也许在输入字段中使用“保存”按钮文本会更容易,因此您可以确切地知道何时关闭它以及何时将输入字段数据存储到动态文本中。如果你不想编辑它,你还需要一个“关闭”功能。

一个非常简单的解决方案也是一个简单的JavaScript提示符:

var xy = prompt("gimme your text");

所以,像这样的东西将是最好的解决方案imho:

myText.on('click', function(evt) {
    this.setText(prompt('New Text:'));
    layer.draw(); //redraw the layer containing the textfield
});

答案 1 :(得分:2)

我尝试了一个具有可编辑文本功能的实际KinetiJS插件。

我知道它正在重新发明轮子,当你可以只悬停一个textarea时,但为什么不在帆布上也只有它。

检查插件:https://github.com/nktsitas/kineticjs-editable-text

答案 2 :(得分:1)

我在我的项目上做了几天。野兔是代码片段。基本上我们首先绘制矩形,然后在其中放入一个文本区域,最后将其转换为kinetic.text节点。

             drawText: function ( color )
            {
                var layer1=this.model.stage.getLayers()[0];
                var $this=this;
                console.log('drawText: color: ' + color);

                if($this.rectangleDrawn==true)
                {

                    var down = false, oPoint;
                    layer1.off("mousedown touchstart mousemove touchmove mouseup touchend");
                    if(group!=undefined && group!='')
                    {
                        $this.hideAnchors(group);
                    }
                    console.log("textX: "+textX);
                    //after rectangle is drawn we now have to add the editablesection
                    $('<textarea id="text" type="text"  width='+textWidth +'px height='+textHeight+'px style="font-size: 30px;font-family:Calibri;height:'+textHeight+'px;width:'+textWidth+'px;position:absolute'+';left:'+textX+'px'+';top:'+textY+'px'+';z-index:5'+';background-color:#ffcc00;"></textarea>')

                    .insertBefore('.kineticjs-content');
                    $('#text').on('blur',function()
                    {
                        console.log("textchange");
                        text = new Kinetic.Text( {
                            x: textX,
                            y: textY,
                            stroke: color,
                            strokeWidth: 1,
                            fontSize: 30,
                            fontFamily: 'Calibri',
                            clearBeforeDraw: false,
                            name: 'image'+layer1.getName(),
                            draggable: true,
                            height: textHeight,
                            width: textWidth,
                            text: $('#text').val()
                        } );
                        text.on( 'mouseleave dbltap', function ()
                        {
                            text=this;
                        } );
                        $('#text').remove();

                        layer1.add( text );
                        layer1.draw(); 
                    })
                    },drawRectangle: function ( opacity, colorFill,stroke,textColor ){rect = new Kinetic.Rect({
                    x: mousePos.x,
                    y: mousePos.y,
                    width: 0,
                    height: 0,
                    stroke: stroke,
                    strokeWidth: 4,
                    opacity: opacity,
                    clearBeforeDraw: false,
                    name: 'image'+layer1.getName()
                });
                layer1.on( "mouseup touchend", function ( e )
                {
                console.log("rectangle: mouseup");
                console.log("width: "+rect.getWidth(  ));
                rect.setOpacity(opacity);
                rect.setFill(colorFill);
                layer1.draw();
                    down = false;
                    console.log("textColor: "+textColor)
                    if(textColor!=undefined)
                    {
                        textWidth=rect.getWidth(  );
                        textHeight=rect.getHeight(  );
                        textX = rect.getAbsolutePosition().x;
                        textY=rect.getAbsolutePosition().y;
                        $this.rectangleDrawn=true;
                        $this.drawText(textColor);
                        console.log("textdrawn ");
                        $this.group.remove();
                    }

        },