forked from EffEPi/fabric.curvedText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
159 lines (143 loc) · 4.07 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
<html lang="en">
<head>
<meta charset="utf-8">
<style>
canvas {
border:1px solid #000;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.0.0-beta.1/fabric.js"></script>
<script src="http://code.jquery.com/jquery-git.js"></script>
<script src="fabric.curvedText.js"></script>
<canvas id="c" width="400" height="200"></canvas><br/>
<input type="text" id="text" value="CurvedText" /><br>
Reverse : <input type="checkbox" name="reverse" id="reverse" /><br>
Radius : <input type="range" min="0" max="100" value="50" id="radius" /><br>
Spacing : <input type="range" min="5" max="40" value="20" id="spacing" /><br>
Color : <input type="color" value="#0000ff" id="fill" /><br>
Effect :
<select name="effect" id="effect" >
<option value="curved">Curved</option>
<option value="arc">Arc</option>
<option value="STRAIGHT">STRAIGHT</option>
<option value="smallToLarge">smallToLarge</option>
<option value="largeToSmallTop">largeToSmallToped</option>
<option value="largeToSmallBottom">largeToSmallBottom</option>
<option value="bulge">bulge</option>
</select>
<br>
<br>
<button id="convert">Convert Text/Curved</button>
<button id="save">Save/Reload</button>
<script>
$(function () {
canvas = new fabric.Canvas('c');
canvas.on('selection:cleared', onDeSelected);
canvas.on('object:selected', onSelected);
canvas.on('selection:created', onSelected);
var CurvedText = new fabric.CurvedText('CurvedText',{
//width: 100,
//height: 50,
left: 100,
top: 100,
textAlign: 'center',
fill: '#0000FF',
radius: 150,
fontSize: 20,
spacing: 20
//fontFamily: 'Arial'
});
canvas.add(CurvedText).renderAll();
canvas.setActiveObject(canvas.item(canvas.getObjects().length-1));
$('#text').keyup(function(){
var obj = canvas.getActiveObject();
if(obj){
obj.setText(this.value);
canvas.renderAll();
}
});
$('#reverse').click(function(){
var obj = canvas.getActiveObject();
if(obj){
obj.set('reverse',$(this).is(':checked'));
canvas.renderAll();
}
});
$('#radius, #spacing, #fill').change(function(){
var obj = canvas.getActiveObject();
if(obj){
obj.set($(this).attr('id'),$(this).val());
}
canvas.renderAll();
});
$('#radius, #spacing, #effect').change(function(){
var obj = canvas.getActiveObject();
if(obj){
obj.set($(this).attr('id'),$(this).val());
}
canvas.renderAll();
});
$('#fill').change(function(){
var obj = canvas.getActiveObject();
if(obj){
obj.setFill($(this).val());
}
canvas.renderAll();
});
$('#save').click(function() {
var design = JSON.stringify(canvas.toJSON());
canvas.clear();
canvas.renderAll();
canvas.loadFromJSON(design, function() {
console.log('loaded');
canvas.renderAll();
});
});
$('#convert').click(function(){
var props = {};
var obj = canvas.getActiveObject();
if(obj){
if(/curvedText/.test(obj.type)) {
default_text = obj.getText();
props = obj.toObject();
delete props['type'];
var textSample = new fabric.Text(default_text, props);
}else if(/text/.test(obj.type)) {
default_text = obj.getText();
props = obj.toObject();
delete props['type'];
props['textAlign'] = 'center';
props['radius'] = 50;
props['spacing'] = 20;
var textSample = new fabric.CurvedText(default_text, props);
}
canvas.remove(obj);
canvas.add(textSample).renderAll();
canvas.setActiveObject(canvas.item(canvas.getObjects().length-1));
}
});
});
function onSelected(){
var obj = canvas.getActiveObject();
$('#text').val(obj.getText());
$('#reverse').prop('checked', obj.get('reverse'));
$('#radius').val(obj.get('radius'));
$('#spacing').val(obj.get('spacing'));
$('#fill').val(obj.getFill());
if(obj.getEffect) {
$('#effect').val(obj.getEffect());
}
}
function onDeSelected(){
$('#text').val('');
$('#reverse').prop('checked', false);
$('#radius').val(50);
$('#spacing').val(20);
$('#fill').val('#0000FF');
$('#effect').val('curved');
}
</script>
</body>
</html>