+ The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
+
+
+ Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
+
+
+ Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
+
+ The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
+
+
+ Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
+
+
+ Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
+
+ The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
+
+
+ Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
+
+
+ Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
+
- The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
-
-
- Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
-
-
- Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
-
+ The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
+
+
+ Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
+
+
+ Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
+
- The light-speed iteration of the digital world makes products more complex. However, human consciousness and attention resources are limited. Facing this design contradiction, the pursuit of natural interaction will be the consistent direction of Ant Design.
-
-
- Natural user cognition: According to cognitive psychology, about 80% of external information is obtained through visual channels. The most important visual elements in the interface design, including layout, colors, illustrations, icons, etc., should fully absorb the laws of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth feelings. In some scenarios, opportunely adding other sensory channels such as hearing, touch can create a richer and more natural product experience.
-
-
- Natural user behavior: In the interaction with the system, the designer should fully understand the relationship between users, system roles, and task objectives, and also contextually organize system functions and services. At the same time, a series of methods such as behavior analysis, artificial intelligence and sensors could be applied to assist users to make effective decisions and reduce extra operations of users, to save users' mental and physical resources and make human-computer interaction more natural.
-
+
+
+
+ The light-speed iteration of the digital world makes products more complex. However,
+ human consciousness and attention resources are limited. Facing this design
+ contradiction, the pursuit of natural interaction will be the consistent direction of
+ Ant Design.
+
+
+ Natural user cognition: According to cognitive psychology, about 80% of external
+ information is obtained through visual channels. The most important visual elements in
+ the interface design, including layout, colors, illustrations, icons, etc., should fully
+ absorb the laws of nature, thereby reducing the user's cognitive cost and bringing
+ authentic and smooth feelings. In some scenarios, opportunely adding other sensory
+ channels such as hearing, touch can create a richer and more natural product experience.
+
+
+ Natural user behavior: In the interaction with the system, the designer should fully
+ understand the relationship between users, system roles, and task objectives, and also
+ contextually organize system functions and services. At the same time, a series of
+ methods such as behavior analysis, artificial intelligence and sensors could be applied
+ to assist users to make effective decisions and reduce extra operations of users, to
+ save users' mental and physical resources and make human-computer interaction more
+ natural.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ );
+};
+
+export default App;
diff --git a/components/watermark/demo/z-index.md b/components/watermark/demo/z-index.md
deleted file mode 100644
index ee397be7b311..000000000000
--- a/components/watermark/demo/z-index.md
+++ /dev/null
@@ -1,7 +0,0 @@
-## zh-CN
-
-zIndex 默认值为 9,通过设置更大的 `zIndex` 堆叠在元素的顶级。
-
-## en-US
-
-zIndex defaults to 9, by setting a larger `zIndex` to stack at the top level of the element.
diff --git a/components/watermark/demo/z-index.tsx b/components/watermark/demo/z-index.tsx
deleted file mode 100644
index 1000c94f280f..000000000000
--- a/components/watermark/demo/z-index.tsx
+++ /dev/null
@@ -1,44 +0,0 @@
-import React from 'react';
-import { Watermark, Typography } from 'antd';
-
-const { Paragraph } = Typography;
-
-const App: React.FC = () => (
-
-
-
- The light-speed iteration of the digital world makes products more complex. However, human
- consciousness and attention resources are limited. Facing this design contradiction, the
- pursuit of natural interaction will be the consistent direction of Ant Design.
-
-
- Natural user cognition: According to cognitive psychology, about 80% of external information
- is obtained through visual channels. The most important visual elements in the interface
- design, including layout, colors, illustrations, icons, etc., should fully absorb the laws
- of nature, thereby reducing the user's cognitive cost and bringing authentic and smooth
- feelings. In some scenarios, opportunely adding other sensory channels such as hearing,
- touch can create a richer and more natural product experience.
-
-
- Natural user behavior: In the interaction with the system, the designer should fully
- understand the relationship between users, system roles, and task objectives, and also
- contextually organize system functions and services. At the same time, a series of methods
- such as behavior analysis, artificial intelligence and sensors could be applied to assist
- users to make effective decisions and reduce extra operations of users, to save users'
- mental and physical resources and make human-computer interaction more natural.
-
-
-
-
-);
-
-export default App;
diff --git a/components/watermark/index.en-US.md b/components/watermark/index.en-US.md
index f7f9d2d62294..76528c01e459 100644
--- a/components/watermark/index.en-US.md
+++ b/components/watermark/index.en-US.md
@@ -20,7 +20,7 @@ Add specific text or patterns to the page.
BasicMulti-line watermarkImage watermark
-High priority
+Custom configuration
## API
@@ -28,8 +28,8 @@ Add specific text or patterns to the page.
| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
-| width | The width of the watermark | number | 120 | |
-| height | The height of the watermark | number | 64 | |
+| width | The width of the watermark, the default value of `content` is its own width | number | 120 | |
+| height | The height of the watermark, the default value of `content` is its own height | number | 64 | |
| rotate | When the watermark is drawn, the rotation Angle, unit `°` | number | -22 | |
| zIndex | The z-index of the appended watermark element | number | 9 | |
| image | Image source, it is recommended to export 2x or 3x image, high priority | string | - | |
diff --git a/components/watermark/index.tsx b/components/watermark/index.tsx
index 50ea2dba240d..d6f65fb6df5b 100644
--- a/components/watermark/index.tsx
+++ b/components/watermark/index.tsx
@@ -1,6 +1,8 @@
import React, { useEffect, useMemo, useRef } from 'react';
import useMutationObserver from './useMutationObserver';
+const FontGap = 3;
+
const getStyleStr = (style: Record): string => {
let styleStr = '';
Object.keys(style).forEach((item) => {
@@ -39,8 +41,8 @@ const Watermark: React.FC = (props) => {
*/
zIndex = 9,
rotate = -22,
- width = 120,
- height = 64,
+ width,
+ height,
image,
content,
font = {},
@@ -88,18 +90,18 @@ const Watermark: React.FC = (props) => {
};
}, [offset, gap]);
- const markStyle: React.CSSProperties = {
+ const getMarkStyle = (markWidth: number) => ({
zIndex,
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%',
- backgroundSize: `${gapX + width}px`,
+ backgroundSize: `${gapX + markWidth}px`,
pointerEvents: 'none',
backgroundRepeat: 'repeat',
...offsetStyle,
- };
+ });
const containerRef = useRef(null);
const watermarkDom = useRef();
@@ -133,14 +135,14 @@ const Watermark: React.FC = (props) => {
return flag;
};
- const appendWatermark = (base64Url: string) => {
+ const appendWatermark = (base64Url: string, markWidth: number) => {
if (containerRef.current && watermarkDom.current) {
destroyObserver();
watermarkDom.current.setAttribute(WATERMARK_ID_NAME, watermarkId);
watermarkDom.current.setAttribute(
'style',
getStyleStr({
- ...markStyle,
+ ...getMarkStyle(markWidth),
backgroundImage: `url('${base64Url}')`,
}),
);
@@ -157,6 +159,23 @@ const Watermark: React.FC = (props) => {
}
};
+ /**
+ * Get the width and height of the watermark. The default values are as follows
+ * Image: [120, 64]; Content: It's calculated by content;
+ */
+ const getMarkSize = (ctx: CanvasRenderingContext2D) => {
+ let defaultWidth = 120;
+ let defaultHeight = 64;
+ if (!image && ctx.measureText) {
+ ctx.font = `${Number(fontSize)}px ${fontFamily}`;
+ const contents = Array.isArray(content) ? content : [content];
+ const widths = contents.map((item) => ctx.measureText(item as string).width);
+ defaultWidth = Math.ceil(Math.max(...widths));
+ defaultHeight = Number(fontSize) * contents.length + (contents.length - 1) * FontGap;
+ }
+ return [width ?? defaultWidth, height ?? defaultHeight];
+ };
+
const renderWatermark = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
@@ -166,21 +185,21 @@ const Watermark: React.FC = (props) => {
watermarkDom.current = document.createElement('div');
}
- const ratio = window.devicePixelRatio;
- const canvasWidth = `${(gapX + width) * ratio}px`;
- const canvasHeight = `${(gapY + height) * ratio}px`;
-
+ const ratio = window.devicePixelRatio || 1;
+ const [markWidth, markHeight] = getMarkSize(ctx);
+ const canvasWidth = `${(gapX + markWidth) * ratio}px`;
+ const canvasHeight = `${(gapY + markHeight) * ratio}px`;
canvas.setAttribute('width', canvasWidth);
canvas.setAttribute('height', canvasHeight);
- const markWidth = width * ratio;
- const markHeight = height * ratio;
+ const mergedMarkWidth = markWidth * ratio;
+ const mergedMarkHeight = markHeight * ratio;
const gapXCenter = (gapX * ratio) / 2;
const gapYCenter = (gapY * ratio) / 2;
/** Rotate with the canvas as the center point */
- const centerX = (markWidth + gapX * ratio) / 2;
- const centerY = (markHeight + gapY * ratio) / 2;
+ const centerX = (mergedMarkWidth + gapX * ratio) / 2;
+ const centerY = (mergedMarkHeight + gapY * ratio) / 2;
ctx.translate(centerX, centerY);
ctx.rotate((Math.PI / 180) * Number(rotate));
ctx.translate(-centerX, -centerY);
@@ -188,27 +207,27 @@ const Watermark: React.FC = (props) => {
if (image) {
const img = new Image();
img.onload = () => {
- ctx.drawImage(img, gapXCenter, gapYCenter, markWidth, markHeight);
- appendWatermark(canvas.toDataURL());
+ ctx.drawImage(img, gapXCenter, gapYCenter, mergedMarkWidth, mergedMarkHeight);
+ appendWatermark(canvas.toDataURL(), markWidth);
};
img.crossOrigin = 'anonymous';
img.referrerPolicy = 'no-referrer';
img.src = image;
- } else if (content) {
- const markSize = Number(fontSize) * ratio;
- const fontGap = ratio * 3;
- ctx.font = `${fontStyle} normal ${fontWeight} ${markSize}px/${markHeight}px ${fontFamily}`;
+ } else {
+ const mergedFontSize = Number(fontSize) * ratio;
+ ctx.font = `${fontStyle} normal ${fontWeight} ${mergedFontSize}px/${mergedMarkHeight}px ${fontFamily}`;
ctx.fillStyle = color;
ctx.textAlign = 'center';
- ctx.translate(markWidth / 2, markSize);
+ ctx.textBaseline = 'top';
+ ctx.translate(mergedMarkWidth / 2, 0);
if (Array.isArray(content)) {
content?.forEach((item: string, index: number) =>
- ctx.fillText(item, gapXCenter, gapYCenter + index * (markSize + fontGap)),
+ ctx.fillText(item, gapXCenter, gapYCenter + index * (mergedFontSize + FontGap * ratio)),
);
} else {
- ctx.fillText(content, gapXCenter, gapYCenter);
+ ctx.fillText(content ?? '', gapXCenter, gapYCenter);
}
- appendWatermark(canvas.toDataURL());
+ appendWatermark(canvas.toDataURL(), markWidth);
}
}
};
diff --git a/components/watermark/index.zh-CN.md b/components/watermark/index.zh-CN.md
index 8b4f29efa8fd..d9f14b8b4725 100644
--- a/components/watermark/index.zh-CN.md
+++ b/components/watermark/index.zh-CN.md
@@ -21,7 +21,7 @@ demo:
基本多行水印图片水印
-高优先级
+自定义配置
## API
@@ -29,8 +29,8 @@ demo:
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
-| width | 水印的宽度 | number | 120 | |
-| height | 水印的高度 | number | 64 | |
+| width | 水印的宽度,`content` 的默认值为自身的宽度 | number | 120 | |
+| height | 水印的高度,`content` 的默认值为自身的高度 | number | 64 | |
| rotate | 水印绘制时,旋转的角度,单位 `°` | number | -22 | |
| zIndex | 追加的水印元素的 z-index | number | 9 | |
| image | 图片源,建议导出 2 倍或 3 倍图,优先级高 | string | - | |
From a8df8747f7cb2b16056360a2302aec64ac21b855 Mon Sep 17 00:00:00 2001
From: JarvisArt <1120886013@qq.com>
Date: Wed, 7 Dec 2022 14:51:26 +0800
Subject: [PATCH 3/7] docs: update docs
---
components/watermark/index.en-US.md | 17 ++++++++---------
components/watermark/index.zh-CN.md | 17 ++++++++---------
2 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/components/watermark/index.en-US.md b/components/watermark/index.en-US.md
index 76528c01e459..dde4404f9632 100644
--- a/components/watermark/index.en-US.md
+++ b/components/watermark/index.en-US.md
@@ -35,17 +35,16 @@ Add specific text or patterns to the page.
| image | Image source, it is recommended to export 2x or 3x image, high priority | string | - | |
| content | Watermark text content | string \| string[] | - | |
| font | Text style | [Font](#Font) | [Font](#Font) | |
-| style | Container layer style | React.CSSProperties | - | |
-| className | The className of the container layer | string | - | |
| gap | The spacing between watermarks | \[number, number\] | \[200, 200\] | |
| offset | The offset of the watermark from the upper left corner of the container. The default is `gap/2` | \[number, number\] | \[gap\[0\]/2, gap\[1\]/2\] | |
### Font
-| 参数 | 说明 | 类型 | 默认值 | 版本 |
-| ---------- | ----------- | ------------------------------------------- | --------------- | ---- |
-| color | font color | string | rgba(0,0,0,.15) | |
-| fontSize | font size | number | 16 | |
-| fontWeight | font weight | `normal` \| `light` \| `weight` \| number | normal | |
-| fontFamily | font family | string | sans-serif | |
-| fontStyle | font style | `none` \| `normal` \| `italic` \| `oblique` | normal | |
+
+| 参数 | 说明 | 类型 | 默认值 | 版本 |
+| --- | --- | --- | --- | --- |
+| color | font color | string | rgba(0,0,0,.15) | |
+| fontSize | font size | number | 16 | |
+| fontWeight | font weight | `normal` \| `light` \| `weight` \| number | normal | |
+| fontFamily | font family | string | sans-serif | |
+| fontStyle | font style | `none` \| `normal` \| `italic` \| `oblique` | normal | |
diff --git a/components/watermark/index.zh-CN.md b/components/watermark/index.zh-CN.md
index d9f14b8b4725..e8bc8d2a99a9 100644
--- a/components/watermark/index.zh-CN.md
+++ b/components/watermark/index.zh-CN.md
@@ -36,17 +36,16 @@ demo:
| image | 图片源,建议导出 2 倍或 3 倍图,优先级高 | string | - | |
| content | 水印文字内容 | string \| string[] | - | |
| font | 文字样式 | [Font](#Font) | [Font](#Font) | |
-| style | 容器层的样式 | React.CSSProperties | - | |
-| className | 容器层的类名 | string | - | |
| gap | 水印之间的间距 | \[number, number\] | \[200, 200\] | |
| offset | 水印距离容器左上角的偏移量,默认为 `gap/2` | \[number, number\] | \[gap\[0\]/2, gap\[1\]/2\] | |
### Font
-| 参数 | 说明 | 类型 | 默认值 | 版本 |
-| ---------- | -------- | ------------------------------------------- | --------------- | ---- |
-| color | 字体颜色 | string | rgba(0,0,0,.15) | |
-| fontSize | 字体大小 | number | 16 | |
-| fontWeight | 字体粗细 | `normal` \| `light` \| `weight` \| number | normal | |
-| fontFamily | 字体类型 | string | sans-serif | |
-| fontStyle | 字体样式 | `none` \| `normal` \| `italic` \| `oblique` | normal | |
+
+| 参数 | 说明 | 类型 | 默认值 | 版本 |
+| --- | --- | --- | --- | --- |
+| color | 字体颜色 | string | rgba(0,0,0,.15) | |
+| fontSize | 字体大小 | number | 16 | |
+| fontWeight | 字体粗细 | `normal` \| `light` \| `weight` \| number | normal | |
+| fontFamily | 字体类型 | string | sans-serif | |
+| fontStyle | 字体样式 | `none` \| `normal` \| `italic` \| `oblique` | normal | |
From 8fa6e5ad85e3683353949baec967ef1cd342a2f9 Mon Sep 17 00:00:00 2001
From: JarvisArt <1120886013@qq.com>
Date: Wed, 7 Dec 2022 15:55:53 +0800
Subject: [PATCH 4/7] chore: update bundlesize
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index d1c494b1e97d..07eb87603b53 100644
--- a/package.json
+++ b/package.json
@@ -324,7 +324,7 @@
"bundlesize": [
{
"path": "./dist/antd.min.js",
- "maxSize": "381 kB"
+ "maxSize": "382 kB"
}
],
"tnpm": {
From 64990427201097aeecead60c05db7a6a4dbb3d74 Mon Sep 17 00:00:00 2001
From: JarvisArt <1120886013@qq.com>
Date: Thu, 8 Dec 2022 01:00:02 +0800
Subject: [PATCH 5/7] chore: Optimize code logic
---
.../__snapshots__/index.test.tsx.snap | 27 ++--
components/watermark/__tests__/index.test.tsx | 5 +-
components/watermark/demo/custom.tsx | 1 -
components/watermark/index.tsx | 137 ++++++++++--------
components/watermark/useMutationObserver.ts | 15 +-
5 files changed, 94 insertions(+), 91 deletions(-)
diff --git a/components/watermark/__tests__/__snapshots__/index.test.tsx.snap b/components/watermark/__tests__/__snapshots__/index.test.tsx.snap
index 4dfac1422744..86c1309b6629 100644
--- a/components/watermark/__tests__/__snapshots__/index.test.tsx.snap
+++ b/components/watermark/__tests__/__snapshots__/index.test.tsx.snap
@@ -6,8 +6,7 @@ exports[`Watermark Image watermark snapshot 1`] = `
style="position: relative;"
>
@@ -30,7 +29,20 @@ exports[`Watermark Observe the modification of data-watermark-id 1`] = `
>
+
+
+`;
+
+exports[`Watermark Observe the modification of style 1`] = `
+