index.js 699 B

1234567891011121314151617181920212223242526272829
  1. var qr = require('qr.js');
  2. var qrcode = qr('foo');
  3. var width = 200;
  4. var height = 200;
  5. var canvas = document.createElement('canvas');
  6. canvas.width = width;
  7. canvas.height = height;
  8. var ctx = canvas.getContext('2d');
  9. var cells = qrcode.modules;
  10. var tileW = width / cells.length;
  11. var tileH = height / cells.length;
  12. for (var r = 0; r < cells.length ; ++r) {
  13. var row = cells[r];
  14. for (var c = 0; c < row.length ; ++c) {
  15. ctx.fillStyle = row[c] ? '#000' : '#fff';
  16. var w = (Math.ceil((c+1)*tileW) - Math.floor(c*tileW));
  17. var h = (Math.ceil((r+1)*tileH) - Math.floor(r*tileH));
  18. ctx.fillRect(Math.round(c*tileW), Math.round(r*tileH), w, h);
  19. }
  20. }
  21. canvas // =>