From: Bruce S. <Bru...@nc...> - 2011-06-28 02:37:11
|
Occasionally users of VPython have expressed interest in displaying VPython animations in a browser. Unfortunately, Python doesn't run in a browser, but something related is in the works. David Scherer, the creator of VPython, and I are working on making it possible for VPython-ish programs to run in a browser window. Recently many browsers (all the major ones other than Internet Explorer) include WebGL, a graphics library similar to the OpenGL library used by VPython. JavaScript programs can drive WebGL, in a browser. Here is an example of a JavaScript+WebGL version of the VPython demo program doublependulum: http://tinyurl.com/684xldd This JavaScript program looks very similar to the original program and would look even more similar were it not for the fact that among the features not yet implemented is the frame object. If the frame object were implemented, this JavaScript program could have been created almost entirely by using an automatic converter program I've just written (in Python). In the absence of this project, the doublependulum program would include a ton of extremely arcane JavaScript code, just as VPython is supported by a ton of arcane C++ code. What's the point of this? Well, for example, you may know there's an ebook version of the intro physics textbook "Matter & Interactions" written by Ruth Chabay and me. Both Wiley and we want eventually to add interactive features such as animations, and it seems likely that would/should take place in a browser, in particular because (as you'll see in the demo mentioned above) one of the strengths of this approach is that there can be text and other standard browser elements on the web page in which the animation is embedded. Ruth and I would like to be able to use our suite of demo programs, and write additional ones, without killing ourselves in minutiae. We do NOT think that this is a replacement for VPython, and for student use in intro physics. Python is a much more mature and cleaner language than JavaScript, and Python has the blessing of computer scientists, so for both technical and political reasons Python is a far better choice for introducing computational physics than is JavaScript. Scherer solved two unsolvable problems to get to the point we've reached after a few weeks of work. Unlike Python, JavaScript does not support "operator overloading", in which you can for example change the meaning of the operator "+" so that 2+3 is 5 but vectors v1+v2 have their individual components added. Nevertheless, by a clever approach Scherer has made it possible to add and subtract vectors, and multiply by scalars: v+v, v-v, s*v, v*s. The second unsolvable problem is that a JavaScript+WebGL program has to be "event-driven", with the program divided into pieces to be called by the web page manager. This requirement means that a straight-forward orbit program will run through an entire loop before the web page ever gets updated, so you only see the final state, not the animation, and if the loop is an infinite loop you never will see anything. Scherer found a tool named "streamline" that will take a non-event-driven JavaScript program and automatically convert it to an event-driven JavaScript program. The only burden on the programmer is that instead of including, say, rate(100) in a loop (to iterate only 100 times per second), you have to include rate(100,_), where the "_" is a signal to the streamline engine where it can break the loop contents into a separate routine that is called 100 times per second. (We intend to change the signal "_" to something meaningful, like "wait"). You can also in this environment write true event-driven programs, but we feel strongly that this style of programming is not where the novice should start. Stay tuned! Bruce P.S. At the moment, the tinyurl above will work on Chrome on Windows and Mac and Linux, and on the WebKit version of Safari on the Mac (a free download that enhances Safari). It should also work on Firefox but doesn't at the moment, though it has in various past versions of the project so I expect it will eventually work again. P.P.S. The attentive reader will have noticed that I didn't reveal the name of this project. That's because we haven't yet thought up one that David, Ruth, and I are happy with. Suggestions welcome! |