jmgraph 3.2.16 → 3.2.18
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/LICENSE +21 -21
- package/README.md +251 -428
- package/build/gulpfile.js +142 -142
- package/build/package-lock.json +10666 -0
- package/build/package.json +71 -71
- package/dev.js +9 -9
- package/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +3500 -2668
- package/dist/jmgraph.min.js +1 -1
- package/example/ball.html +216 -216
- package/example/base.html +111 -111
- package/example/canvas.html +53 -53
- package/example/cell.html +283 -283
- package/example/controls/arc.html +128 -128
- package/example/controls/arrowline.html +77 -77
- package/example/controls/bezier.html +298 -298
- package/example/controls/img.html +96 -96
- package/example/controls/label.html +86 -86
- package/example/controls/line.html +172 -172
- package/example/controls/prismatic.html +62 -62
- package/example/controls/rect.html +63 -63
- package/example/controls/resize.html +111 -111
- package/example/controls/test.html +359 -359
- package/example/es.html +69 -69
- package/example/es5module.html +62 -63
- package/example/heartarc.html +115 -115
- package/example/index.html +46 -46
- package/example/js/require.js +4 -4
- package/example/love/img/bling/bling.tps +265 -265
- package/example/love/img/bling.json +87 -87
- package/example/love/img/bling.tps +295 -295
- package/example/love/img/love.json +95 -95
- package/example/love/img/love.tps +315 -315
- package/example/love/img/qq/qq.tps +399 -399
- package/example/love/img/qq.json +242 -242
- package/example/love/index.html +40 -40
- package/example/love/js/game.js +558 -558
- package/example/music.html +210 -210
- package/example/node/test.js +137 -137
- package/example/pdf.html +186 -186
- package/example/progress.html +172 -172
- package/example/pso.html +147 -147
- package/example/sort.html +804 -815
- package/example/tweenjs.html +83 -83
- package/example/webgl.html +278 -278
- package/example/xfj/index.html +331 -331
- package/example/xfj/shake.js +48 -48
- package/example/xfj/testori.html +75 -75
- package/index.js +99 -99
- package/package.json +58 -56
- package/src/core/jmControl.js +1376 -1531
- package/src/core/jmEvents.js +240 -281
- package/src/core/jmGradient.js +231 -231
- package/src/core/jmGraph.js +569 -569
- package/src/core/jmList.js +92 -157
- package/src/core/jmObject.js +83 -103
- package/src/core/jmPath.js +35 -35
- package/src/core/jmProperty.js +71 -110
- package/src/core/jmShadow.js +65 -65
- package/src/core/jmUtils.js +906 -919
- package/src/lib/earcut.js +680 -680
- package/src/lib/earcut.md +73 -73
- package/src/lib/webgl/base.js +522 -452
- package/src/lib/webgl/core/buffer.js +48 -48
- package/src/lib/webgl/core/mapSize.js +40 -40
- package/src/lib/webgl/core/mapType.js +43 -43
- package/src/lib/webgl/core/program.js +138 -138
- package/src/lib/webgl/core/shader.js +13 -13
- package/src/lib/webgl/core/texture.js +60 -60
- package/src/lib/webgl/gradient.js +168 -168
- package/src/lib/webgl/index.js +137 -11
- package/src/lib/webgl/path.js +568 -561
- package/src/shapes/jmArrowLine.js +36 -36
- package/src/shapes/jmImage.js +244 -244
- package/src/shapes/jmLabel.js +271 -271
- package/src/shapes/jmResize.js +332 -330
package/example/node/test.js
CHANGED
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const jmGraph = require("../../dist/jmgraph.js");
|
|
3
|
-
const { createCanvas, Image, registerFont } = require('canvas');
|
|
4
|
-
|
|
5
|
-
registerFont('msyh.ttf', { family: '微软雅黑' });
|
|
6
|
-
|
|
7
|
-
const mycanvas = createCanvas(800, 600);
|
|
8
|
-
console.log(mycanvas.type);
|
|
9
|
-
const g = jmGraph.create(mycanvas, {
|
|
10
|
-
width: 800,
|
|
11
|
-
height: 600,
|
|
12
|
-
mode: '2d',
|
|
13
|
-
style: {
|
|
14
|
-
fill: '#000'
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
init(g);
|
|
18
|
-
|
|
19
|
-
const img = new Image();
|
|
20
|
-
img.onload = () => {
|
|
21
|
-
//创建一个image
|
|
22
|
-
var imgControl = g.createShape('image',{
|
|
23
|
-
style: {
|
|
24
|
-
shadow: '0,0,10,#fff'
|
|
25
|
-
},
|
|
26
|
-
position: {x:300, y:300},
|
|
27
|
-
image: img
|
|
28
|
-
});
|
|
29
|
-
g.children.add(imgControl);
|
|
30
|
-
|
|
31
|
-
saveToImage();
|
|
32
|
-
}
|
|
33
|
-
img.onerror = err => { throw err }
|
|
34
|
-
img.src = 'https://mat1.gtimg.com/www/qq2018/imgs/qq_logo_2018x2.png'
|
|
35
|
-
|
|
36
|
-
// 导出为图片
|
|
37
|
-
function saveToImage() {
|
|
38
|
-
g.redraw();
|
|
39
|
-
const out = fs.createWriteStream(__dirname + '/test.png');
|
|
40
|
-
const stream = mycanvas.createPNGStream();
|
|
41
|
-
stream.pipe(out);
|
|
42
|
-
out.on('finish', () => console.log('The PNG file was created.'));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function init(g){
|
|
46
|
-
g.style.fill = '#000'; //画布背景
|
|
47
|
-
var style = {
|
|
48
|
-
stroke:'#46BF86',
|
|
49
|
-
fill: '#556662',
|
|
50
|
-
lineWidth: 1.5
|
|
51
|
-
};
|
|
52
|
-
style.shadow = '0,0,10,#fff';
|
|
53
|
-
style.opacity = 0.2;
|
|
54
|
-
//style.lineCap = 'round';
|
|
55
|
-
|
|
56
|
-
//创建一个方块
|
|
57
|
-
|
|
58
|
-
var rect = g.createShape('rect',{
|
|
59
|
-
style:style,
|
|
60
|
-
position: {x:300,y:100},
|
|
61
|
-
width:100,
|
|
62
|
-
height:100
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
var rect2 = g.createShape('rect',{
|
|
66
|
-
style:style,
|
|
67
|
-
position: {x:'50%',y:'50%'},
|
|
68
|
-
width:50,
|
|
69
|
-
height:50
|
|
70
|
-
});
|
|
71
|
-
rect.children.add(rect2);
|
|
72
|
-
|
|
73
|
-
g.children.add(rect);
|
|
74
|
-
rect.canMove(true);
|
|
75
|
-
|
|
76
|
-
// 画二个五角星
|
|
77
|
-
var coordinates = [
|
|
78
|
-
{x:50,y:100},
|
|
79
|
-
{x:250,y:100},
|
|
80
|
-
{x:250,y:50},
|
|
81
|
-
{x:300,y:200},
|
|
82
|
-
{x:200,y:200},
|
|
83
|
-
{x:50,y:300},
|
|
84
|
-
];
|
|
85
|
-
style = g.utils.clone(style);
|
|
86
|
-
var star1 = g.createShape('path',{
|
|
87
|
-
style:style,
|
|
88
|
-
points: coordinates
|
|
89
|
-
});
|
|
90
|
-
g.children.add(star1);
|
|
91
|
-
star1.canMove(true);
|
|
92
|
-
|
|
93
|
-
var coordinates2 = [
|
|
94
|
-
{x:50,y:300},
|
|
95
|
-
{x:250,y:300},
|
|
96
|
-
{x:100,y:350},
|
|
97
|
-
{x:150,y:250},
|
|
98
|
-
{x:200,y:350},
|
|
99
|
-
{x:50,y:300},
|
|
100
|
-
];
|
|
101
|
-
style = g.utils.clone(style);
|
|
102
|
-
delete style.fill;
|
|
103
|
-
var star2 = g.createShape('path',{
|
|
104
|
-
style:style,
|
|
105
|
-
points: coordinates2
|
|
106
|
-
});
|
|
107
|
-
g.children.add(star2);
|
|
108
|
-
|
|
109
|
-
style = {
|
|
110
|
-
stroke: '#effaaa',
|
|
111
|
-
fill: '#fff',
|
|
112
|
-
textAlign: 'right',
|
|
113
|
-
textBaseline: 'middle',
|
|
114
|
-
font: '24px "微软雅黑"', // 需要加载字体
|
|
115
|
-
border: {
|
|
116
|
-
left:1,
|
|
117
|
-
top:1,
|
|
118
|
-
right:1,
|
|
119
|
-
bottom:1,
|
|
120
|
-
style: {
|
|
121
|
-
stroke: 'red'
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
shadow: '0,0,10,#fff'
|
|
125
|
-
};
|
|
126
|
-
//style.opacity = 0.2;
|
|
127
|
-
|
|
128
|
-
//创建一个label
|
|
129
|
-
var label = g.createShape('label',{
|
|
130
|
-
style:style,
|
|
131
|
-
//position:{x:200,y:150},
|
|
132
|
-
center: {x: 250, y: 250},
|
|
133
|
-
text:'test label 中文',
|
|
134
|
-
//width:120,
|
|
135
|
-
height:80
|
|
136
|
-
});
|
|
137
|
-
g.children.add(label);
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const jmGraph = require("../../dist/jmgraph.js");
|
|
3
|
+
const { createCanvas, Image, registerFont } = require('canvas');
|
|
4
|
+
|
|
5
|
+
registerFont('msyh.ttf', { family: '微软雅黑' });
|
|
6
|
+
|
|
7
|
+
const mycanvas = createCanvas(800, 600);
|
|
8
|
+
console.log(mycanvas.type);
|
|
9
|
+
const g = jmGraph.create(mycanvas, {
|
|
10
|
+
width: 800,
|
|
11
|
+
height: 600,
|
|
12
|
+
mode: '2d',
|
|
13
|
+
style: {
|
|
14
|
+
fill: '#000'
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
init(g);
|
|
18
|
+
|
|
19
|
+
const img = new Image();
|
|
20
|
+
img.onload = () => {
|
|
21
|
+
//创建一个image
|
|
22
|
+
var imgControl = g.createShape('image',{
|
|
23
|
+
style: {
|
|
24
|
+
shadow: '0,0,10,#fff'
|
|
25
|
+
},
|
|
26
|
+
position: {x:300, y:300},
|
|
27
|
+
image: img
|
|
28
|
+
});
|
|
29
|
+
g.children.add(imgControl);
|
|
30
|
+
|
|
31
|
+
saveToImage();
|
|
32
|
+
}
|
|
33
|
+
img.onerror = err => { throw err }
|
|
34
|
+
img.src = 'https://mat1.gtimg.com/www/qq2018/imgs/qq_logo_2018x2.png'
|
|
35
|
+
|
|
36
|
+
// 导出为图片
|
|
37
|
+
function saveToImage() {
|
|
38
|
+
g.redraw();
|
|
39
|
+
const out = fs.createWriteStream(__dirname + '/test.png');
|
|
40
|
+
const stream = mycanvas.createPNGStream();
|
|
41
|
+
stream.pipe(out);
|
|
42
|
+
out.on('finish', () => console.log('The PNG file was created.'));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function init(g){
|
|
46
|
+
g.style.fill = '#000'; //画布背景
|
|
47
|
+
var style = {
|
|
48
|
+
stroke:'#46BF86',
|
|
49
|
+
fill: '#556662',
|
|
50
|
+
lineWidth: 1.5
|
|
51
|
+
};
|
|
52
|
+
style.shadow = '0,0,10,#fff';
|
|
53
|
+
style.opacity = 0.2;
|
|
54
|
+
//style.lineCap = 'round';
|
|
55
|
+
|
|
56
|
+
//创建一个方块
|
|
57
|
+
|
|
58
|
+
var rect = g.createShape('rect',{
|
|
59
|
+
style:style,
|
|
60
|
+
position: {x:300,y:100},
|
|
61
|
+
width:100,
|
|
62
|
+
height:100
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
var rect2 = g.createShape('rect',{
|
|
66
|
+
style:style,
|
|
67
|
+
position: {x:'50%',y:'50%'},
|
|
68
|
+
width:50,
|
|
69
|
+
height:50
|
|
70
|
+
});
|
|
71
|
+
rect.children.add(rect2);
|
|
72
|
+
|
|
73
|
+
g.children.add(rect);
|
|
74
|
+
rect.canMove(true);
|
|
75
|
+
|
|
76
|
+
// 画二个五角星
|
|
77
|
+
var coordinates = [
|
|
78
|
+
{x:50,y:100},
|
|
79
|
+
{x:250,y:100},
|
|
80
|
+
{x:250,y:50},
|
|
81
|
+
{x:300,y:200},
|
|
82
|
+
{x:200,y:200},
|
|
83
|
+
{x:50,y:300},
|
|
84
|
+
];
|
|
85
|
+
style = g.utils.clone(style);
|
|
86
|
+
var star1 = g.createShape('path',{
|
|
87
|
+
style:style,
|
|
88
|
+
points: coordinates
|
|
89
|
+
});
|
|
90
|
+
g.children.add(star1);
|
|
91
|
+
star1.canMove(true);
|
|
92
|
+
|
|
93
|
+
var coordinates2 = [
|
|
94
|
+
{x:50,y:300},
|
|
95
|
+
{x:250,y:300},
|
|
96
|
+
{x:100,y:350},
|
|
97
|
+
{x:150,y:250},
|
|
98
|
+
{x:200,y:350},
|
|
99
|
+
{x:50,y:300},
|
|
100
|
+
];
|
|
101
|
+
style = g.utils.clone(style);
|
|
102
|
+
delete style.fill;
|
|
103
|
+
var star2 = g.createShape('path',{
|
|
104
|
+
style:style,
|
|
105
|
+
points: coordinates2
|
|
106
|
+
});
|
|
107
|
+
g.children.add(star2);
|
|
108
|
+
|
|
109
|
+
style = {
|
|
110
|
+
stroke: '#effaaa',
|
|
111
|
+
fill: '#fff',
|
|
112
|
+
textAlign: 'right',
|
|
113
|
+
textBaseline: 'middle',
|
|
114
|
+
font: '24px "微软雅黑"', // 需要加载字体
|
|
115
|
+
border: {
|
|
116
|
+
left:1,
|
|
117
|
+
top:1,
|
|
118
|
+
right:1,
|
|
119
|
+
bottom:1,
|
|
120
|
+
style: {
|
|
121
|
+
stroke: 'red'
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
shadow: '0,0,10,#fff'
|
|
125
|
+
};
|
|
126
|
+
//style.opacity = 0.2;
|
|
127
|
+
|
|
128
|
+
//创建一个label
|
|
129
|
+
var label = g.createShape('label',{
|
|
130
|
+
style:style,
|
|
131
|
+
//position:{x:200,y:150},
|
|
132
|
+
center: {x: 250, y: 250},
|
|
133
|
+
text:'test label 中文',
|
|
134
|
+
//width:120,
|
|
135
|
+
height:80
|
|
136
|
+
});
|
|
137
|
+
g.children.add(label);
|
|
138
138
|
}
|
package/example/pdf.html
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
|
|
5
|
-
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
6
|
-
<style>
|
|
7
|
-
html,body{
|
|
8
|
-
margin:0;
|
|
9
|
-
padding: 0;
|
|
10
|
-
}
|
|
11
|
-
#mycanvas_container{
|
|
12
|
-
overflow: hidden;
|
|
13
|
-
}
|
|
14
|
-
</style>
|
|
15
|
-
|
|
16
|
-
<script type="text/javascript" src="//jtcospublic.ciccten.com/public/pdfjs/pdf.min.js"></script>
|
|
17
|
-
</head>
|
|
18
|
-
<body>
|
|
19
|
-
<div id="mycanvas_container"></div>
|
|
20
|
-
</body>
|
|
21
|
-
<script type="module">
|
|
22
|
-
import jmGraph from "../index.js";
|
|
23
|
-
|
|
24
|
-
var pdfDocument, pdfImage;
|
|
25
|
-
|
|
26
|
-
var container = document.getElementById('mycanvas_container');
|
|
27
|
-
|
|
28
|
-
init();
|
|
29
|
-
|
|
30
|
-
function init(g){
|
|
31
|
-
|
|
32
|
-
// 初始化pdf组件 这里的worker请下到你站点内,不能跨域
|
|
33
|
-
pdfjsLib.GlobalWorkerOptions.workerSrc = '//jtcospublic.ciccten.com/public/pdfjs/pdf.worker.min.js';//'//qian-img.tenpay.com/static/pdfjs/pdf.worker.min.js';
|
|
34
|
-
loadPdf('//qian-img.tenpay.com/resources/vtools/img/201906/20ff3e6197e118dcb82fac4cf5a5feeb.pdf');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 加载PDF
|
|
38
|
-
async function loadPdf(url) {
|
|
39
|
-
const pdf = await pdfjsLib.getDocument(url).promise;
|
|
40
|
-
|
|
41
|
-
if (pdf.numPages > 0) {
|
|
42
|
-
for (var i = 0; i < pdf.numPages; i++) {
|
|
43
|
-
await renderPage(pdf, i);// 默认打开第一页
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
// 打开当前PDF指定页
|
|
48
|
-
async function renderPage(pdf, page) {
|
|
49
|
-
const canvas = document.createElement('canvas');
|
|
50
|
-
canvas.style.width = '100%';
|
|
51
|
-
container.appendChild(canvas);
|
|
52
|
-
|
|
53
|
-
const pageInstance = await pdf.getPage(page + 1);
|
|
54
|
-
|
|
55
|
-
const scale = 1;
|
|
56
|
-
const viewport = pageInstance.getViewport({
|
|
57
|
-
scale
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const context = canvas.getContext('2d');
|
|
61
|
-
|
|
62
|
-
canvas.height = viewport.height || viewport.viewBox[3]; /* viewport.height is NaN */
|
|
63
|
-
|
|
64
|
-
canvas.width = viewport.width || viewport.viewBox[2]; /* viewport.width is also NaN */
|
|
65
|
-
|
|
66
|
-
//
|
|
67
|
-
// Render PDF page into canvas context
|
|
68
|
-
//
|
|
69
|
-
const renderContext = {
|
|
70
|
-
canvasContext: context,
|
|
71
|
-
viewport: viewport
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
await pageInstance.render(renderContext).promise;
|
|
76
|
-
|
|
77
|
-
initGraph(canvas);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 初始化当前页,并改造支持放大缩小
|
|
81
|
-
function initGraph(canvas) {
|
|
82
|
-
const el = new Image();
|
|
83
|
-
|
|
84
|
-
el.onload = () => {
|
|
85
|
-
const width = canvas.clientWidth || canvas.offsetWidth;
|
|
86
|
-
const height = canvas.clientHeight || canvas.offsetHeight;
|
|
87
|
-
|
|
88
|
-
const g = new jmGraph(canvas, {
|
|
89
|
-
width,
|
|
90
|
-
height,
|
|
91
|
-
autoRefresh: true // 自动刷新
|
|
92
|
-
});
|
|
93
|
-
const img = g.createShape('image', {
|
|
94
|
-
//sourcePosition: {x: 0, y: 0},
|
|
95
|
-
//sourceWidth: width,
|
|
96
|
-
//sourceHeight: height,
|
|
97
|
-
position: {x: 0, y: 0},
|
|
98
|
-
width: '100%',
|
|
99
|
-
height: '100%',
|
|
100
|
-
image: el
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
//img.canMove(true);
|
|
104
|
-
|
|
105
|
-
// 初始化缩放属性
|
|
106
|
-
img.transform = {
|
|
107
|
-
scaleX: 1,
|
|
108
|
-
scaleY: 1,
|
|
109
|
-
skewX: 0,
|
|
110
|
-
skewY: 0,
|
|
111
|
-
offsetX: 0,
|
|
112
|
-
offsetY: 0
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
let prePosition;
|
|
116
|
-
g.on('touchstart', (e) => {
|
|
117
|
-
console.log('touchstart', e.position);
|
|
118
|
-
// 二个手指滑动
|
|
119
|
-
if(e.position?.touches?.length === 2) {
|
|
120
|
-
prePosition = e.position;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
g.on('touchmove', (e) => {
|
|
125
|
-
//console.log('touchmove', prePosition, e.position);
|
|
126
|
-
if(e.position?.touches?.length !== 2) return;
|
|
127
|
-
|
|
128
|
-
e.event?.stopPropagation();
|
|
129
|
-
e.event?.preventDefault();
|
|
130
|
-
|
|
131
|
-
// 没有上一个滑动,则这次就当上次
|
|
132
|
-
if(!prePosition) {
|
|
133
|
-
prePosition = e.position;
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
//上次滑动二指的距离
|
|
138
|
-
const preOffX = prePosition.touches[0].pageX - prePosition.touches[1].pageX;
|
|
139
|
-
const preOffY = prePosition.touches[0].pageY - prePosition.touches[1].pageY;
|
|
140
|
-
const preDis = Math.sqrt(preOffX * preOffX + preOffY * preOffY);
|
|
141
|
-
//当次滑动二指的距离
|
|
142
|
-
const curOffX = e.position.touches[0].pageX - e.position.touches[1].pageX;
|
|
143
|
-
const curOffY = e.position.touches[0].pageY - e.position.touches[1].pageY;
|
|
144
|
-
const curDis = Math.sqrt(curOffX * curOffX + curOffY * curOffY);
|
|
145
|
-
|
|
146
|
-
// 如果二指移动距离小于1,则认为是在移动图片,不缩放
|
|
147
|
-
if(Math.abs(preDis - curDis) < 0.05) {
|
|
148
|
-
img.offset(e.position.touches[0].pageX - prePosition.touches[0].pageX,
|
|
149
|
-
e.position.touches[0].pageY - prePosition.touches[0].pageY);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const disx = Math.abs(preOffX - curOffX) / width;//x轴滑行的距离
|
|
153
|
-
const disy = Math.abs(preOffY - curOffY) / height;//y轴滑行的距离
|
|
154
|
-
|
|
155
|
-
let offScale = Math.max(disx, disy);
|
|
156
|
-
|
|
157
|
-
console.log('offScale1', offScale);
|
|
158
|
-
|
|
159
|
-
offScale = img.transform.scaleX * (curDis > preDis? (1+offScale): (1-offScale));
|
|
160
|
-
|
|
161
|
-
console.log('offScale2', offScale);
|
|
162
|
-
|
|
163
|
-
img.transform.scaleX = img.transform.scaleY = offScale;
|
|
164
|
-
|
|
165
|
-
const imgBounds = img.getBounds();
|
|
166
|
-
|
|
167
|
-
img.transform.offsetX = (g.width - imgBounds.width * offScale) / 2;
|
|
168
|
-
img.transform.offsetY = (g.height - imgBounds.height * offScale) / 2;
|
|
169
|
-
|
|
170
|
-
console.log('transform', img.transform);
|
|
171
|
-
}
|
|
172
|
-
img.needUpdate = true;
|
|
173
|
-
prePosition = e.position;
|
|
174
|
-
|
|
175
|
-
return false;
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
g.on('touchend touchcancel touchleave', (e) => {
|
|
179
|
-
prePosition = null;
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
g.children.add(img);
|
|
183
|
-
};
|
|
184
|
-
el.src = canvas.toDataURL('image/png');
|
|
185
|
-
}
|
|
186
|
-
</script>
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
|
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
6
|
+
<style>
|
|
7
|
+
html,body{
|
|
8
|
+
margin:0;
|
|
9
|
+
padding: 0;
|
|
10
|
+
}
|
|
11
|
+
#mycanvas_container{
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
}
|
|
14
|
+
</style>
|
|
15
|
+
|
|
16
|
+
<script type="text/javascript" src="//jtcospublic.ciccten.com/public/pdfjs/pdf.min.js"></script>
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<div id="mycanvas_container"></div>
|
|
20
|
+
</body>
|
|
21
|
+
<script type="module">
|
|
22
|
+
import jmGraph from "../index.js";
|
|
23
|
+
|
|
24
|
+
var pdfDocument, pdfImage;
|
|
25
|
+
|
|
26
|
+
var container = document.getElementById('mycanvas_container');
|
|
27
|
+
|
|
28
|
+
init();
|
|
29
|
+
|
|
30
|
+
function init(g){
|
|
31
|
+
|
|
32
|
+
// 初始化pdf组件 这里的worker请下到你站点内,不能跨域
|
|
33
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = '//jtcospublic.ciccten.com/public/pdfjs/pdf.worker.min.js';//'//qian-img.tenpay.com/static/pdfjs/pdf.worker.min.js';
|
|
34
|
+
loadPdf('//qian-img.tenpay.com/resources/vtools/img/201906/20ff3e6197e118dcb82fac4cf5a5feeb.pdf');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 加载PDF
|
|
38
|
+
async function loadPdf(url) {
|
|
39
|
+
const pdf = await pdfjsLib.getDocument(url).promise;
|
|
40
|
+
|
|
41
|
+
if (pdf.numPages > 0) {
|
|
42
|
+
for (var i = 0; i < pdf.numPages; i++) {
|
|
43
|
+
await renderPage(pdf, i);// 默认打开第一页
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// 打开当前PDF指定页
|
|
48
|
+
async function renderPage(pdf, page) {
|
|
49
|
+
const canvas = document.createElement('canvas');
|
|
50
|
+
canvas.style.width = '100%';
|
|
51
|
+
container.appendChild(canvas);
|
|
52
|
+
|
|
53
|
+
const pageInstance = await pdf.getPage(page + 1);
|
|
54
|
+
|
|
55
|
+
const scale = 1;
|
|
56
|
+
const viewport = pageInstance.getViewport({
|
|
57
|
+
scale
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const context = canvas.getContext('2d');
|
|
61
|
+
|
|
62
|
+
canvas.height = viewport.height || viewport.viewBox[3]; /* viewport.height is NaN */
|
|
63
|
+
|
|
64
|
+
canvas.width = viewport.width || viewport.viewBox[2]; /* viewport.width is also NaN */
|
|
65
|
+
|
|
66
|
+
//
|
|
67
|
+
// Render PDF page into canvas context
|
|
68
|
+
//
|
|
69
|
+
const renderContext = {
|
|
70
|
+
canvasContext: context,
|
|
71
|
+
viewport: viewport
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
await pageInstance.render(renderContext).promise;
|
|
76
|
+
|
|
77
|
+
initGraph(canvas);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 初始化当前页,并改造支持放大缩小
|
|
81
|
+
function initGraph(canvas) {
|
|
82
|
+
const el = new Image();
|
|
83
|
+
|
|
84
|
+
el.onload = () => {
|
|
85
|
+
const width = canvas.clientWidth || canvas.offsetWidth;
|
|
86
|
+
const height = canvas.clientHeight || canvas.offsetHeight;
|
|
87
|
+
|
|
88
|
+
const g = new jmGraph(canvas, {
|
|
89
|
+
width,
|
|
90
|
+
height,
|
|
91
|
+
autoRefresh: true // 自动刷新
|
|
92
|
+
});
|
|
93
|
+
const img = g.createShape('image', {
|
|
94
|
+
//sourcePosition: {x: 0, y: 0},
|
|
95
|
+
//sourceWidth: width,
|
|
96
|
+
//sourceHeight: height,
|
|
97
|
+
position: {x: 0, y: 0},
|
|
98
|
+
width: '100%',
|
|
99
|
+
height: '100%',
|
|
100
|
+
image: el
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
//img.canMove(true);
|
|
104
|
+
|
|
105
|
+
// 初始化缩放属性
|
|
106
|
+
img.transform = {
|
|
107
|
+
scaleX: 1,
|
|
108
|
+
scaleY: 1,
|
|
109
|
+
skewX: 0,
|
|
110
|
+
skewY: 0,
|
|
111
|
+
offsetX: 0,
|
|
112
|
+
offsetY: 0
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
let prePosition;
|
|
116
|
+
g.on('touchstart', (e) => {
|
|
117
|
+
console.log('touchstart', e.position);
|
|
118
|
+
// 二个手指滑动
|
|
119
|
+
if(e.position?.touches?.length === 2) {
|
|
120
|
+
prePosition = e.position;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
g.on('touchmove', (e) => {
|
|
125
|
+
//console.log('touchmove', prePosition, e.position);
|
|
126
|
+
if(e.position?.touches?.length !== 2) return;
|
|
127
|
+
|
|
128
|
+
e.event?.stopPropagation();
|
|
129
|
+
e.event?.preventDefault();
|
|
130
|
+
|
|
131
|
+
// 没有上一个滑动,则这次就当上次
|
|
132
|
+
if(!prePosition) {
|
|
133
|
+
prePosition = e.position;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//上次滑动二指的距离
|
|
138
|
+
const preOffX = prePosition.touches[0].pageX - prePosition.touches[1].pageX;
|
|
139
|
+
const preOffY = prePosition.touches[0].pageY - prePosition.touches[1].pageY;
|
|
140
|
+
const preDis = Math.sqrt(preOffX * preOffX + preOffY * preOffY);
|
|
141
|
+
//当次滑动二指的距离
|
|
142
|
+
const curOffX = e.position.touches[0].pageX - e.position.touches[1].pageX;
|
|
143
|
+
const curOffY = e.position.touches[0].pageY - e.position.touches[1].pageY;
|
|
144
|
+
const curDis = Math.sqrt(curOffX * curOffX + curOffY * curOffY);
|
|
145
|
+
|
|
146
|
+
// 如果二指移动距离小于1,则认为是在移动图片,不缩放
|
|
147
|
+
if(Math.abs(preDis - curDis) < 0.05) {
|
|
148
|
+
img.offset(e.position.touches[0].pageX - prePosition.touches[0].pageX,
|
|
149
|
+
e.position.touches[0].pageY - prePosition.touches[0].pageY);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const disx = Math.abs(preOffX - curOffX) / width;//x轴滑行的距离
|
|
153
|
+
const disy = Math.abs(preOffY - curOffY) / height;//y轴滑行的距离
|
|
154
|
+
|
|
155
|
+
let offScale = Math.max(disx, disy);
|
|
156
|
+
|
|
157
|
+
console.log('offScale1', offScale);
|
|
158
|
+
|
|
159
|
+
offScale = img.transform.scaleX * (curDis > preDis? (1+offScale): (1-offScale));
|
|
160
|
+
|
|
161
|
+
console.log('offScale2', offScale);
|
|
162
|
+
|
|
163
|
+
img.transform.scaleX = img.transform.scaleY = offScale;
|
|
164
|
+
|
|
165
|
+
const imgBounds = img.getBounds();
|
|
166
|
+
|
|
167
|
+
img.transform.offsetX = (g.width - imgBounds.width * offScale) / 2;
|
|
168
|
+
img.transform.offsetY = (g.height - imgBounds.height * offScale) / 2;
|
|
169
|
+
|
|
170
|
+
console.log('transform', img.transform);
|
|
171
|
+
}
|
|
172
|
+
img.needUpdate = true;
|
|
173
|
+
prePosition = e.position;
|
|
174
|
+
|
|
175
|
+
return false;
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
g.on('touchend touchcancel touchleave', (e) => {
|
|
179
|
+
prePosition = null;
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
g.children.add(img);
|
|
183
|
+
};
|
|
184
|
+
el.src = canvas.toDataURL('image/png');
|
|
185
|
+
}
|
|
186
|
+
</script>
|
|
187
187
|
</html>
|