-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvasDraw.js
125 lines (124 loc) · 4.1 KB
/
canvasDraw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* CanvasDraw 在canvas上画图片,并一直居中。
*/
class CanvasDraw {
/**
* @param {Object} ctx canvas的context 对象
* @param {[type]} screenWidth canvas的宽度
* @param {[type]} screenHeight canvas的高度
* @return {void}
*/
constructor (ctx, screenWidth = 0, screenHeight = 0) {
this.sequenceCtx = ctx
this.screenWidth = screenWidth
this.screenHeight = screenHeight
}
/**
* 画的函数
* @param {Image} image new Image()对象
* @param {[type]} imgWidth 图片原始宽度
* @param {[type]} imgHeight 图片原始高度
* @return {void}
*/
handleDraw (image, imgWidth, imgHeight) {
if (this.screenWidth) {
this.screenWidth = window.innerWidth
}
if (this.screenHeight) {
this.screenHeight = window.innerHeight
}
if (this.screenWidth * 9 === this.screenHeight * 16) {
this.sequenceCtx.drawImage(image, 0, 0, this.screenWidth, this.screenHeight)
} else {
const dw = this.screenWidth / imgWidth
const dh = this.screenHeight / imgHeight
if (this.screenWidth <= imgWidth && this.screenHeight >= imgHeight) {
// console.log(0)
this.sequenceCtx.drawImage(
image,
(this.screenHeight * 16 / 9 - this.screenWidth) / 2 * imgHeight / this.screenHeight,
0,
imgWidth - ((this.screenHeight * 16 / 9 - this.screenWidth) * imgHeight / this.screenHeight),
imgHeight,
0,
0,
this.screenWidth,
this.screenHeight
)
}
if (this.screenWidth < imgWidth && this.screenHeight <= imgHeight && dw < dh) {
// console.log(1)
this.sequenceCtx.drawImage(
image,
(this.screenHeight * 16 / 9 - this.screenWidth) / 2 * imgHeight / this.screenHeight,
0,
// (this.screenHeight * 16 / 9 - this.screenWidth) / 2 + 40,
// 0,
imgWidth - ((this.screenHeight * 16 / 9 - this.screenWidth) * imgHeight / this.screenHeight),
imgHeight,
0,
0,
this.screenWidth,
this.screenHeight
)
}
if (this.screenWidth >= imgWidth && this.screenHeight <= imgHeight) {
// console.log(2)
this.sequenceCtx.drawImage(
image,
0,
(this.screenWidth * 9 / 16 - this.screenHeight) / 2 * imgWidth / this.screenWidth,
imgWidth,
imgHeight - ((this.screenWidth * 9 / 16 - this.screenHeight) * imgWidth / this.screenWidth),
0,
0,
this.screenWidth,
this.screenHeight
)
}
if (this.screenWidth < imgWidth && this.screenHeight < imgHeight && dw > dh) {
// console.log(3)
this.sequenceCtx.drawImage(
image,
0,
(this.screenWidth * 9 / 16 - this.screenHeight) / 2 * imgWidth / this.screenWidth,
// 0,
// (this.screenWidth * 9 / 16 - this.screenHeight) / 2 + 40,
imgWidth,
imgHeight - ((this.screenWidth * 9 / 16 - this.screenHeight) * imgWidth / this.screenWidth),
0,
0,
this.screenWidth,
this.screenHeight
)
}
if (this.screenWidth > imgWidth && this.screenHeight > imgHeight && dw < dh) {
this.sequenceCtx.drawImage(
image,
(this.screenHeight * 16 / 9 - this.screenWidth) / 2 * imgHeight / this.screenHeight,
0,
imgWidth - ((this.screenHeight * 16 / 9 - this.screenWidth) * imgHeight / this.screenHeight),
imgHeight,
0,
0,
this.screenWidth,
this.screenHeight
)
}
if (this.screenWidth > imgWidth && this.screenHeight > imgHeight && dw > dh) {
this.sequenceCtx.drawImage(
image,
0,
(this.screenWidth * 9 / 16 - this.screenHeight) / 2 * imgWidth / this.screenWidth,
imgWidth,
imgHeight - ((this.screenWidth * 9 / 16 - this.screenHeight) * imgWidth / this.screenWidth),
0,
0,
this.screenWidth,
this.screenHeight
)
}
}
}
}
exports.module = CanvasDraw