foreignobject-renderer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  18. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  19. return new (P || (P = Promise))(function (resolve, reject) {
  20. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  21. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  22. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  23. step((generator = generator.apply(thisArg, _arguments || [])).next());
  24. });
  25. };
  26. var __generator = (this && this.__generator) || function (thisArg, body) {
  27. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  28. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  29. function verb(n) { return function (v) { return step([n, v]); }; }
  30. function step(op) {
  31. if (f) throw new TypeError("Generator is already executing.");
  32. while (_) try {
  33. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  34. if (y = 0, t) op = [op[0] & 2, t.value];
  35. switch (op[0]) {
  36. case 0: case 1: t = op; break;
  37. case 4: _.label++; return { value: op[1], done: false };
  38. case 5: _.label++; y = op[1]; op = [0]; continue;
  39. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  40. default:
  41. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  42. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  43. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  44. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  45. if (t[2]) _.ops.pop();
  46. _.trys.pop(); continue;
  47. }
  48. op = body.call(thisArg, _);
  49. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  50. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  51. }
  52. };
  53. Object.defineProperty(exports, "__esModule", { value: true });
  54. exports.loadSerializedSVG = exports.ForeignObjectRenderer = void 0;
  55. var features_1 = require("../../core/features");
  56. var color_1 = require("../../css/types/color");
  57. var renderer_1 = require("../renderer");
  58. var ForeignObjectRenderer = /** @class */ (function (_super) {
  59. __extends(ForeignObjectRenderer, _super);
  60. function ForeignObjectRenderer(context, options) {
  61. var _this = _super.call(this, context, options) || this;
  62. _this.canvas = options.canvas ? options.canvas : document.createElement('canvas');
  63. _this.ctx = _this.canvas.getContext('2d');
  64. _this.options = options;
  65. _this.canvas.width = Math.floor(options.width * options.scale);
  66. _this.canvas.height = Math.floor(options.height * options.scale);
  67. _this.canvas.style.width = options.width + "px";
  68. _this.canvas.style.height = options.height + "px";
  69. _this.ctx.scale(_this.options.scale, _this.options.scale);
  70. _this.ctx.translate(-options.x, -options.y);
  71. _this.context.logger.debug("EXPERIMENTAL ForeignObject renderer initialized (" + options.width + "x" + options.height + " at " + options.x + "," + options.y + ") with scale " + options.scale);
  72. return _this;
  73. }
  74. ForeignObjectRenderer.prototype.render = function (element) {
  75. return __awaiter(this, void 0, void 0, function () {
  76. var svg, img;
  77. return __generator(this, function (_a) {
  78. switch (_a.label) {
  79. case 0:
  80. svg = features_1.createForeignObjectSVG(this.options.width * this.options.scale, this.options.height * this.options.scale, this.options.scale, this.options.scale, element);
  81. return [4 /*yield*/, exports.loadSerializedSVG(svg)];
  82. case 1:
  83. img = _a.sent();
  84. if (this.options.backgroundColor) {
  85. this.ctx.fillStyle = color_1.asString(this.options.backgroundColor);
  86. this.ctx.fillRect(0, 0, this.options.width * this.options.scale, this.options.height * this.options.scale);
  87. }
  88. this.ctx.drawImage(img, -this.options.x * this.options.scale, -this.options.y * this.options.scale);
  89. return [2 /*return*/, this.canvas];
  90. }
  91. });
  92. });
  93. };
  94. return ForeignObjectRenderer;
  95. }(renderer_1.Renderer));
  96. exports.ForeignObjectRenderer = ForeignObjectRenderer;
  97. var loadSerializedSVG = function (svg) {
  98. return new Promise(function (resolve, reject) {
  99. var img = new Image();
  100. img.onload = function () {
  101. resolve(img);
  102. };
  103. img.onerror = reject;
  104. img.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(new XMLSerializer().serializeToString(svg));
  105. });
  106. };
  107. exports.loadSerializedSVG = loadSerializedSVG;
  108. //# sourceMappingURL=foreignobject-renderer.js.map