The folks at Khan Academy use a javascript package from the processing.js team at http://processingjs.org/ . It is open source and freely available to anyone. That means you can use the same package Khan Academy started with to build their Computer Science javascript section. All you need to get started is an HTML5 compatable browser and the freely available processing.js files.
OVERVIEW
STEP BY STEP
4) Open the sub folder and right click the files area to create a new text file. Name of that file: First Project.js. That will be your code file. After it is created, when you highlight and right click that file you should see options to Open or Edit it. When programming, you want to Edit the file.
var jsMain = function(processingInstance){ with (processingInstance){size(400, 400); // set size of canvas// add your code here ...
background(255, 0, 0);}};
<!DOCTYPE html><html><head><title>First Project</title></head>
<body><canvas id="pageCanvas"></canvas></body><script src="..\processing.min.js"></script><script src="First Project.js"></script><script type="application/javascript">var canvas = document.getElementById("pageCanvas");var processingInstance = new Processing(canvas, jsMain);</script></html>
If it runs right off, Congratulations you have sucessfully followed all the instructions!!!
If it does not run, be sure two things match what you have on your system.
From those 10 steps you will be able to make as many different programs as you want.
BAD NEWS GOOD NEWS
The downside to offline development is that you need to do steps 3, 6, and 9 for every new program, adjusting the second script src to match your new project name. But, if you are running Windows, there is some good news! The HTML file at the end of this message will do the three steps for you. All you need do is launch the HTML file and enter your new project name!
TIPS FOR EDITING
There are a few differences between running offline and running the same program at Khan Academy. Offline is a little more tolerant about forgetting trailing semi-colons. And offline uses radian triginometry where KA uses degrees. If you have troubles during the conversion, search this newsgroup for an answer, or post a question if you don't see your issued covered already.
One useful tip is to work in milestones. When you get a major portion of your program done and working, stop and make a copy of your program file. That way if you code yourself into a corner (and need to go at it a different way) you have saved files to backtrack as far as you need to bring in the new methods.
To test your code, launch the HTML file in your browser. While editing and updating, use CTRL+S to save the file in Notepad and hit the Refresh button in the browser to pick up the new changes. You can do that combination reasonably rapidly, if you want to make incremental changes to correctly align images and shapes.
And now for the piece de resistance (the reason for this post). The HTML code below will make building new projects a breeze. To see it in action follow these steps:
The JS Project Starter will make a new sub folder (step 3) and create 2 new files in the new folder (steps 6 and 9). You need only to navigate into the new folder and right click the new code file to open it for editing, and away you go!
Happy coding!
<JS Project Starter code follows>
<html><head><title>P5-JS Project Starter App</title>
<HTA:APPLICATION
ID="JSProject"APPLICATIONNAME="JSProjectStarter"SCROLL="no"MAXIMIZEBUTTON="no"SINGLEINSTANCE="yes"WINDOWSTATE="normal"></head><style>
BODY{background-color: buttonface;font-family: Helvetica;font-size: 10pt;margin-top: 10px;margin-left: 20px;margin-right: 20px;margin-bottom: 10px;}.button
{font-family: Helvetica;font-size: 8pt;width: 92px;}
textarea
{font-family: arial;font-size: 8pt;}</style>
<script>
var ps_file = "";var ps_name = "";var fso = new ActiveXObject("Scripting.FileSystemObject");function position(){window.resizeTo(400, 355);
window.moveTo((screen.width - 400) * 0.5, (screen.height - 440) * 0.4);}function titleCase(text){
var spl=text.split(" ");var ups="";for(var i=0;i<spl.length;i++){ups = "" + spl[i].charAt(0).toUpperCase();ups += "" + spl[i].substring(1);spl[i] = ups}return spl.join(" ");}function runBuild(){ps_name = document.getElementById("project").value;
if (ps_name.length == 0){alert("Project name error. Operation aborted.");return;}ps_file = fso.GetFileName(findPS());
if (ps_file.length == 0){alert("File access errors. Operation aborted.");return;}fso.CreateFolder(".\\" + titleCase(ps_name));var txt = fso.CreateTextFile(".\\" + ps_name + "\\" + ps_name + ".js", true);txt.WriteLine("var jsMain = function(processingInstance){ with (processingInstance){");txt.WriteLine("size(400, 400); // set size of canvas");txt.WriteLine("");txt.WriteLine("// add your code here ...");txt.WriteLine("");txt.WriteLine("");txt.WriteLine("}};");txt.close()txt = fso.CreateTextFile(".\\" + ps_name + "\\" + ps_name + ".htm", true);
txt.Write('<!DOCTYPE html><html><head><title>' );txt.WriteLine(titleCase(ps_name) + '</title></head>');txt.WriteLine('<body><canvas id="pageCanvas"></canvas></body>');txt.WriteLine('<script src="..\\' + ps_file + '"><\/script>');txt.WriteLine('<script src="' + ps_name + '.js"><\/script>');txt.WriteLine('<script type="application/javascript">');txt.WriteLine(' var canvas = document.getElementById("pageCanvas");');txt.WriteLine(' var processingInstance = new Processing(canvas, jsMain);');txt.WriteLine('</script\>');txt.WriteLine('</html>');txt.close();txt = document.getElementById("project");
txt.value = "";}function findPS(){var fld = fso.GetFolder(".");var fls = new Enumerator(fld.Files);var nam = "";var lst = "1";for(fls.moveFirst(); !fls.atEnd(); fls.moveNext() ) {
nam = "" + fls.item();if (nam.indexOf(".min.js") > 0) {if (nam > lst){ lst = nam;}}}fls = null;fld = null;return lst;}</script><body onLoad="position()">
 Enter the name of your project:<br><input type="text" name="project" size="50"><br><br><input type="button" value="Build" onClick="runBuild()"><br> <br><p><h3>INSTRUCTIONS:</h3><p>Enter the name of your new project. (above)</p><p>Press the Build button to create the folder and files for your new project.</p><p>Enter your program into the newly created <b>*.js</b> file and <i><b>save your work</b></i>, then double clickthe <b>*.htm</b> file to view the results.</p>
