-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.html
77 lines (77 loc) · 2.3 KB
/
app.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
<!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>01、操作活动对象</title>
<link rel="stylesheet" href="styles/app.css">
</head>
<body>
<canvas id="c"></canvas>
<script src="bower_components/fabric.js/dist/fabric.js"></script>
<script>
var canvas = new fabric.Canvas('c', {
width: window.innerWidth,
height: window.innerHeight,
backgroundColor: '#eee'
});
// canvas.setBackgroundImage('images/demo.png', canvas.renderAll.bind(canvas), {
// originX: 'left',
// originY: 'top',
// scaleX: canvas.width / img.width,
// scaleY: canvas.height / img.height
// });
fabric.Image.fromURL('images/demo.png', function(img) {
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: canvas.width / img.width,
scaleY: canvas.height / img.height
});
console.log(img.get('scaleX'))
console.log(img.get('scaleY'))
});
canvas.on('mouse:wheel', function(opt) {
var delta = opt.e.deltaY;
var zoom = canvas.getZoom();
console.log(delta, zoom)
// if (zoom > 20) zoom = 20;
// if (zoom < 0.01) zoom = 0.01;
canvas.setZoom(zoom);
opt.e.preventDefault();
opt.e.stopPropagation();
})
canvas.on('mouse:down', function(opt) {
var evt = opt.e;
if (evt.altKey === true) {
this.isDragging = true;
this.selection = false;
this.lastPosX = evt.clientX;
this.lastPosY = evt.clientY;
}
});
canvas.on('mouse:move', function(opt) {
if (this.isDragging) {
var e = opt.e;
this.viewportTransform[4] += e.clientX - this.lastPosX;
this.viewportTransform[5] += e.clientY - this.lastPosY;
this.requestRenderAll();
this.lastPosX = e.clientX;
this.lastPosY = e.clientY;
}
});
canvas.on('mouse:up', function(opt) {
this.isDragging = false;
this.selection = true;
});
// canvas.add(rect1, rect2);
// rect1.on({
// 'moving': function(e) {
// e.target.opacity = 0.5;
// },
// 'modified': function(e) {
// e.target.opacity = 1;
// }
// });
</script>
</body>
</html>