-
Notifications
You must be signed in to change notification settings - Fork 2
/
05.html
59 lines (56 loc) · 1.64 KB
/
05.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
<!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>05、复制和粘贴</title>
</head>
<body>
<h3>分两步完成复制、粘贴</h3>
<button id="copy">复制选中对象</button>
<button id="paste">粘贴</button>
<h3>一步完成复制、粘贴</h3>
<button id="clone">克隆选中对象</button>
<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 clipboard = null;
var canvas = new fabric.Canvas('c', { width: 500, height: 300, backgroundColor: '#ccc' });
var rect = new fabric.Rect({ top: 30, width: 100, height: 50, fill: 'green' });
var circle = new fabric.Circle({ top: 30, left: 130, radius: 50, fill: 'red' });
canvas.add(rect, circle);
// 默认选中圆
canvas.setActiveObject(circle)
// 不选中任何对象
// canvas.discardActiveObject();
// 复制
$('#copy').click(function () {
var active = canvas.getActiveObject();
if (!active) return;
active.clone(function (cloned) {
clipboard = cloned;
});
});
// 粘贴
$('#paste').click(function () {
canvas.add(clipboard);
});
// 复制、粘贴
$('#clone').click(function () {
var active = canvas.getActiveObject();
if (!active) return;
active.clone(function (cloned) {
cloned.set({
left: cloned.left + 10,
top: cloned.top + 10
});
canvas.add(cloned)
canvas.setActiveObject(cloned)
})
});
</script>
</body>
</html>