antd-mobile-taro-ui
Version:
以antd-mobile为设计标准,基于taro框架的微信小程序组件库
142 lines (120 loc) • 5.65 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("antd-mobile/es/utils/native-props");
var _withDefaultProps = require("antd-mobile/es/utils/with-default-props");
var _components = require("@tarojs/components");
var _taro = _interopRequireDefault(require("@tarojs/taro"));
var _client = require("../../utils/client");
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 = 2000,
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)('');
const canvasRef = (0, _react.useRef)(null);
(0, _react.useEffect)(() => {
setTimeout(() => {
var _a, _b;
const canvasDraw = (canvas, ratio, img) => {
const ctx = canvas.getContext('2d');
const canvasWidth = (gapX + width) * ratio;
const canvasHeight = (gapY + height) * ratio;
const markWidth = width * ratio;
const markHeight = height * ratio;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
if (ctx) {
if (image) {
ctx.translate(markWidth / 2, markHeight / 2);
ctx.rotate(Math.PI / 180 * Number(rotate));
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;
ctx.fillText(content, 0, 0);
ctx.restore();
setBase64Url(canvas.toDataURL());
}
} else {
throw new Error('Canvas is not supported in the current environment');
}
};
if (_client.isWeapp) {
const query = _taro.default.createSelectorQuery();
if (!canvasRef.current) return;
if ((_a = canvasRef.current) === null || _a === void 0 ? void 0 : _a.sid) {
query.select(`#${(_b = canvasRef.current) === null || _b === void 0 ? void 0 : _b.sid}`).fields({
node: true,
size: true
}).exec(res => {
var _a, _b;
const canvas = (_a = res[0]) === null || _a === void 0 ? void 0 : _a.node;
const ratio = ((_b = _taro.default.getSystemInfoSync()) === null || _b === void 0 ? void 0 : _b.pixelRatio) || 1;
const img = canvas.createImage();
canvasDraw(canvas, ratio, img);
});
}
} else if (_client.isH5) {
const canvas = document.createElement('canvas');
const ratio = window.devicePixelRatio;
const img = new Image();
canvasDraw(canvas, ratio, img);
}
}, 0);
}, [gapX, gapY, rotate, fontStyle, fontWeight, width, height, fontFamily, fontColor, image, content, fontSize]);
return (0, _nativeProps.withNativeProps)(props, _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_components.View, {
className: (0, _classnames.default)(classPrefix, {
[`${classPrefix}-full-page`]: props.fullPage
}),
style: {
zIndex,
backgroundSize: `${gapX + width}px`,
backgroundImage: `url('${base64Url}')`
}
}), _react.default.createElement(_components.Canvas, {
type: '2d',
style: {
display: 'none'
},
ref: canvasRef
})));
};
exports.WaterMark = WaterMark;