I can't edit text on layer after add new layer using kineticjs

135 views Asked by At

I have make this http://jsfiddle.net/62Wjc/5/, create and change text on layer but it's static. Now I make script for dynamic add fields + add layer + change layer.

On my script have three functions, which are :

  1. Function CreateTxt for create element and create layer with KineticJS.
  2. Function fRField for remove element
  3. Function ChangeTxt for change text on layer.

And this my script :

$(document).ready(function() {

    var nwValue = [];
    var nwLayer = {};
    var nwTxt = {};
    var tulisan = 'Your text here';
    var con = $('#idEditor');
    var cW = con.width();
    var cH = con.height();
    var stage = new Kinetic.Stage({
        container: 'idEditor',
        width: cW,
        height: cH
    });

    $('a[data-selector="get_new_txt"], a[data-selector="get_new_img"]').on('click', function(event){
        var time = new Date().getTime();
        var regexp = new RegExp($(this).data('id'), 'g');
        $('#nF').append($(this).data('fields').replace(regexp, time));
        CreateTxt(time);
        event.preventDefault();
    });

    $('#nF').on('click', 'a[data-selector="removeField"]', function(e) {
        var a = $(this);
        fRField(a);
        e.preventDefault();
    });

    $('#nF').on('keyup', 'input[data-selector="inputName"]', function() {
        var a = $(this);
        ChangeTxt(a);
    });

    function CreateTxt(idV) {
        var iclone = 0;
        nwValue.push(idV);
        $.each(nwValue, function( index, value ){
          nwLayer["layer"+ value] = new Kinetic.Layer();
        });

        stage.add(nwLayer["layer"+ idV]);
        $.each(nwValue, function( index, value ){
          nwTxt["text"+ value] = new Kinetic.Text({
                x: 100,
                y: 100,
                text: "Your text here",
                fontSize:18,
                fill: 'red',
                draggable: true,
                id: 'txt'+ value
            });
        });
        nwLayer["layer"+ idV].add(nwTxt["text"+ idV]);
        nwLayer["layer"+ idV].draw();      
    }

    function fRField(a){
      if(confirm('Are you sure to delete this field ?')) {
        a.prev("input[type=hidden]").value = "1";
        a.closest(".form-inline").remove();
      }
      return false;
    }

    function ChangeTxt(a){
        var dataid = a.attr('data-id');
        var newValue = a.val();
        nwTxt["text"+ dataid].setText(newValue);
        nwLayer["layer"+ dataid].draw();
    }

});

jsfiddle : http://jsfiddle.net/8oLsoo25/5/

After add a new text, I can't change text on layer first.

1

There are 1 answers

3
truefusion On BEST ANSWER

Your $.each call for nwValue is ruining things for you. If you place nwValue = []; above nwValue.push(idV); in CreateTxt, everything works as expected. In other words, you're replacing references to the original objects. Also, do you know that a new Layer in Kinetic means a new canvas element in the DOM?