domtest.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <html>
  2. <!--
  3. Example of using the turtle module in skulpt.
  4. Author: Brad Miller
  5. Note: One important convention, since I plan to use
  6. multiple turtle canvases on a page I am passing the runit
  7. function a prefix to use in creating the id for the following:
  8. - textarea containing the code
  9. - pre tag for any printed output
  10. - canvas tag for the turtle
  11. I've enclosed the whole group of them in a div because I was thinking
  12. at one point about creating the pre tag and the canvas tag on the fly
  13. the more I think about it the more I wonder...
  14. -->
  15. <head>
  16. <title>Processing Integration with Skulpt</title>
  17. <link rel="stylesheet" type="text/css" media="all" href="main.css">
  18. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
  19. <script src="env/codemirror/js/codemirror.js" type="text/javascript"></script>
  20. <script src="env/editor.js" type="text/javascript"></script>
  21. <script src="skulpt.js" type="text/javascript"></script>
  22. <script src="builtin.js" type="text/javascript"></script>
  23. <script src="processing-1.4.1.min.js" type="text/javascript"></script>
  24. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript" >
  25. </script>
  26. </head>
  27. <div class="page">
  28. <div class="body">
  29. <div class="main">
  30. <body>
  31. <script type="text/javascript">
  32. function outf(text) {
  33. var mypre = document.getElementById(Sk.pre);
  34. mypre.innerHTML = mypre.innerHTML + text;
  35. }
  36. function builtinRead(x)
  37. {
  38. if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
  39. throw "File not found: '" + x + "'";
  40. return Sk.builtinFiles["files"][x];
  41. }
  42. function runit(myDiv) {
  43. var prog = document.getElementById(myDiv+"_code").value;
  44. var mypre = document.getElementById(myDiv+"_pre");
  45. mypre.innerHTML = '';
  46. Sk.divid = myDiv;
  47. Sk.canvas = myDiv+"_canvas";
  48. var can = document.getElementById(Sk.canvas);
  49. can.style.display = 'block';
  50. if (can) {
  51. can.width = can.width;
  52. if (Sk.tg) {
  53. Sk.tg.canvasInit = false;
  54. Sk.tg.turtleList = [];
  55. }
  56. }
  57. Sk.pre = myDiv+"_pre";
  58. Sk.configure({output:outf,
  59. read: builtinRead
  60. });
  61. try {
  62. Sk.importMainWithBody("<stdin>",false,prog);
  63. } catch (e) {
  64. alert(e);
  65. }
  66. }
  67. </script>
  68. <h1>Integrating Skulpt and the DOM</h1>
  69. <div id="example2">
  70. <textarea edit_id="eta_5a" id="example2_code" cols="60" rows="21">
  71. from document import *
  72. p = getElementById('myp')
  73. p.innerHTML = 'foo'
  74. div = getElementById('example2')
  75. h1 = createElement("h1")
  76. h1.innerHTML = 'HELLO'
  77. h1.setCSS('color','#f00')
  78. div.appendChild(h1)
  79. l = getElementsByTagName('h1')
  80. for e in l:
  81. print e.innerText
  82. ct = getElementsByName('loopcount')[0].value
  83. print ct
  84. for i in range(int(ct)):
  85. print i
  86. </textarea>
  87. How many times to loop <input type="text" name="loopcount" value="10" />
  88. <button onclick="runit('example2')" type="button">Run</button>
  89. <canvas id="example2_canvas"></canvas>
  90. <pre id="example2_pre"></pre>
  91. </div>
  92. <h3>Output should appear here</h3>
  93. <p id="myp"></p>
  94. </body>
  95. </div> </div></div>
  96. </html>