Web UI for ARGUS server
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.

66 lines
2.4 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. // Add some useful extensions to HTMLCanvasElement:
  13. // - HTMLCanvasElement#type, so we can switch to a PDF canvas
  14. // - Various Node-Canvas methods, routed through from HTMLCanvasElement:
  15. // toBuffer, pngStream, createPNGStream, jpegStream, createJPEGStream
  16. module.exports = function(self, requireName) {
  17. var Canvas;
  18. try {
  19. Canvas = require('canvas').Canvas;
  20. } catch(error) {
  21. // Remove `self.window`, so we still have the global `self` reference,
  22. // but no `window` object:
  23. // - On the browser, this corresponds to a worker context.
  24. // - On Node.js, it basically means the canvas is missing or not working
  25. // which can be treated the same way.
  26. delete self.window;
  27. // Check the required module's name to see if it contains canvas, and
  28. // only complain about its lack if the module requires it.
  29. if (/\bcanvas\b/.test(requireName)) {
  30. throw new Error('Unable to load canvas module.');
  31. }
  32. return;
  33. }
  34. var HTMLCanvasElement = self.HTMLCanvasElement,
  35. idlUtils = require('jsdom/lib/jsdom/living/generated/utils');
  36. // Add fake HTMLCanvasElement#type property:
  37. Object.defineProperty(HTMLCanvasElement.prototype, 'type', {
  38. get: function() {
  39. var canvas = idlUtils.implForWrapper(this)._canvas;
  40. return canvas && canvas.type || 'image';
  41. },
  42. set: function(type) {
  43. // Allow replacement of internal node-canvas, so we can switch to a
  44. // PDF canvas.
  45. var impl = idlUtils.implForWrapper(this),
  46. size = impl._canvas || impl;
  47. impl._canvas = new Canvas(size.width, size.height, type);
  48. impl._context = null;
  49. }
  50. });
  51. // Extend HTMLCanvasElement with useful methods from the underlying Canvas:
  52. var methods = ['toBuffer', 'pngStream', 'createPNGStream', 'jpegStream',
  53. 'createJPEGStream'];
  54. methods.forEach(function(key) {
  55. HTMLCanvasElement.prototype[key] = function() {
  56. var canvas = idlUtils.implForWrapper(this)._canvas;
  57. return canvas[key].apply(canvas, arguments);
  58. };
  59. });
  60. };