useCanvas.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const sheep_components_sShareModal_canvasPoster_poster_index = require("./poster/index.js");
  4. async function useCanvas(options, vm) {
  5. const width = options.width;
  6. const qsc = new common_vendor.QSCanvas(
  7. {
  8. canvasId: options.canvasId,
  9. width: options.width,
  10. height: options.height,
  11. setCanvasWH: (canvas) => {
  12. options.height = canvas.height;
  13. }
  14. },
  15. vm
  16. );
  17. let drawer = await sheep_components_sShareModal_canvasPoster_poster_index.getPosterData(options);
  18. const background = await qsc.drawImg({
  19. type: "image",
  20. val: drawer.background,
  21. x: 0,
  22. y: 0,
  23. width,
  24. mode: "widthFix",
  25. zIndex: 0
  26. });
  27. await qsc.updateCanvasWH({
  28. width: background.width,
  29. height: background.bottom
  30. });
  31. let list = drawer.list;
  32. for (let i = 0; i < list.length; i++) {
  33. let item = list[i];
  34. if (item.type === "text") {
  35. await qsc.drawText(item);
  36. }
  37. if (item.type === "image") {
  38. if (item.d) {
  39. qsc.setCircle({
  40. x: item.x,
  41. y: item.y,
  42. d: item.d,
  43. clip: true
  44. });
  45. }
  46. if (item.r) {
  47. qsc.setRect({
  48. x: item.x,
  49. y: item.y,
  50. height: item.height,
  51. width: item.width,
  52. r: item.r,
  53. clip: true
  54. });
  55. }
  56. try {
  57. await qsc.drawImg(item);
  58. } catch (error) {
  59. console.log(error);
  60. }
  61. qsc.restore();
  62. }
  63. if (item.type === "rect") {
  64. if (item.r) {
  65. qsc.setRect({
  66. x: item.x,
  67. y: item.y,
  68. height: item.height,
  69. width: item.width,
  70. r: item.r,
  71. clip: true
  72. });
  73. }
  74. try {
  75. await qsc.draw(item);
  76. } catch (error) {
  77. console.log(error);
  78. }
  79. qsc.restore();
  80. }
  81. if (item.type === "qrcode") {
  82. console.log(item);
  83. await qsc.drawQrCode(item);
  84. }
  85. }
  86. await qsc.draw();
  87. setTimeout(async () => {
  88. options.src = await qsc.toImage();
  89. }, 100);
  90. return options;
  91. }
  92. exports.useCanvas = useCanvas;