-
Notifications
You must be signed in to change notification settings - Fork 2
/
07.html
48 lines (48 loc) · 1.63 KB
/
07.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07、图形变换了,controls 也得做相应变换</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script src="./bower_components/jQuery/dist/jquery.js"></script>
<script src="./bower_components/fabric.js/dist/fabric.js"></script>
<script>
var canvas = new fabric.Canvas('c', { width: 800, height: 500, backgroundColor: '#ccc' });
fabric.Image.fromURL('images/qq.png', function(image) {
// 反转矩阵 fabric.util.invertTransform(image.calcTransformMatrix())
// 自定义的矩阵使图片进行缩放、扭曲、移动
var configMatrix = [1, 0, 0, -1, 200, 100]
image.transformMatrix = configMatrix;
var currentT = image.calcTransformMatrix();
console.log(1, currentT)
// 矩阵乘
var mT = fabric.util.multiplyTransformMatrices(currentT, configMatrix);
console.log(2, mT);
// 将矩阵乘结果转换为图片属性
var options = fabric.util.qrDecompose(mT);
console.log(3, options);
var newCenter = { x: options.translateX, y: options.translateY };
image.set({
transformMatrix: [1, 0, 0, 1, 0, 0],
flipX: false,
flipY: false,
...options
});
// 设置对象的位置,同时考虑对象的原点
image.setPositionByOrigin(newCenter, 'center', 'center');
// 重新计算控件位置;否则会出现错位现象
image.setCoords(false, true);
canvas.add(image);
});
</script>
</body>
</html>