</body></html>
<end of message>
Thanks for the work you've put into this. I followed the steps and was able to launch the red canvas, but when I followed the same steps to launch my KA program (star(t)), it comes up with a gray canvas.
I tried moving my code to Fiddle, and it displays using Bob's KA template, so I think I must be missing something. Is there anything else I need to add to my KA code to get it to display in a browser?
Wow, thank you again! If it took you an hour to find a solution, I can only imagine how long it would have taken me :).
I implemented that, and I have a black canvas now! Progress :).
I opened developer tools, and see these errors:
processing.min.js:164 Failed to decode downloaded font: data:application/x-font-ttf;base64,(followed by many letters, which I've removed for sake of space)
getElementWidth @ processing.min.js:164
pending @ processing.min.js:165
Ir @ processing.min.js:1415
E.Processing @ processing.min.js:1418
(anonymous) @ star(t).html:8
processing.min.js:164 OTS parsing error: post: Failed to parse table
getElementWidth @ processing.min.js:164
pending @ processing.min.js:165
Ir @ processing.min.js:1415
E.Processing @ processing.min.js:1418
(anonymous) @ star(t).html:8
I have the processing.min files in my main folder, so I'm wondering if perhaps I need to download something more. I've just been coding on KA (and nowhere else) since October, so this is new to me. I realize the first error something to do with the font, but I'm not entirely sure how to read this, or where to start for a solution. Any help would be appreciated.
Thanks so much!
I'm still trying to figure out this font decoding issue. I've searched through StackOverflow, and have only found reference to people using @fontface and custom fonts. I haven't specified any fonts in my html. In my JS file, I had set verdana as my font, but just commented that out to test it. I'm still getting the same error - failed to decode font. I'm not really sure what else to do.
I also tried adding the verdana .ttf file and putting @fontface for verdana in my html, but that also didn't work.
Do you happen to have any ideas? If you were able to load my program, I really am not sure what could be the problem.
As I said, it works fine on JSFiddle and Khan Academy. I'm able to load a black canvas, and the music I've since added plays as well.
I had made my way to github, and downloaded an old version. I didn't realize that I just needed to copy and paste the script on the processing JS download page, and save it as a processing.min.js file. I thought that maybe something was wrong with the download, so I went to Github and downloaded an older version.
Now that I "downloaded" the newest version it's working!! Hooray!
Thanks again for all your help, Larry!