Search code examples
javascriptobjectlayerjcanvas

"jcanvas" I can't get "data property" within the layer itself


// first case

$('canvas').drawRect({

  layer: true,

  data: { w: 300 },

  fillStyle: '#585',

  x: 100, y: 100,

  width: 100, height: 50

});

alert($('canvas').getLayer(0).data.w);

I can get the data out of the layer.


// second case

$('canvas').drawRect({

  layer: true,

  data: { w: 300 },

  fillStyle: '#585',

  x: 100, y: 100,

  width: $('canvas').getLayer(0).data.w, height: 50

});

I can't get the data within the layer itself.


Solution

  • An easy and practical solution would be to specify your data object beforehand so that it is accessible within the scope of your drawRect() call:

    var rectData = { w: 300 };
    
    $('canvas').drawRect({
    
      layer: true,
    
      data: rectData,
    
      fillStyle: '#585',
    
      x: 100, y: 100,
    
      width: rectData.w, height: 50
    
    });