-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
169 lines (164 loc) · 5.99 KB
/
index.html
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>使用canvas模拟图像 object-fit,并支持旋转图片 </title>
</head>
<body>
<div>
测试的图片地址<input
type="text"
id="imgInput"
value="https://img11.360buyimg.com/imagetools/jfs/t1/194717/29/3901/78726/60a4d373E231e8129/d65a12481578a32b.png"
/>
</div>
<div>
容器宽度<input type="text" id="widthInput" value="375" /> 容器高度<input
type="text"
id="heightInput"
value="667"
/>
</div>
<div>
形式
<select id="typeSelect">
<option value="FILL">FILL</option>
<option value="FIT">FIT</option>
<option value="STRETCH">STRETCH</option>
</select>
</div>
<div><button onclick="createCanvas()">生成</button><button id="rotate" onclick="onRotate()">旋转</button></div>
<div id="imgResult"></div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let rotation = 0;
const imgInput = document.getElementById("imgInput");
const widthInput = document.getElementById("widthInput");
const heightInput = document.getElementById("heightInput");
const typeSelect = document.getElementById("typeSelect");
const imgResult = document.getElementById("imgResult");
createCanvas()
function onRotate() {
rotation += 90;
createCanvas();
}
// 测试生成canvas
function createCanvas() {
const containerWidth = parseFloat(widthInput.value);
const containerHeight = parseFloat(heightInput.value);
ctx.clearRect(0, 0, containerWidth, containerHeight);
canvas.width = containerWidth;
canvas.height = containerHeight;
// 绘容器
ctx.rect(0, 0, containerWidth, containerHeight);
ctx.stroke();
// 绘图
const type = typeSelect.options[typeSelect.selectedIndex].value;
const img = document.createElement("img");
img.src = imgInput.value;
img.onload = function () {
imgResult.innerHTML =
"图片的宽:" + this.width + "; 图片的高:" + this.height;
const radians = (Math.PI / 180) * rotation;
const { width: cWidth, height: cHeight } = getBoundingDimentions(
containerWidth,
containerHeight,
radians
);
console.log(cWidth, cHeight);
ctx.translate(containerWidth / 2, containerHeight / 2);
ctx.rotate((Math.PI * rotation) / 180);
ctx.translate(-cWidth / 2, -cHeight / 2);
if (type === "STRETCH") {
ctx.drawImage(img, 0, 0, cWidth, cHeight);
} else {
const { sx, sy, swidth, sheight, x, y, width, height } =
getObjectFitSize(type, cWidth, cHeight, this.width, this.height);
ctx.drawImage(img, sx, sy, swidth, sheight, x, y, width, height);
}
};
}
/**
* 计算图片裁剪或者摆放位置
* @param {*} type FILL, FIT 暂时只兼容这两个模式
* @param {*} containerWidth 容器宽度
* @param {*} containerHeight 容器高度
* @param {*} imgWidth 图片宽度
* @param {*} imgHeight 图片高度
*/
function getObjectFitSize(
type = "FILL",
containerWidth,
containerHeight,
imgWidth,
imgHeight
) {
let radio = 1, // 容器与图片的比例
sx = 0, // 开始剪切的 x 坐标位置。
sy = 0, // 开始剪切的 y 坐标位置。
swidth = imgWidth, // 被剪切图像的宽度。
sheight = imgHeight, // 被剪切图像的高度。
x = 0, // 在画布上放置图像的 x 坐标位置。
y = 0, // 在画布上放置图像的 y 坐标位置。
width = containerWidth, // 要使用的图像的宽度(伸展或缩小图像)。
height = containerHeight; // 要使用的图像的高度(伸展或缩小图像)。
let cWHRatio = containerWidth / containerHeight;
let iWHRatio = imgWidth / imgHeight;
if (type === "FILL") {
// fill模式,需要裁剪
if (iWHRatio >= cWHRatio) {
// 横图,高先匹配,裁剪宽度
radio = containerHeight / imgHeight;
sx = (imgWidth - containerWidth / radio) / 2;
swidth = containerWidth / radio;
sheight = imgHeight;
} else {
// 竖图,宽先匹配,裁剪高度
radio = containerWidth / imgWidth;
sy = (imgHeight - containerHeight / radio) / 2;
swidth = imgWidth;
sheight = containerHeight / radio;
}
} else if (type === "FIT") {
if (iWHRatio >= cWHRatio) {
// 横图,宽先匹配,高度自适应
radio = containerWidth / imgWidth;
y = (containerHeight - imgHeight * radio) / 2;
height = imgHeight * radio;
} else {
// 竖图,高先匹配,宽度自适应
radio = containerHeight / imgHeight;
x = (containerWidth - imgWidth * radio) / 2;
width = imgWidth * radio;
}
}
return {
sx,
sy,
swidth,
sheight,
x,
y,
width,
height,
};
}
function getBoundingDimentions(width, height, rotation) {
const { cos, sin, abs } = Math;
/* eslint-disable max-len */
const widthAfterRotation =
abs(width * sin(rotation)) + abs(height * cos(rotation));
const heightAfterRotation =
abs(width * cos(rotation)) + abs(height * sin(rotation));
/* eslint-enable max-len */
return {
height: widthAfterRotation,
width: heightAfterRotation,
};
}
</script>
</body>
</html>