Using variable from javascript in jQuery plugin

Hi I am using this jQuery plugin jCanvas for draving lines.

It work perfect

    $("canvas").drawLine({   
    strokeStyle: "#d96363",   
    strokeWidth: 5,  
    x1: 68, y1: 318,   
    x2: 176, y2: 158,   
    x3: 566, y3: 138,   
    x4: 559, y4: 199,   
    x5: 68, y5: 318 
});

But in my javascript source is generating string suradnice which looks (for example with same values) :

x1: 68, y1: 318, x2: 176, y2: 158, x3: 566, y3: 138, x4: 599, y4: 199, x5: 68, y5: 318

and I need to draw lines with values in the string suradnice

function draw_graph(suradnice){

  $("canvas").drawLine({
  strokeStyle: "#d96363",
  strokeWidth: 5,

  suradnice //Here 

}));
} 

How can I fix it?

Upvotes: 0

Views: 106

Answers (2)

Vlad
Vlad

Reputation: 3645

You can use eval, as variant:

obj = eval('({' + suradnice + '})');
obj.strokeStyle = '#d96363';
obj.strokeWidth = 5;
$("canvas").drawLine(obj);

More variants you can find in this question answers.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

What you're doing looks like you're trying to use a C-style macro to insert your values. Unfortunately there is no such feature in JavaScript.

However, if your data were formatted like this:

var suradnice = '{"x1":68,"y1":318.....}';

Then you would be able to do this:

var tmp = JSON.parse(suradnice);
tmp.strokeStye = "#d96363";
tmp.strokeWidth = 5;
$("canvas").drawLine(tmp);

Upvotes: 1

Related Questions