tdesign-vue
Version:
168 lines (160 loc) • 7.4 kB
JavaScript
/**
* tdesign v1.15.0
* (c) 2026 tdesign
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default["default"](e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function generateBase64Url(_ref, onFinish) {
var width = _ref.width,
height = _ref.height,
gapX = _ref.gapX,
gapY = _ref.gapY,
offsetLeft = _ref.offsetLeft,
offsetTop = _ref.offsetTop,
rotate = _ref.rotate,
alpha = _ref.alpha,
watermarkContent = _ref.watermarkContent,
lineSpace = _ref.lineSpace,
_ref$fontColor = _ref.fontColor,
fontColor = _ref$fontColor === void 0 ? "rgba(0,0,0,0.1)" : _ref$fontColor,
layout = _ref.layout;
var isHexagonal = layout === "hexagonal";
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
if (!ctx) {
console.warn("\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301Canvas, \u65E0\u6CD5\u7ED8\u5236\u6C34\u5370");
onFinish("");
return;
}
var ratio = window.devicePixelRatio || 1;
var actualBackgroundSize = {
width: gapX + width
};
var canvasWidth = (gapX + width) * ratio;
var canvasHeight = (gapY + height) * ratio;
var markWidth = width * ratio;
var markHeight = height * ratio;
var dislocationRotateX = canvasWidth;
var dislocationRotateY = canvasHeight;
var dislocationDrawX = (gapX + width) * ratio;
var dislocationDrawY = (gapY + height) * ratio;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.width = "".concat(gapX + width, "px");
canvas.style.height = "".concat(gapY + height, "px");
if (isHexagonal) {
canvas.style.width = "".concat(canvasWidth * 2, "px");
canvas.style.height = "".concat(canvasHeight * 2, "px");
canvas.width = canvasWidth * 2;
canvas.height = canvasHeight * 2;
actualBackgroundSize = {
width: gapX + width * 2 + width / 2
};
}
ctx.translate(offsetLeft * ratio, offsetTop * ratio);
ctx.globalAlpha = alpha;
ctx.fillStyle = "transparent";
ctx.fillRect(0, 0, markWidth, markHeight);
var drawRotate = function drawRotate(ctx2, x, y, rotate2) {
ctx2.translate(x, y);
ctx2.rotate(Math.PI / 180 * Number(rotate2));
ctx2.translate(-x, -y);
};
var drawText = function drawText(ctx2, x, y, markHeight2, text, fontWeight, fontSize, fontFamily, fillStyle) {
ctx2.font = "normal normal ".concat(fontWeight, " ").concat(fontSize * ratio, "px/").concat(markHeight2, "px ").concat(fontFamily);
ctx2.fillStyle = fillStyle;
ctx2.textAlign = "start";
ctx2.textBaseline = "top";
ctx2.fillText(text, x, y);
};
var contents = Array.isArray(watermarkContent) ? watermarkContent : [_objectSpread({}, watermarkContent)];
var top = 0;
var imageLoadCount = 0;
var totalImages = 0;
contents.forEach(function (item) {
item.top = top;
if (item.url) {
top += height;
totalImages += isHexagonal ? 2 : 1;
} else if (item.text) {
top += lineSpace;
}
});
var renderWatermarkItem = function renderWatermarkItem(item) {
var offsetX = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var offsetY = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var rotateX = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var rotateY = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
if (item.url) {
var url = item.url,
_item$isGrayscale = item.isGrayscale,
isGrayscale = _item$isGrayscale === void 0 ? false : _item$isGrayscale;
var img = new Image();
img.crossOrigin = "anonymous";
img.referrerPolicy = "no-referrer";
img.src = url;
img.onload = function () {
var _ctx$save, _ctx$restore;
(_ctx$save = ctx.save) === null || _ctx$save === void 0 || _ctx$save.call(ctx);
drawRotate(ctx, rotateX, rotateY, rotate);
if (isGrayscale) {
var tempCanvas = document.createElement("canvas");
var tempCtx = tempCanvas.getContext("2d");
tempCanvas.width = width * ratio;
tempCanvas.height = height * ratio;
tempCtx.drawImage(img, 0, 0, width * ratio, height * ratio);
var imgData = tempCtx.getImageData(0, 0, width * ratio, height * ratio);
var pixels = imgData.data;
for (var i = 0; i < pixels.length; i += 4) {
var lightness = (pixels[i] + pixels[i + 1] + pixels[i + 2]) / 3;
pixels[i] = lightness;
pixels[i + 1] = lightness;
pixels[i + 2] = lightness;
}
tempCtx.putImageData(imgData, 0, 0);
ctx.drawImage(tempCanvas, offsetX, offsetY + item.top * ratio, width * ratio, height * ratio);
} else {
ctx.drawImage(img, offsetX, offsetY + item.top * ratio, width * ratio, height * ratio);
}
(_ctx$restore = ctx.restore) === null || _ctx$restore === void 0 || _ctx$restore.call(ctx);
imageLoadCount += 1;
if (imageLoadCount === totalImages) {
onFinish(canvas.toDataURL(), actualBackgroundSize);
}
};
} else if (item.text) {
var _ctx$save2, _ctx$restore2;
var text = item.text,
_item$fontSize = item.fontSize,
fontSize = _item$fontSize === void 0 ? 16 : _item$fontSize,
_item$fontFamily = item.fontFamily,
fontFamily = _item$fontFamily === void 0 ? "normal" : _item$fontFamily,
_item$fontWeight = item.fontWeight,
fontWeight = _item$fontWeight === void 0 ? "normal" : _item$fontWeight;
var fillStyle = (item === null || item === void 0 ? void 0 : item.fontColor) || fontColor;
(_ctx$save2 = ctx.save) === null || _ctx$save2 === void 0 || _ctx$save2.call(ctx);
drawRotate(ctx, rotateX, rotateY, rotate);
drawText(ctx, offsetX, offsetY + item.top * ratio, markHeight, text, fontWeight, fontSize, fontFamily, fillStyle);
(_ctx$restore2 = ctx.restore) === null || _ctx$restore2 === void 0 || _ctx$restore2.call(ctx);
}
};
contents.forEach(function (item) {
renderWatermarkItem(item, 0, 0, 0, 0);
});
if (isHexagonal) {
contents.forEach(function (item) {
renderWatermarkItem(item, dislocationDrawX, dislocationDrawY, dislocationRotateX, dislocationRotateY);
});
}
if (totalImages === 0) {
onFinish(canvas.toDataURL(), actualBackgroundSize);
}
}
exports["default"] = generateBase64Url;
//# sourceMappingURL=generateBase64Url.js.map