-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmyScript.js
More file actions
149 lines (134 loc) · 3.65 KB
/
myScript.js
File metadata and controls
149 lines (134 loc) · 3.65 KB
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
// create a wrapper around native canvas element (with id="c")
var canvas = new fabric.Canvas('myCanvas');
canvas.setWidth(780);
canvas.setHeight(500);
// // create a rectangle object
// var rect = new fabric.Rect({
// left: 100,
// top: 100,
// fill: 'red',
// width: 20,
// height: 20
// });
// // create a circle object
// var cir = new fabric.Circle({
// left: 200,
// top: 100,
// fill: 'red',
// radius: 100
// });
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) - 1);
}
var objectList = {};
var nameObject = 'Z';
function draw_shapes(name, value) {
if (value == null) {
value = 10;
}
if (name == 'circle') {
var cir = new fabric.Circle({
left: 100,
top: 100,
fill: 'red',
radius: value,
shape: 'circle'
});
console.log(cir);
canvas.add(cir);
objectList["circle"] = cir;
// nameObject = nextChar(nameObject);
} else if (name == 'square') {
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'green',
width: value,
height: value,
shape: 'rectangle'
});
canvas.add(rect);
console.log(rect.left);
objectList["square"] = rect;
// nameObject = nextChar(nameObject);
} else if (name == 'triangle') {
var tri = new fabric.Triangle({
width: 20,
height: 30,
fill: 'blue',
left: 100,
top: 100
});
canvas.add(tri);
console.log(tri.left);
objectList["triangle"] = tri;
console.log(tri.angle);
// nameObject = nextChar(nameObject);
}
}
function move_shapes(objectName, direction, value) {
if (objectName in objectList) {
var obj = objectList[objectName];
if (direction == 'left')
obj.set('left',obj.left - value);
else if (direction == 'right')
obj.set('left',obj.left + value);
else if (direction == 'up')
obj.set('top',obj.top - value);
else if (direction == 'down')
obj.set('top',obj.top + value);
canvas.add(obj)
}
}
function resize_shape(objectName,direction,value)
{
if(objectName in objectList)
{
var obj = objectList[objectName];
if(obj.type == "circle")
{
if(direction=="decrease")
obj.set('radius',obj.radius / value);
else
obj.set('radius', obj.radius * value);
}
else
{
if(direction=="decrease"){
obj.set('height',obj.height / value);
obj.set('width',obj.width / value);
}
else{
obj.set('height',obj.height * value);
obj.set('width',obj.width * value);
}
}
canvas.add(obj);
}
}
function color_shape(objectName,colorName)
{
if(objectName in objectList)
{
var obj = objectList[objectName];
obj.set('fill',colorName);
canvas.add(obj);
console.log(obj);
}
}
function clear_shape(){
canvas.clear();
finalString=''
}
function save_shape(){
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // here is the most important part because if you dont replace you will get a DOM 18 exception.
window.location.href=image;
}
function rotate_shape(objName, angleInRad)
{
var obj = objectList[objName];
var angle = obj.angle;
obj.set('angle',angle+angleInRad);
console.log(angle);
canvas.add(obj);
}