modules/canvas.js

  1. /**
  2. * @license
  3. * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
  4. *
  5. * Licensed under the MIT License.
  6. * http://opensource.org/licenses/mit-license
  7. */
  8. import { jsPDF } from "../jspdf.js";
  9. /**
  10. * jsPDF Canvas PlugIn
  11. * This plugin mimics the HTML5 Canvas
  12. *
  13. * The goal is to provide a way for current canvas users to print directly to a PDF.
  14. * @name canvas
  15. * @module
  16. */
  17. (function(jsPDFAPI) {
  18. "use strict";
  19. /**
  20. * @class Canvas
  21. * @classdesc A Canvas Wrapper for jsPDF
  22. */
  23. var Canvas = function() {
  24. var jsPdfInstance = undefined;
  25. Object.defineProperty(this, "pdf", {
  26. get: function() {
  27. return jsPdfInstance;
  28. },
  29. set: function(value) {
  30. jsPdfInstance = value;
  31. }
  32. });
  33. var _width = 150;
  34. /**
  35. * The height property is a positive integer reflecting the height HTML attribute of the <canvas> element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of 150 is used.
  36. * This is one of the two properties, the other being width, that controls the size of the canvas.
  37. *
  38. * @name width
  39. */
  40. Object.defineProperty(this, "width", {
  41. get: function() {
  42. return _width;
  43. },
  44. set: function(value) {
  45. if (isNaN(value) || Number.isInteger(value) === false || value < 0) {
  46. _width = 150;
  47. } else {
  48. _width = value;
  49. }
  50. if (this.getContext("2d").pageWrapXEnabled) {
  51. this.getContext("2d").pageWrapX = _width + 1;
  52. }
  53. }
  54. });
  55. var _height = 300;
  56. /**
  57. * The width property is a positive integer reflecting the width HTML attribute of the <canvas> element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of 300 is used.
  58. * This is one of the two properties, the other being height, that controls the size of the canvas.
  59. *
  60. * @name height
  61. */
  62. Object.defineProperty(this, "height", {
  63. get: function() {
  64. return _height;
  65. },
  66. set: function(value) {
  67. if (isNaN(value) || Number.isInteger(value) === false || value < 0) {
  68. _height = 300;
  69. } else {
  70. _height = value;
  71. }
  72. if (this.getContext("2d").pageWrapYEnabled) {
  73. this.getContext("2d").pageWrapY = _height + 1;
  74. }
  75. }
  76. });
  77. var _childNodes = [];
  78. Object.defineProperty(this, "childNodes", {
  79. get: function() {
  80. return _childNodes;
  81. },
  82. set: function(value) {
  83. _childNodes = value;
  84. }
  85. });
  86. var _style = {};
  87. Object.defineProperty(this, "style", {
  88. get: function() {
  89. return _style;
  90. },
  91. set: function(value) {
  92. _style = value;
  93. }
  94. });
  95. Object.defineProperty(this, "parentNode", {});
  96. };
  97. /**
  98. * The getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported.
  99. *
  100. * @name getContext
  101. * @function
  102. * @param {string} contextType Is a String containing the context identifier defining the drawing context associated to the canvas. Possible value is "2d", leading to the creation of a Context2D object representing a two-dimensional rendering context.
  103. * @param {object} contextAttributes
  104. */
  105. Canvas.prototype.getContext = function(contextType, contextAttributes) {
  106. contextType = contextType || "2d";
  107. var key;
  108. if (contextType !== "2d") {
  109. return null;
  110. }
  111. for (key in contextAttributes) {
  112. if (this.pdf.context2d.hasOwnProperty(key)) {
  113. this.pdf.context2d[key] = contextAttributes[key];
  114. }
  115. }
  116. this.pdf.context2d._canvas = this;
  117. return this.pdf.context2d;
  118. };
  119. /**
  120. * The toDataURL() method is just a stub to throw an error if accidently called.
  121. *
  122. * @name toDataURL
  123. * @function
  124. */
  125. Canvas.prototype.toDataURL = function() {
  126. throw new Error("toDataURL is not implemented.");
  127. };
  128. jsPDFAPI.events.push([
  129. "initialized",
  130. function() {
  131. this.canvas = new Canvas();
  132. this.canvas.pdf = this;
  133. }
  134. ]);
  135. return this;
  136. })(jsPDF.API);