forked from willianpc/comicgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomicgen.js
222 lines (177 loc) · 4.57 KB
/
comicgen.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var d = document;
var cg = {};
var c = $('#c')[0];
var ctx = c.getContext('2d');
var scene = new RB.Scene(c);
var w = c.width;
var h = c.height;
var fontFamily = "Arial, helvetica";
var pop = new Audio('pop.ogg');
var currentObj = null;
scene.add( scene.rect(w, h, 'white') );
scene.update();
var lib = $('#lib');
var miniUrls = ["sm01_mini.png", "sm02_mini.png", "sm03_mini.png", "sushi_mini.png", "z01_mini.png", "z02_mini.png", "z03_mini.png", "z04_mini.png", "balao.png", "toon01_mini.png", "toon02_mini.png", "toon03_mini.png", "toon04_mini.png", "toon05_mini.png", "toon06_mini.png"];
var toonUrls = ["sm01.png", "sm02.png", "sm03.png", "sushi.png", "z01.png", "z02.png", "z03.png", "z04.png", "balao.png", "toon01.png", "toon02.png", "toon03.png", "toon04.png", "toon05.png", "toon06.png"];
cg.clearScreen = function(){
ctx = c.getContext('2d');
scene = new RB.Scene(c);
w = c.width;
h = c.height;
fontFamily = "Arial, helvetica";
pop = new Audio('pop.ogg');
currentObj = null;
scene.add( scene.rect(w, h, 'white') );
scene.update();
}
$(d).keyup(function(e){
var key = e.keyCode || e.which;
if(key == 46 && currentObj){
scene.remove(currentObj);
scene.update();
RB.destroyCanvas( currentObj.getCanvas().id );
currentObj = null;
}
if( currentObj && (key==37 || key==39) ){
cg.hFlip(currentObj);
}
});
$(d).keydown(function(event){
var key = event.keyCode || event.which;
if(key == 38 && currentObj){
cg.zoomIn(currentObj);
}
if(key == 40 && currentObj){
cg.zoomOut(currentObj);
}
});
d.onmousewheel = function(mw){
if(currentObj && mw.wheelDelta > 0){
cg.zoomIn(currentObj);
} else if (currentObj && mw.wheelDelta < 0){
cg.zoomOut(currentObj);
}
};
cg.buildMinis = function(){
var buffer = '';
var imgString = "<img src='toons/IMG_URL' class='rc mini'></img>";
var link = "<a href=\"javascript:cg.createImage('toons/IMG_URL')\">";
for(var i=0; i < miniUrls.length; i++){
buffer += link.replace(/IMG_URL/, toonUrls[i]);
buffer += imgString.replace(/IMG_URL/, miniUrls[i]) + '</a>';
}
lib.append(buffer);
//lib.append( $('#textTool').clone() );
$('#menuContainer').append( $('#instructs').clone() );
}
cg.buildMinis();
cg.createImage = function(url){
scene.image(url, function(obj){
obj.draggable = true;
obj.setXY(30, 30);
obj.onmousedown = function(e){
currentObj = obj;
scene.zIndex(obj, 1);
scene.update();
}
scene.add(obj);
currentObj = obj;
scene.update();
pop.play();
});
}
cg.createText = function(){
var txt = prompt("Adicione um texto:");
if(txt){
var obj = scene.text(txt, fontFamily, 20, 'black');
obj.setXY(40, 40);
obj.draggable = true;
obj.onmousedown = function(e){
currentObj = obj;
scene.zIndex(obj, 1);
scene.update();
}
currentObj = obj;
scene.add(obj);
scene.update();
pop.play();
}
}
cg.createTextFromInput = function(e){
var key = e.keyCode || e.which;
var txt = $('#newText').val();
if(key == 13){
var obj = scene.text(txt, fontFamily, 20, 'black');
obj.setXY(40, 40);
obj.draggable = true;
obj.onmousedown = function(e){
currentObj = obj;
scene.zIndex(obj, 1);
scene.update();
}
currentObj = obj;
scene.add(obj);
scene.update();
$('#newText').val('');
pop.play();
}
}
cg.saveImage = function(){
var data = c.toDataURL('png');
var win = window.open();
var b = win.document.body;
var img = new Image();
img.src = data;
b.appendChild(img);
}
cg.zoomOut = function(obj){
var w = obj.w * 0.05;
var h = obj.h * 0.05;
if(obj.w - w > 0 && obj.h - h > 0){
obj.w -= w;
obj.h -= h;
obj.x += (w/2);
obj.y += (h/2);
scene.update();
}
}
cg.zoomIn = function(obj){
var w = obj.w * 0.05;
var h = obj.h * 0.05;
obj.w += w;
obj.h += h;
obj.x -= (w/2);
obj.y -= (h/2);
scene.update();
}
cg.hFlip = function(obj){
var tmpCanvas = $(obj.getCanvas()).clone()[0];
var img = obj.getCanvas();
var tmpCtx = tmpCanvas.getContext('2d');
var w = tmpCanvas.width;
var h = tmpCanvas.height;
//save current size and position
var cW = obj.w, cH = obj.h, cX = obj.x, cY = obj.y;
tmpCtx.translate(w/2, h/2);
tmpCtx.scale(-1, 1);
tmpCtx.drawImage(img, (-1*w/2), (-1*h/2));
tmpCanvas.id = obj.getCanvas().id;
obj.getCanvas().id = 'killme';
RB.destroyCanvas('killme');
d.body.appendChild(tmpCanvas);
obj.setCanvas(tmpCanvas);
obj.x=cX; obj.y=cY; obj.h=cH; obj.w=cW;
scene.update();
}
cg.setScreen = function(w, h){
if(w && h && !isNaN(w) && !isNaN(h)){
//var ok = confirm('All your work will be lost. Continue?');
ok=true;
if(ok){
c.width = w;
c.height = h;
scene.update();
//cg.clearScreen();
}
}
}