xl-public-utils 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +130 -0
- package/index.js +4 -0
- package/package.json +27 -0
- package/src/qrcode.js +99 -0
- package/src/vtk.js +28 -0
- package/src/window.js +32 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} QrCodeOptions
|
|
5
|
+
* @property {string} [bcid='qrcode'] - 二维码类型,具体支持请查看文档
|
|
6
|
+
* @property {number} [width=1056] - 最终图片宽度
|
|
7
|
+
* @property {number} [height=342] - 最终图片高度
|
|
8
|
+
* @property {number} [qrcodeW=300] - 二维码宽度
|
|
9
|
+
* @property {number} [qrcodeH=300] - 二维码高度
|
|
10
|
+
* @property {boolean} [addText=true] - 是否在右侧添加文字
|
|
11
|
+
* @property {number} [top=10] - 顶部距离
|
|
12
|
+
* @property {number} [left=10] - 左侧距离
|
|
13
|
+
* @property {number} [lineHeight=1] - 文字行高,换行后下一行文字的y位置计算为fontSize * lineHeight
|
|
14
|
+
* @property {number} [fontSize=60] - 文字大小
|
|
15
|
+
* @property {string} [fontFamily='黑体'] - 字体类型
|
|
16
|
+
* @property {string} [bgColor='#fff'] - 背景颜色
|
|
17
|
+
* @property {number} [textLeftMargin=20] - 文字距离二维码间隔
|
|
18
|
+
* @property {boolean} [fontWeight=700] - 字体加粗
|
|
19
|
+
* @property {string} [textColor='#000'] - 字体颜色
|
|
20
|
+
* @property {string} [textContent] - 右侧文字内容
|
|
21
|
+
*/
|
|
22
|
+
declare module "public-utils" {
|
|
23
|
+
export interface QrCodeOptions {
|
|
24
|
+
/** 二维码类型,具体支持请查看文档 */
|
|
25
|
+
bcid?: string; // 默认值 'qrcode'
|
|
26
|
+
|
|
27
|
+
/** 最终图片宽度 */
|
|
28
|
+
width?: number; // 默认值 1056
|
|
29
|
+
|
|
30
|
+
/** 最终图片高度 */
|
|
31
|
+
height?: number; // 默认值 342
|
|
32
|
+
|
|
33
|
+
/** 二维码宽度 */
|
|
34
|
+
qrcodeW?: number; // 默认值 300
|
|
35
|
+
|
|
36
|
+
/** 二维码高度 */
|
|
37
|
+
qrcodeH?: number; // 默认值 300
|
|
38
|
+
|
|
39
|
+
/** 是否在右侧添加文字 */
|
|
40
|
+
addText?: boolean; // 默认值 true
|
|
41
|
+
|
|
42
|
+
/** 顶部距离 */
|
|
43
|
+
top?: number; // 默认值 10
|
|
44
|
+
|
|
45
|
+
/** 左侧距离 */
|
|
46
|
+
left?: number; // 默认值 10
|
|
47
|
+
|
|
48
|
+
/** 文字行高,换行后下一行文字的 y 位置计算为 fontSize * lineHeight */
|
|
49
|
+
lineHeight?: number; // 默认值 1
|
|
50
|
+
|
|
51
|
+
/** 文字大小 */
|
|
52
|
+
fontSize?: number; // 默认值 60
|
|
53
|
+
|
|
54
|
+
/** 字体类型 */
|
|
55
|
+
fontFamily?: string; // 默认值 '黑体'
|
|
56
|
+
|
|
57
|
+
/** 背景颜色 */
|
|
58
|
+
bgColor?: string; // 默认值 '#fff'
|
|
59
|
+
|
|
60
|
+
/** 文字距离二维码间隔 */
|
|
61
|
+
textLeftMargin?: number; // 默认值 20
|
|
62
|
+
|
|
63
|
+
/** 字体加粗 */
|
|
64
|
+
fontWeight?: boolean; // 默认值 700
|
|
65
|
+
|
|
66
|
+
/** 字体颜色 */
|
|
67
|
+
textColor?: string; // 默认值 '#000'
|
|
68
|
+
|
|
69
|
+
/** 右侧文字内容 */
|
|
70
|
+
textContent?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export module vtk {
|
|
74
|
+
/**
|
|
75
|
+
* 计算屏幕坐标到三维坐标
|
|
76
|
+
* @param {vtkRenderer} renderer vtkRenderer
|
|
77
|
+
* @param {number} x display x position
|
|
78
|
+
* @param {number} y display y position
|
|
79
|
+
* @param {number} z display z position
|
|
80
|
+
* @returns {[number, number, number]} 三维坐标
|
|
81
|
+
*/
|
|
82
|
+
export function computeDisplayToWorld(
|
|
83
|
+
renderer: vtkRenderer,
|
|
84
|
+
x: number,
|
|
85
|
+
y: number,
|
|
86
|
+
z: number
|
|
87
|
+
): [number, number, number];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 计算三维坐标到屏幕坐标
|
|
91
|
+
* @param {vtkRenderer} renderer vtkRenderer
|
|
92
|
+
* @param {number} x 三维坐标 x值
|
|
93
|
+
* @param {number} y 三维坐标 y值
|
|
94
|
+
* @param {number} z 三维坐标 z值
|
|
95
|
+
* @returns {[number, number, number]} 屏幕坐标
|
|
96
|
+
*/
|
|
97
|
+
export function computeWorldToDisplay(
|
|
98
|
+
renderer: vtkRenderer,
|
|
99
|
+
x: number,
|
|
100
|
+
y: number,
|
|
101
|
+
z: number
|
|
102
|
+
): [number, number, number];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export module window {
|
|
106
|
+
/**
|
|
107
|
+
* 获取当前屏幕分辨率(百分制,默认100)
|
|
108
|
+
* @returns {number} 当前屏幕分辨率
|
|
109
|
+
*/
|
|
110
|
+
export function getRatio(): number;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 获取当前屏幕分辨率(百分制,默认100)
|
|
114
|
+
* @returns {number} 当前屏幕分辨率
|
|
115
|
+
*/
|
|
116
|
+
export function detectZoom(): number;
|
|
117
|
+
}
|
|
118
|
+
export module qrcode {
|
|
119
|
+
/**
|
|
120
|
+
* 生成一个二维码图片,可以在二维码右侧添加二维码文字内容信息
|
|
121
|
+
* @param {string} content 二维码内容
|
|
122
|
+
* @param {QrCodeOptions} options 图片参数
|
|
123
|
+
* @returns {string} 返回图片的base64
|
|
124
|
+
*/
|
|
125
|
+
export function generateQRCodeWithText(
|
|
126
|
+
content: string,
|
|
127
|
+
options?: QrCodeOptions
|
|
128
|
+
): string | false;
|
|
129
|
+
}
|
|
130
|
+
}
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xl-public-utils",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "一些通用的方法",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"@kitware/vtk.js": "^25.7.2",
|
|
10
|
+
"bwip-js": "4.3.1"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./index.js",
|
|
14
|
+
"./qrcode": "./src/qrcode.js",
|
|
15
|
+
"./vtk": "./src/vtk.js",
|
|
16
|
+
"./window": "./src/window.js"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"一些通用方法"
|
|
24
|
+
],
|
|
25
|
+
"author": "xl",
|
|
26
|
+
"license": "ISC"
|
|
27
|
+
}
|
package/src/qrcode.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import bwipjs from 'bwip-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} QrCodeOptions
|
|
5
|
+
* @property {string} [bcid='qrcode'] - 二维码类型,具体支持请查看文档
|
|
6
|
+
* @property {number} [width=1056] - 最终图片宽度
|
|
7
|
+
* @property {number} [height=342] - 最终图片高度
|
|
8
|
+
* @property {number} [qrcodeW=300] - 二维码宽度
|
|
9
|
+
* @property {number} [qrcodeH=300] - 二维码高度
|
|
10
|
+
* @property {boolean} [addText=true] - 是否在右侧添加文字
|
|
11
|
+
* @property {number} [top=10] - 顶部距离
|
|
12
|
+
* @property {number} [left=10] - 左侧距离
|
|
13
|
+
* @property {number} [lineHeight=1] - 文字行高,换行后下一行文字的y位置计算为fontSize * lineHeight
|
|
14
|
+
* @property {number} [fontSize=60] - 文字大小
|
|
15
|
+
* @property {string} [fontFamily='黑体'] - 字体类型
|
|
16
|
+
* @property {string} [bgColor='#fff'] - 背景颜色
|
|
17
|
+
* @property {number} [textLeftMargin=20] - 文字距离二维码间隔
|
|
18
|
+
* @property {boolean} [fontWeight=700] - 字体加粗
|
|
19
|
+
* @property {string} [textColor='#000'] - 字体颜色
|
|
20
|
+
* @property {string} [textContent] - 右侧文字内容
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 生成一个二维码图片,可以在二维码右侧添加二维码文字内容信息
|
|
25
|
+
* @param {string} content 二维码内容
|
|
26
|
+
* @param {QrCodeOptions} options 图片参数
|
|
27
|
+
* @returns {string | false} 返回图片的base64
|
|
28
|
+
*/
|
|
29
|
+
export function generateQRCodeWithText(content, options = {}) {
|
|
30
|
+
const {
|
|
31
|
+
width = 1056,
|
|
32
|
+
height = 342,
|
|
33
|
+
bcid = 'qrcode',
|
|
34
|
+
qrcodeW = 300,
|
|
35
|
+
qrcodeH = 300,
|
|
36
|
+
addText = true,
|
|
37
|
+
top = 10,
|
|
38
|
+
left = 10,
|
|
39
|
+
lineHeight = 1,
|
|
40
|
+
fontSize = 60,
|
|
41
|
+
fontFamily = '黑体',
|
|
42
|
+
bgColor = '#fff',
|
|
43
|
+
textLeftMargin = 20,
|
|
44
|
+
fontWeight = 700,
|
|
45
|
+
textColor = '#000',
|
|
46
|
+
textContent
|
|
47
|
+
} = options
|
|
48
|
+
// Create the canvas and set its size
|
|
49
|
+
const canvas = document.createElement('canvas');
|
|
50
|
+
canvas.width = width;
|
|
51
|
+
canvas.height = height;
|
|
52
|
+
const ctx = canvas.getContext('2d');
|
|
53
|
+
|
|
54
|
+
// Clear the canvas before drawing
|
|
55
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
56
|
+
ctx.fillStyle = bgColor;
|
|
57
|
+
ctx.fillRect(0, 0, width, height);
|
|
58
|
+
// Create a temporary canvas to draw the QR code
|
|
59
|
+
const tempCanvas = document.createElement('canvas');
|
|
60
|
+
tempCanvas.width = qrcodeW;
|
|
61
|
+
tempCanvas.height = qrcodeH;
|
|
62
|
+
try {
|
|
63
|
+
bwipjs.toCanvas(tempCanvas, {
|
|
64
|
+
bcid: bcid, // Barcode type
|
|
65
|
+
text: content, // QR code content
|
|
66
|
+
scale: 1,
|
|
67
|
+
width: qrcodeW, // QR code width
|
|
68
|
+
height: qrcodeH, // QR code height
|
|
69
|
+
includetext: false, // Do not include text below QR code
|
|
70
|
+
});
|
|
71
|
+
// Draw the QR code on the left (300x300), centered vertically at (0, 21)
|
|
72
|
+
ctx.drawImage(tempCanvas, left, top, qrcodeW, qrcodeH);
|
|
73
|
+
if (addText) {
|
|
74
|
+
const _lineHeight = lineHeight * fontSize;
|
|
75
|
+
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
76
|
+
ctx.fillStyle = textColor;
|
|
77
|
+
let arrText = textContent ? textContent.split('') : content.split('');
|
|
78
|
+
let line = '';
|
|
79
|
+
let x = qrcodeW + textLeftMargin;
|
|
80
|
+
let y = fontSize;
|
|
81
|
+
for (let n = 0; n < arrText.length; n++) {
|
|
82
|
+
let testLine = line + arrText[n];
|
|
83
|
+
let metrics = ctx.measureText(testLine);
|
|
84
|
+
let testWidth = metrics.width;
|
|
85
|
+
if (testWidth > (width - x) && n > 0) {
|
|
86
|
+
ctx.fillText(line, x, y);
|
|
87
|
+
line = arrText[n];
|
|
88
|
+
y += _lineHeight;
|
|
89
|
+
} else {
|
|
90
|
+
line = testLine;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
ctx.fillText(line, x, y);
|
|
94
|
+
}
|
|
95
|
+
return canvas.toDataURL('image/png')
|
|
96
|
+
} catch (e) {
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
}
|
package/src/vtk.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 计算屏幕坐标到三维坐标
|
|
6
|
+
* @param {vtkRenderer} renderer vtkRenderer
|
|
7
|
+
* @param {number} x display x position
|
|
8
|
+
* @param {number} y display y position
|
|
9
|
+
* @param {number} z display z position
|
|
10
|
+
* @returns {[number, number, number]} 三维坐标
|
|
11
|
+
*/
|
|
12
|
+
export function computeDisplayToWorld(renderer, x, y, z) {
|
|
13
|
+
const view = renderer.getRenderWindow()?.getViews()[0];
|
|
14
|
+
return view.displayToWorld(x, y, z, renderer);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 计算三维坐标到屏幕坐标
|
|
19
|
+
* @param {vtkRenderer} renderer vtkRenderer
|
|
20
|
+
* @param {number} x 三维坐标 x值
|
|
21
|
+
* @param {number} y 三维坐标 y值
|
|
22
|
+
* @param {number} z 三维坐标 z值
|
|
23
|
+
* @returns {[number, number, number]} 屏幕坐标
|
|
24
|
+
*/
|
|
25
|
+
export function computeWorldToDisplay(renderer, x, y, z) {
|
|
26
|
+
const view = renderer.getRenderWindow().getViews()[0];
|
|
27
|
+
return view.worldToDisplay(x, y, z, renderer);
|
|
28
|
+
}
|
package/src/window.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取当前屏幕分辨率(百分制,默认100)
|
|
3
|
+
* @returns {number} 当前屏幕分辨率
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function getRatio() {
|
|
7
|
+
let ratio = 0;
|
|
8
|
+
const screen = window.screen;
|
|
9
|
+
const ua = navigator.userAgent.toLowerCase();
|
|
10
|
+
if (window.devicePixelRatio !== undefined) {
|
|
11
|
+
ratio = window.devicePixelRatio;
|
|
12
|
+
} else if (~ua.indexOf('msie')) {
|
|
13
|
+
if (screen.deviceXDPI && screen.logicalXDPI) {
|
|
14
|
+
ratio = screen.deviceXDPI / screen.logicalXDPI;
|
|
15
|
+
}
|
|
16
|
+
} else if (
|
|
17
|
+
window.outerWidth !== undefined &&
|
|
18
|
+
window.innerWidth !== undefined
|
|
19
|
+
) {
|
|
20
|
+
ratio = window.outerWidth / window.innerWidth;
|
|
21
|
+
}
|
|
22
|
+
if (ratio) {
|
|
23
|
+
ratio = Math.round(ratio * 100);
|
|
24
|
+
}
|
|
25
|
+
return ratio;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Alias for {@link getRatio}
|
|
30
|
+
* @function
|
|
31
|
+
*/
|
|
32
|
+
export const detectZoom = getRatio;
|