diff --git a/components/qrcode/QRCodeCanvas.tsx b/components/qrcode/QRCode.tsx similarity index 77% rename from components/qrcode/QRCodeCanvas.tsx rename to components/qrcode/QRCode.tsx index 787c89946..7e18176c0 100644 --- a/components/qrcode/QRCodeCanvas.tsx +++ b/components/qrcode/QRCode.tsx @@ -265,3 +265,85 @@ export const QRCodeCanvas = defineComponent({ }; }, }); + +export const QRCodeSVG = defineComponent({ + name: 'QRCodeSVG', + inheritAttrs: false, + props: { + ...qrProps(), + color: String, + level: String, + bgColor: String, + fgColor: String, + marginSize: Number, + title: String, + }, + setup(props) { + let cells = null; + let margin = null; + let numCells = null; + let calculatedImageSettings = null; + + let fgPath = null; + let image = null; + + watchEffect(() => { + const { + value, + size = DEFAULT_SIZE, + level = DEFAULT_LEVEL, + includeMargin = DEFAULT_INCLUDEMARGIN, + marginSize, + imageSettings, + } = props; + + cells = qrcodegen.QrCode.encodeText(value, ERROR_LEVEL_MAP[level]).getModules(); + + margin = getMarginSize(includeMargin, marginSize); + numCells = cells.length + margin * 2; + calculatedImageSettings = getImageSettings(cells, size, margin, imageSettings); + + if (imageSettings != null && calculatedImageSettings != null) { + if (calculatedImageSettings.excavation != null) { + cells = excavateModules(cells, calculatedImageSettings.excavation); + } + + image = ( + + ); + } + + // Drawing strategy: instead of a rect per module, we're going to create a + // single path for the dark modules and layer that on top of a light rect, + // for a total of 2 DOM nodes. We pay a bit more in string concat but that's + // way faster than DOM ops. + // For level 1, 441 nodes -> 2 + // For level 40, 31329 -> 2 + fgPath = generatePath(cells, margin); + }); + + return () => { + const bgColor = props.bgColor && DEFAULT_BGCOLOR; + const fgColor = props.fgColor && DEFAULT_FGCOLOR; + return ( + + {!!props.title && {props.title}} + + + {image} + + ); + }; + }, +}); diff --git a/components/qrcode/__tests__/__snapshots__/demo.test.js.snap b/components/qrcode/__tests__/__snapshots__/demo.test.js.snap index 6f5c081d6..e05657599 100644 --- a/components/qrcode/__tests__/__snapshots__/demo.test.js.snap +++ b/components/qrcode/__tests__/__snapshots__/demo.test.js.snap @@ -39,6 +39,29 @@ exports[`renders ./components/qrcode/demo/customSize.vue correctly 1`] = ` `; +exports[`renders ./components/qrcode/demo/customType.vue correctly 1`] = ` +
+
+
+ + +
+
+ +
+
+ + + + + + +
+
+ +
+`; + exports[`renders ./components/qrcode/demo/download.vue correctly 1`] = `
diff --git a/components/qrcode/demo/base.vue b/components/qrcode/demo/base.vue index 39bf6f438..6aa48c319 100644 --- a/components/qrcode/demo/base.vue +++ b/components/qrcode/demo/base.vue @@ -15,5 +15,14 @@ Basic Usage. + + diff --git a/components/qrcode/demo/customColor.vue b/components/qrcode/demo/customColor.vue index 00ba76498..8ce1bec3b 100644 --- a/components/qrcode/demo/customColor.vue +++ b/components/qrcode/demo/customColor.vue @@ -16,7 +16,18 @@ Custom Color. + + diff --git a/components/qrcode/demo/customSize.vue b/components/qrcode/demo/customSize.vue index bc3ddc1be..caf504b07 100644 --- a/components/qrcode/demo/customSize.vue +++ b/components/qrcode/demo/customSize.vue @@ -31,7 +31,7 @@ Custom Size. :size="size" :icon-size="size / 4" error-level="H" - value="https://www.antv.com" + value="https://www.antdv.com" icon="https://www.antdv.com/assets/logo.1ef800a8.svg" /> diff --git a/components/qrcode/demo/customType.vue b/components/qrcode/demo/customType.vue new file mode 100644 index 000000000..13fb7a0ac --- /dev/null +++ b/components/qrcode/demo/customType.vue @@ -0,0 +1,22 @@ + +--- +order: 4 +title: + zh-CN: 自定义渲染类型 + en-US: Custom Render Type +--- + +## zh-CN + +通过设置 `type` 自定义渲染结果,提供 `canvas` 和 `svg` 两个选项。 + +## en-US +Customize the rendering results by `type`, provide options `canvas` and `svg`. + + + diff --git a/components/qrcode/demo/download.vue b/components/qrcode/demo/download.vue index bb432b3ac..b82bfd8b8 100644 --- a/components/qrcode/demo/download.vue +++ b/components/qrcode/demo/download.vue @@ -15,7 +15,7 @@ A way to download QRCode.