draw-device.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Use device interface to draw some graphics and save as a PNG.
  2. var font = new Font("Times-Roman");
  3. var image = new Image("huntingofthesnark.png");
  4. var path, text;
  5. var pixmap = new Pixmap(ColorSpace.DeviceRGB, [0,0,500,600], false);
  6. pixmap.clear(255);
  7. var device = new DrawDevice(Matrix.identity, pixmap);
  8. var transform = [2,0,0,2,0,0]
  9. {
  10. text = new Text();
  11. {
  12. text.showString(font, [16,0,0,-16,100,30], "Hello, world!");
  13. text.showString(font, [0,16,16,0,15,100], "Hello, world!");
  14. }
  15. device.fillText(text, transform, ColorSpace.DeviceGray, [0], 1);
  16. path = new Path();
  17. {
  18. path.moveTo(10, 10);
  19. path.lineTo(90, 10);
  20. path.lineTo(90, 90);
  21. path.lineTo(10, 90);
  22. path.closePath();
  23. }
  24. device.fillPath(path, false, transform, ColorSpace.DeviceRGB, [1,0,0], 1);
  25. device.strokePath(path, {dashes:[5,10], lineWidth:3, lineCap:'Round'}, transform, ColorSpace.DeviceRGB, [0,0,0], 1);
  26. path = new Path();
  27. {
  28. path.moveTo(100,100);
  29. path.curveTo(150,100, 200,150, 200,200);
  30. path.curveTo(200,300, 0,300, 100,100);
  31. path.closePath();
  32. }
  33. device.clipPath(path, true, transform);
  34. {
  35. device.fillImage(image, Matrix.concat(transform, [300,0,0,300,0,0]), 1);
  36. }
  37. device.popClip();
  38. }
  39. device.close();
  40. pixmap.saveAsPNG("out.png");