useCanvas.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = 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. console.log(background);
  28. await qsc.updateCanvasWH({
  29. width: background.width,
  30. height: background.bottom
  31. });
  32. let list = drawer.list;
  33. for (let i = 0; i < list.length; i++) {
  34. let item = list[i];
  35. if (item.type === "text") {
  36. await qsc.drawText(item);
  37. }
  38. if (item.type === "image") {
  39. if (item.d) {
  40. qsc.setCircle({
  41. x: item.x,
  42. y: item.y,
  43. d: item.d,
  44. clip: true
  45. });
  46. }
  47. if (item.r) {
  48. qsc.setRect({
  49. x: item.x,
  50. y: item.y,
  51. height: item.height,
  52. width: item.width,
  53. r: item.r,
  54. clip: true
  55. });
  56. }
  57. console.log(item);
  58. try {
  59. await qsc.drawImg(item);
  60. } catch (error) {
  61. console.log(error);
  62. }
  63. qsc.restore();
  64. }
  65. if (item.type === "qrcode") {
  66. await qsc.drawQrCode(item);
  67. }
  68. }
  69. await qsc.draw();
  70. setTimeout(async () => {
  71. options.src = await qsc.toImage();
  72. }, 100);
  73. return options;
  74. }
  75. exports.useCanvas = useCanvas;