I’m writing a webpage, and because my PC is not working right now, I need to do it using my MacBook.
And when I use a laptop computer, I don’t use a mouse, so it is very hard to draw graphics in MacBook. Also, GIMP doesn’t work very well with Mac OS X.
So I come up with something. Why don’t I draw images using only code?
I came up with this. It’s called Canvas Renderer, a small python script that takes a JavaScript file, and generate images. It uses WebKit and the Canvas element. On Mac OS X it uses PyObjC and WebKit framework. On Linux it uses PyWebKitGtk.
You can get the code here.
On a Mac, you need to sudo easy_install pyobjc-framework-WebKit first. On Linux, just install PyWebKitGtk. As far as I know it comes with Ubuntu Karmic. To run the code,
python canvasrenderer.py
The above command takes the JavaScript code from the standard output.
python canvasrenderer.py file.js
The above command takes the JavaScript code from a the file specified in the arguments. Try running:
python canvasrenderer.py test.js
and you should get something like this:
The API
Functions
- newFile(width, height) – creates a new blank canvas.
- saveFile(filename) – saves the image.
Variables (these variables are reset after calling newFile)
- canvas – the canvas element
- ctx – the canvas context
- w – width of the canvas
- h – height of the canvas
Right now there are no ways to import image and use it.
Oh. Another thing: you can use bash to stick files together and then run it.
cat libs.js background.js | python canvasrenderer.py