antd-mobile
Version:
<div align="center">
94 lines (93 loc) • 4.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.WaterMark = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _nativeProps = require("../../utils/native-props");
var _withDefaultProps = require("../../utils/with-default-props");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const classPrefix = `adm-water-mark`;
const defaultProps = {
fullPage: true
};
const WaterMark = p => {
const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
const {
zIndex,
gapX = 24,
gapY = 48,
width = 120,
height = 64,
rotate = -22,
image,
imageWidth = 120,
imageHeight = 64,
content,
fontStyle = 'normal',
fontWeight = 'normal',
fontColor = 'rgba(0,0,0,.15)',
fontSize = 14,
fontFamily = 'sans-serif'
} = props;
const [base64Url, setBase64Url] = (0, _react.useState)('');
(0, _react.useEffect)(() => {
const canvas = document.createElement('canvas');
const ratio = window.devicePixelRatio;
const ctx = canvas.getContext('2d');
const canvasWidth = `${(gapX + width) * ratio}px`;
const canvasHeight = `${(gapY + height) * ratio}px`;
const markWidth = width * ratio;
const markHeight = height * ratio;
canvas.setAttribute('width', canvasWidth);
canvas.setAttribute('height', canvasHeight);
if (ctx) {
if (image) {
ctx.translate(markWidth / 2, markHeight / 2);
ctx.rotate(Math.PI / 180 * Number(rotate));
const img = new Image();
img.crossOrigin = 'anonymous';
img.referrerPolicy = 'no-referrer';
img.onload = () => {
ctx.drawImage(img, -imageWidth * ratio / 2, -imageHeight * ratio / 2, imageWidth * ratio, imageHeight * ratio);
ctx.restore();
setBase64Url(canvas.toDataURL());
};
img.src = image;
} else if (content) {
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
// 文字绕中间旋转
ctx.translate(markWidth / 2, markHeight / 2);
ctx.rotate(Math.PI / 180 * Number(rotate));
const markSize = Number(fontSize) * ratio;
ctx.font = `${fontStyle} normal ${fontWeight} ${markSize}px/${markHeight}px ${fontFamily}`;
ctx.fillStyle = fontColor;
if (Array.isArray(content)) {
content.forEach((item, index) => ctx.fillText(item, 0, index * markSize));
} else {
ctx.fillText(content, 0, 0);
}
ctx.restore();
setBase64Url(canvas.toDataURL());
}
} else {
throw new Error('Canvas is not supported in the current environment');
}
}, [gapX, gapY, rotate, fontStyle, fontWeight, width, height, fontFamily, fontColor, image, content, fontSize]);
return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
className: (0, _classnames.default)(classPrefix, {
[`${classPrefix}-full-page`]: props.fullPage
}),
style: {
zIndex,
backgroundSize: `${gapX + width}px`,
// Not give `url` if its empty. Which will cause 404 error.
backgroundImage: base64Url === '' ? undefined : `url('${base64Url}')`
}
}));
};
exports.WaterMark = WaterMark;
;