/
index.js
202 lines (179 loc) · 5.03 KB
/
index.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
var Intent = window.Intent || window.WebKitIntent;
var startActivity = window.navigator.startActivity || window.navigator.webkitStartActivity;
window.intent = window.intent || window.webkitIntent;
var context;
var canvas;
var imageID;
var permissionKey;
var draw = function(topline, bottomline) {
var image = $("#image")[0];
var width = image.width;
var height = image.height;
var canvasWidth = width + 100;
var canvasHeight = height + 200;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
context = canvas.getContext('2d');
context.save();
context.fillStyle = "rgb(0,0,0)";
context.fillRect(0,0, canvas.width, canvas.height);
context.save();
context.strokeStyle = "rgb(255,255,255)";
context.lineWidth = 2;
context.strokeRect(46, 46, width + 8, height + 8);
context.restore();
context.drawImage(image, 50, 50);
context.save();
context.font = "62px Georgia";
context.textAlign = "center";
context.fillStyle = "white";
context.strokeStyle = "white";
context.lineWidth = 2;
context.fillText(topline, canvasWidth * 0.5, height + 125, canvasWidth * 0.9);
context.restore();
context.save();
context.font = "24px Georgia";
context.textAlign = "center";
context.fillStyle = "white";
context.strokeStyle = "white";
context.lineWidth = 2;
context.fillText(bottomline, canvasWidth * 0.5, height + 160, canvasWidth * 0.9);
context.restore();
context.restore();
if(imageID) {
updateImageData(imageID, canvas, topline, bottomline);
}
$('#container').find('h2').hide();
$('#container').addClass('loaded');
$('#words').show();
$('#help').hide();
$('#top').focus();
};
function textChanged() {
if (context) {
topText = $('#top').val() || "";
bottomText = $('#bottom').val() || "";
draw(topText, bottomText);
}
}
var createNewImage = function(c) {
var data = c.toDataURL('image/png');
// Create an image.
$.ajax({
type: 'POST',
url: '/image',
data: {image: data},
success: function(data) {
imageID = data.id;
permissionKey = data.permissionKey;
}
});
};
var updateImageData = function(id, c, textTop, textBottom) {
var data = c.toDataURL('image/png');
$.ajax({
type: 'PUT',
url: '/image/' + id,
data: { image: data, permissionKey: permissionKey, textTop: textTop, textBottom: textBottom },
success: function(data) {
}
});
};
var updateImage = function(data) {
var url = $.isArray(data) ? data[0] : data;
var img = $('#image');
img.load(function() {
var image = $('#image');
canvas = $('#container canvas');
if(canvas.length == 0) {
canvas = document.createElement('canvas');
$('#container').append(canvas);
}
else
canvas = canvas[0];
draw("","");
createNewImage(canvas);
if(window.intent) {
$('#done').show();
$('#actions').show();
}
else {
$('#done').hide();
$('#save').show();
$('#share').show();
$('#actions').show();
}
});
loadImage(img, url);
};
$(function() {
var idLocation = window.location.search.indexOf("id=");
if (window.intent || idLocation > -1) {
$('#actions').show();
$('#share').hide();
$('#save').hide();
$('#done').show();
$('#done').click(function() {
if (canvas) {
window.intent.postResult(canvas.toDataURL());
}
});
if(window.intent) {
updateImage(window.intent.data);
}
else {
var imageIDMatch = window.location.search.match(/id=(\d+)/);
if(imageIDMatch.length == 2) {
var newImageID = imageIDMatch[1];
updateImage("http://www.inspirationmator.com/image/" + newImageID + ".png");
}
}
}
else {
$('#container').click(function() {
$('#save').hide();
$('#share').hide();
$('#done').hide();
$('#actions').hide();
var i = new Intent("http://webintents.org/pick", "image/*");
startActivity.call(window.navigator, i, function(data) {
updateImage(data);
});
});
}
$('#save').click(function() {
var url = "http://www.inspirationmator.com/image/" + imageID + ".png";
var i = new Intent("http://webintents.org/save", "image/*", url);
startActivity.call(window.navigator, i);
});
$('#share').click(function() {
var url = "http://www.inspirationmator.com/image/" + imageID + ".png";
var params = {
"action": "http://webintents.org/share",
"type": "image/png",
"data": {
"url": url,
"blob": data
}
};
var i = new Intent(params);
startActivity.call(window.navigator, i);
});
$('#top').change(textChanged);
$('#bottom').change(textChanged);
var top = $('#top');
var bot = $('#bottom');
var help = $('#help');
help.on('click',function(){
$(this).fadeOut(180);
});
var btnh = $('#actions').find('.btn');
var btn = btnh.find('.icon');
btnh.hover(
function(){
$(this).find(btn).addClass('ani');
},
function(){
$(this).find(btn).removeClass('ani');
});
});