UNPKG

@nutui/nutui-react

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

104 lines (103 loc) 5.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "WaterMark", { enumerable: true, get: function() { return WaterMark; } }); var _interop_require_default = require("@swc/helpers/_/_interop_require_default"); var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); var _define_property = require("@swc/helpers/_/_define_property"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_spread_props = require("@swc/helpers/_/_object_spread_props"); var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array"); var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react")); var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames")); var _configprovider = require("../configprovider"); var _typings = require("../../utils/typings"); var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.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 }); var WaterMark = function WaterMark(props) { var locale = (0, _configprovider.useConfig)().locale; var _$_object_spread = (0, _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 = (0, _sliced_to_array._)((0, _react.useState)(''), 2), base64Url = _useState[0], setBase64Url = _useState[1]; var classPrefix = 'nut-watermark'; var classes = (0, _classnames.default)(classPrefix, (0, _define_property._)({}, "".concat(classPrefix, "-full-page"), fullPage)); var cls = (0, _classnames.default)(classes, className); (0, _react.useEffect)(function() { init(); }, []); var init = function init() { 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.default.createElement("div", { className: cls, style: (0, _object_spread._)({ zIndex: zIndex, backgroundSize: "".concat(gapX + width, "px"), backgroundImage: "url('".concat(base64Url, "')") }, style) }); }; WaterMark.displayName = 'NutWaterMark';