tdesign-mobile-vue
Version:
tdesign-mobile-vue
104 lines (96 loc) • 4.46 kB
JavaScript
/**
* tdesign v1.7.0
* (c) 2024 TDesign Group
* @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;
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 canvasWidth = (gapX + width) * ratio;
var canvasHeight = (gapY + height) * ratio;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.width = "".concat(gapX + width, "px");
canvas.style.height = "".concat(gapY + height, "px");
ctx.translate(offsetLeft * ratio, offsetTop * ratio);
ctx.rotate(Math.PI / 180 * Number(rotate));
ctx.globalAlpha = alpha;
var markWidth = width * ratio;
var markHeight = height * ratio;
ctx.fillStyle = "transparent";
ctx.fillRect(0, 0, markWidth, markHeight);
var contents = Array.isArray(watermarkContent) ? watermarkContent : [_objectSpread({}, watermarkContent)];
var top = 0;
contents.forEach(function (item) {
if (item.url) {
var url = item.url,
_item$isGrayscale = item.isGrayscale,
isGrayscale = _item$isGrayscale === void 0 ? false : _item$isGrayscale;
item.top = top;
top += height;
var img = new Image();
img.crossOrigin = "anonymous";
img.referrerPolicy = "no-referrer";
img.src = url;
img.onload = function () {
ctx.drawImage(img, 0, item.top * ratio, width * ratio, height * ratio);
if (isGrayscale) {
var imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
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;
}
ctx.putImageData(imgData, 0, 0);
}
onFinish(canvas.toDataURL());
};
} else if (item.text) {
var text = item.text,
_item$fontColor = item.fontColor,
fontColor = _item$fontColor === void 0 ? "rgba(0, 0, 0, 0.1)" : _item$fontColor,
_item$fontSize = item.fontSize,
fontSize = _item$fontSize === void 0 ? 16 : _item$fontSize,
_item$fontFamily = item.fontFamily,
fontFamily = _item$fontFamily === void 0 ? void 0 : _item$fontFamily,
_item$fontWeight = item.fontWeight,
fontWeight = _item$fontWeight === void 0 ? "normal" : _item$fontWeight;
item.top = top;
top += lineSpace;
var markSize = Number(fontSize) * ratio;
ctx.font = "normal normal ".concat(fontWeight, " ").concat(markSize, "px/").concat(markHeight, "px ").concat(fontFamily);
ctx.textAlign = "start";
ctx.textBaseline = "top";
ctx.fillStyle = fontColor;
ctx.fillText(text, 0, item.top * ratio);
}
});
onFinish(canvas.toDataURL());
}
exports["default"] = generateBase64Url;
//# sourceMappingURL=generateBase64Url.js.map