map.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var PDFDocument = require('pdfkit');
  2. var fs = require('fs');
  3. var factor = 300;
  4. var doc = new PDFDocument({layout: 'landscape', size: 'A0'});
  5. doc.pipe(fs.createWriteStream('output.pdf'));
  6. // background
  7. doc.polygon([0,0], [4000, 0], [4000,2500], [0, 2500]).fill("#000");
  8. // doc.polygon([100, 200], [600, 200], [600,1600], [100, 1600]).fill("#fff");
  9. for (var i = 0; i < 80; i++) {
  10. var width = doc._pageBuffer[0].width;
  11. var height = doc._pageBuffer[0].height;
  12. var x = Math.random()*width;
  13. var y = Math.random()*height;
  14. var figure_prob = Math.random();
  15. if (figure_prob < 1/3.0) {
  16. // rectangle
  17. var a = [x-factor*Math.random(), y-factor*Math.random()];
  18. var b = [x+factor*Math.random(), y-factor*Math.random()];
  19. var c = [x+factor*Math.random(), y+factor*Math.random()];
  20. var d = [x-factor*Math.random(), y+factor*Math.random()];
  21. doc.polygon(a, b, c, d).fill("#fff");
  22. console.log("REC", a, b, c, d);
  23. } else if (figure_prob < 2/3.0) {
  24. var radius = factor*Math.random()+factor/10;
  25. doc.circle(x, y, radius).fill("#fff");
  26. console.log("CIRCLE", x, y, radius);
  27. } else {
  28. // triangle
  29. var a = [x-factor*Math.random(), y-factor*Math.random()];
  30. var b = [x+factor*Math.random(), y-factor*Math.random()];
  31. var c = [x+factor*Math.random(), y+factor*Math.random()];
  32. doc.polygon(a, b, c).fill("#fff");
  33. console.log("TRIANGLE", a, b, c);
  34. }
  35. }
  36. doc.end();