3

I have the folowing string variable returned from a textarea : jtext = $('#putJS').val();

The result of this string is the following:

ctx.beginPath();
    ctx.moveTo(172 + xoff, 29 + yoff);
    ctx.bezierCurveTo(183 + xoff, 33 + yoff, 199 + xoff, 36 + yoff, 201 + xoff, 74 + yoff);
    ctx.bezierCurveTo(191 + xoff, 91 + yoff, 197 + xoff, 130 + yoff, 196 + xoff, 110 + yoff);
    ctx.bezierCurveTo(195 + xoff, 95 + yoff, 200 + xoff, 230 + yoff, 200 + xoff, 248 + yoff);
    ctx.bezierCurveTo(200 + xoff, 256 + yoff, 85 + xoff, 251 + yoff, 70 + xoff, 251 + yoff);
    ctx.bezierCurveTo(65 + xoff, 251 + yoff, 74 + xoff, 122 + yoff, 71 + xoff, 106 + yoff);
    ctx.bezierCurveTo(68 + xoff, 88 + yoff, 65 + xoff, 87 + yoff, 65 + xoff, 73 + yoff);
    ctx.bezierCurveTo(65 + xoff, 52 + yoff, 70 + xoff, 35 + yoff, 95 + xoff, 28 + yoff);
    ctx.bezierCurveTo(99 + xoff, 37 + yoff, 130 + xoff, 72 + yoff, 171 + xoff, 29 + yoff);
    ctx.stroke(); 

The question is, how do I put this string as javascript code to design the canvas in my code javascript file?

3
  • Have you heard about eval? Commented May 21, 2013 at 11:48
  • Just put in a <script> tag and insert that into the document? But be careful about what code you execute there! Commented May 21, 2013 at 11:49
  • We have to assume here that the object referenced by ctx exists already within the page. Commented May 21, 2013 at 11:59

1 Answer 1

1

There is an eval function in with you can pass any string. SImple example you can find here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval

But using eval function could be dangerous, so be careful with this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.