0

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!

Scott Ord
  • 128
  • 3

1 Answers1

2

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.

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Aha! I hadn't thought to assign the result of the gm(source) function - it works exactly as you stated! Many thanks. – Scott Ord Aug 07 '14 at 21:34