@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
92 lines (91 loc) • 4.63 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useEffect, useState } from "react";
import classNames from "classnames";
import { useConfig } from "../configprovider";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
content: '',
fullPage: true,
zIndex: 2000,
gapX: 24,
gapY: 48,
width: 120,
height: 64,
startX: 0,
startY: 0,
image: '',
imageWidth: 120,
imageHeight: 64,
rotate: -22,
color: 'rgba(0,0,0,.15)',
fontStyle: 'normal',
fontWeight: 'normal',
fontSize: 14
});
export var WaterMark = function(props) {
var locale = useConfig().locale;
var _$_object_spread = _object_spread({}, defaultProps, props), content = _$_object_spread.content, fullPage = _$_object_spread.fullPage, zIndex = _$_object_spread.zIndex, className = _$_object_spread.className, gapX = _$_object_spread.gapX, gapY = _$_object_spread.gapY, startX = _$_object_spread.startX, startY = _$_object_spread.startY, width = _$_object_spread.width, height = _$_object_spread.height, image = _$_object_spread.image, imageWidth = _$_object_spread.imageWidth, imageHeight = _$_object_spread.imageHeight, rotate = _$_object_spread.rotate, color = _$_object_spread.color, fontStyle = _$_object_spread.fontStyle, fontFamily = _$_object_spread.fontFamily, fontWeight = _$_object_spread.fontWeight, fontSize = _$_object_spread.fontSize, style = _$_object_spread.style;
var _useState = _sliced_to_array(useState(''), 2), base64Url = _useState[0], setBase64Url = _useState[1];
var classPrefix = 'nut-watermark';
var classes = classNames(classPrefix, _define_property({}, "".concat(classPrefix, "-full-page"), fullPage));
var cls = classNames(classes, className);
useEffect(function() {
init();
}, []);
var init = function() {
var canvas = document.createElement('canvas');
var ratio = window.devicePixelRatio;
var ctx = canvas.getContext('2d');
var canvasWidth = "".concat((gapX + width) * ratio, "px");
var canvasHeight = "".concat((gapY + height) * ratio, "px");
var markWidth = width * ratio;
var 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));
var img = new Image();
img.crossOrigin = 'anonymous';
img.referrerPolicy = 'no-referrer';
img.src = image;
img.onload = function() {
ctx.drawImage(img, -imageWidth * ratio / 2, -imageHeight * ratio / 2, imageWidth * ratio, imageHeight * ratio);
ctx.restore();
setBase64Url(canvas.toDataURL());
};
} else if (content) {
ctx.textBaseline = 'middle' // 设置或返回在绘制文本时使用的当前文本基线。(正中)
;
ctx.textAlign = 'center' // 设置或返回文本内容的当前对齐方式。
;
// 文字绕中间旋转
ctx.translate(markWidth / 2, markHeight / 2);
ctx.rotate(Math.PI / 180 * Number(rotate));
var markSize = Number(fontSize) * ratio;
ctx.font = "".concat(fontStyle, " normal ").concat(fontWeight, " ").concat(markSize, "px/").concat(markHeight, "px ").concat(fontFamily);
ctx.fillStyle = color;
ctx.fillText(content, startX, startY) // 在画布上绘制"被填充的"文本。
;
ctx.restore() // 返回之前保存过的路径状态和属性。
;
setBase64Url(canvas.toDataURL());
}
} else {
throw new Error(locale.watermark.errorCanvasTips);
}
};
return /*#__PURE__*/ React.createElement("div", {
className: cls,
style: _object_spread({
zIndex: zIndex,
backgroundSize: "".concat(gapX + width, "px"),
backgroundImage: "url('".concat(base64Url, "')")
}, style)
});
};
WaterMark.displayName = 'NutWaterMark';