Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
  3. * http://paperjs.org/
  4. *
  5. * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
  6. * http://juerglehni.com/ & https://puckey.studio/
  7. *
  8. * Distributed under the MIT license. See LICENSE file for details.
  9. *
  10. * All rights reserved.
  11. */
  12. var fs = require('fs'),
  13. path = require('path');
  14. module.exports = function(paper) {
  15. if (paper.PaperScript) {
  16. var sourceMapSupport = 'require("source-map-support").install(paper.PaperScript.sourceMapSupport);\n',
  17. sourceMaps = {};
  18. paper.PaperScript.sourceMapSupport = {
  19. retrieveSourceMap: function(source) {
  20. var map = sourceMaps[source];
  21. return map ? { url: source, map: map } : null;
  22. }
  23. };
  24. // Register the .pjs extension for automatic compilation as PaperScript
  25. require.extensions['.pjs'] = function(module, filename) {
  26. // Requiring a PaperScript on Node.js returns an initialize method which
  27. // needs to receive a Canvas object when called and returns the
  28. // PaperScope.
  29. module.exports = function(canvas) {
  30. var source = fs.readFileSync(filename, 'utf8'),
  31. code = sourceMapSupport + source,
  32. compiled = paper.PaperScript.compile(code, {
  33. url: filename,
  34. source: source,
  35. sourceMaps: true,
  36. offset: -1 // remove sourceMapSupport...
  37. }),
  38. scope = new paper.PaperScope();
  39. // Keep track of sourceMaps so retrieveSourceMap() can link them up
  40. scope.setup(canvas);
  41. scope.__filename = filename;
  42. scope.__dirname = path.dirname(filename);
  43. // Expose core methods and values
  44. scope.require = require;
  45. scope.console = console;
  46. sourceMaps[filename] = compiled.map;
  47. paper.PaperScript.execute(compiled, scope);
  48. return scope;
  49. };
  50. };
  51. }
  52. paper.PaperScope.inject({
  53. createCanvas: function(width, height, type) {
  54. // Do not use CanvasProvider.getCanvas(), since we may be changing
  55. // the underlying node-canvas when requesting PDF support, and don't
  56. // want to release it after back into the pool.
  57. var canvas = paper.document.createElement('canvas');
  58. canvas.width = width;
  59. canvas.height = height;
  60. canvas.type = type;
  61. return canvas;
  62. },
  63. /**
  64. * @deprecated, use use {@link #createCanvas(width, height)} instead.
  65. */
  66. Canvas: '#createCanvas'
  67. });
  68. // Override requestAnimationFrame() to avoid setInterval() timers.
  69. // NOTE: In Node.js, we only support manual updating for now, but
  70. // View#exportFrames() below offers a way to emulate animations by exporting
  71. // them frame by frame at the given frame-rate.
  72. paper.DomEvent.requestAnimationFrame = function(callback) {
  73. };
  74. // Node.js based image exporting code.
  75. paper.CanvasView.inject({
  76. // DOCS: CanvasView#exportFrames(options);
  77. exportFrames: function(options) {
  78. options = paper.Base.set({
  79. fps: 30,
  80. prefix: 'frame-',
  81. amount: 1,
  82. format: 'png' // Supported: 'png' or 'jpeg'
  83. }, options);
  84. if (!options.directory)
  85. throw new Error('Missing options.directory');
  86. if (options.format && !/^(jpeg|png)$/.test(options.format))
  87. throw new Error('Unsupported format. Use "png" or "jpeg"');
  88. var view = this,
  89. count = 0,
  90. frameDuration = 1 / options.fps,
  91. startTime = Date.now(),
  92. lastTime = startTime,
  93. padding = options.padding || ((options.amount - 1) + '').length,
  94. paddedStr = Array(padding + 1).join('0');
  95. // Start exporting frames by exporting the first frame:
  96. exportFrame(options);
  97. function exportFrame() {
  98. // Convert to a Base object, for #toString()
  99. view.emit('frame', new paper.Base({
  100. delta: frameDuration,
  101. time: frameDuration * count,
  102. count: count
  103. }));
  104. var file = path.join(options.directory,
  105. options.prefix + (paddedStr + count).slice(-padding)
  106. + '.' + options.format);
  107. var out = view.exportImage(file, function() {
  108. // Once the file has been closed, export the next fame:
  109. var then = Date.now();
  110. if (options.onProgress) {
  111. options.onProgress({
  112. count: count,
  113. amount: options.amount,
  114. percentage: Math.round((count + 1) / options.amount
  115. * 10000) / 100,
  116. time: then - startTime,
  117. delta: then - lastTime
  118. });
  119. }
  120. lastTime = then;
  121. if (++count < options.amount) {
  122. exportFrame();
  123. } else {
  124. // Call onComplete handler when finished:
  125. if (options.onComplete) {
  126. options.onComplete();
  127. }
  128. }
  129. });
  130. }
  131. },
  132. // DOCS: CanvasView#exportImage(path, callback);
  133. exportImage: function(path, callback) {
  134. this.update();
  135. var out = fs.createWriteStream(path),
  136. format = /\.jp(e?)g$/.test(path) ? 'jpeg' : 'png',
  137. stream = this._element[format + 'Stream']();
  138. stream.pipe(out);
  139. if (callback) {
  140. out.on('close', callback);
  141. }
  142. return out;
  143. }
  144. });
  145. };