Search code examples
javascriptnode.jsgraphicsmagick

What is a method to draw many graphic primitives using node.js and gm?


I'm just getting started with Node and have some simple results from the gm package. What I want to do now is draw many (thousands or more) shapes on an image.

I can't figure out how to create multiple shapes other than

gm(source)
.draw('rectangle', 0, 0, 100, 100, 'rectangle', 100, 100, 200, 200)
.write(dest, writeCallback);

or

gm(source)
.drawRectangle(0, 0, 100, 100)
.drawRectangle(100, 100, 200, 200)
.write(dest, writeCallback);

Is there a way to use an iterator to achieve this? Building a string doesn't work due to the way that 'draw()' accepts arguments.

I appreciate any assistance or direction!


Solution

  • Without reading the documentation, I'd venture that you can simply store the picture in a variable, and apply any drawing primitives as you want:

    var pic = gm(source);
    pic.drawRectangle(whatever);
    pic.drawRectangle(whatever else);
    pic.write(dest,writeCallback);
    

    so you can use any loops and logic you want. The examples you gave are just shortcuts for simple drawings, where each call to a drawing primitive returns the picture, exactly like jQuery for instance.