social_cheesecake 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/app/assets/javascripts/kinetic.js +710 -407
- data/app/assets/javascripts/socialcheesecake/_header.js +1 -1
- data/app/assets/javascripts/socialcheesecake/actor.js +30 -6
- data/app/assets/javascripts/socialcheesecake/cheesecake.js +388 -64
- data/app/assets/javascripts/socialcheesecake/grid.js +63 -34
- data/app/assets/javascripts/socialcheesecake/search.js +26 -0
- data/app/assets/javascripts/socialcheesecake/sector.js +326 -62
- data/app/assets/javascripts/socialcheesecake/text.js +25 -2
- data/lib/social_cheesecake/version.rb +1 -1
- data/test/dummy/public/index.html +18 -19
- metadata +5 -4
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* KineticJS JavaScript Library v3.
|
2
|
+
* KineticJS JavaScript Library v3.4.0
|
3
3
|
* http://www.kineticjs.com/
|
4
4
|
* Copyright 2011, Eric Rowell
|
5
5
|
* Licensed under the MIT or GPL Version 2 licenses.
|
6
|
-
* Date: Dec
|
6
|
+
* Date: Dec 31 2011
|
7
7
|
*
|
8
8
|
* Copyright (C) 2011 by Eric Rowell
|
9
9
|
*
|
@@ -30,553 +30,856 @@ var Kinetic = {};
|
|
30
30
|
/****************************************
|
31
31
|
* Layer
|
32
32
|
*/
|
33
|
-
Kinetic.Layer = function(stage,
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
33
|
+
Kinetic.Layer = function(stage, isInvisible){
|
34
|
+
this.canvas = document.createElement('canvas');
|
35
|
+
this.context = this.canvas.getContext('2d');
|
36
|
+
this.canvas.width = stage.width;
|
37
|
+
this.canvas.height = stage.height;
|
38
|
+
this.canvas.style.position = 'absolute';
|
39
|
+
this.shapes = [];
|
40
|
+
|
41
|
+
if (isInvisible) {
|
42
|
+
var that = this;
|
43
|
+
this.context.stroke = function(){
|
44
|
+
};
|
45
|
+
this.context.fill = function(){
|
46
|
+
};
|
47
|
+
this.context.fillRect = function(x, y, width, height){
|
48
|
+
that.context.rect(x, y, width, height);
|
49
|
+
};
|
50
|
+
this.context.strokeRect = function(x, y, width, height){
|
51
|
+
that.context.rect(x, y, width, height);
|
52
|
+
};
|
53
|
+
this.context.drawImage = function(){
|
54
|
+
};
|
55
|
+
this.context.fillText = function(){
|
56
|
+
};
|
57
|
+
this.context.strokeText = function(){
|
58
|
+
};
|
59
|
+
}
|
60
|
+
|
61
|
+
stage.container.appendChild(this.canvas);
|
49
62
|
}
|
50
63
|
/*
|
51
64
|
* clear layer
|
52
65
|
*/
|
53
|
-
Kinetic.Layer.prototype.clear = function()
|
54
|
-
|
55
|
-
|
56
|
-
|
66
|
+
Kinetic.Layer.prototype.clear = function(){
|
67
|
+
var context = this.getContext();
|
68
|
+
var canvas = this.getCanvas();
|
69
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
57
70
|
}
|
58
71
|
/*
|
59
72
|
* get layer canvas
|
60
73
|
*/
|
61
|
-
Kinetic.Layer.prototype.getCanvas = function()
|
62
|
-
|
74
|
+
Kinetic.Layer.prototype.getCanvas = function(){
|
75
|
+
return this.canvas;
|
63
76
|
}
|
64
77
|
/*
|
65
78
|
* get layer context
|
66
79
|
*/
|
67
|
-
Kinetic.Layer.prototype.getContext = function()
|
68
|
-
|
80
|
+
Kinetic.Layer.prototype.getContext = function(){
|
81
|
+
return this.context;
|
69
82
|
}
|
70
83
|
/*
|
71
84
|
* get layer shapes
|
72
85
|
*/
|
73
|
-
Kinetic.Layer.prototype.getShapes = function()
|
74
|
-
|
86
|
+
Kinetic.Layer.prototype.getShapes = function(){
|
87
|
+
return this.shapes;
|
75
88
|
}
|
76
89
|
/*
|
77
90
|
* draw all shapes in layer
|
78
91
|
*/
|
79
|
-
Kinetic.Layer.prototype.draw = function()
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
92
|
+
Kinetic.Layer.prototype.draw = function(){
|
93
|
+
this.clear();
|
94
|
+
var context = this.getContext();
|
95
|
+
|
96
|
+
for (var n = 0; n < this.getShapes().length; n++) {
|
97
|
+
this.getShapes()[n].draw(this);
|
98
|
+
}
|
86
99
|
};
|
87
100
|
/****************************************
|
88
101
|
* Stage
|
89
102
|
*/
|
90
|
-
Kinetic.Stage = function(containerId, width, height)
|
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
|
-
|
103
|
+
Kinetic.Stage = function(containerId, width, height){
|
104
|
+
this.container = document.getElementById(containerId);
|
105
|
+
this.width = width;
|
106
|
+
this.height = height;
|
107
|
+
this.zIndexCounter = 9999;
|
108
|
+
this.idCounter = 0;
|
109
|
+
this.dblClickWindow = 400;
|
110
|
+
// desktop flags
|
111
|
+
this.mousePos = null;
|
112
|
+
this.mouseDown = false;
|
113
|
+
this.mouseUp = false;
|
114
|
+
|
115
|
+
// mobile flags
|
116
|
+
this.touchPos = null;
|
117
|
+
this.touchStart = false;
|
118
|
+
this.touchEnd = false;
|
119
|
+
|
120
|
+
// create layers
|
121
|
+
this.bufferLayer = new Kinetic.Layer(this);
|
122
|
+
this.backstageLayer = new Kinetic.Layer(this, true);
|
123
|
+
this.stageLayer = new Kinetic.Layer(this);
|
124
|
+
this.propsLayer = new Kinetic.Layer(this);
|
125
|
+
this.actorsLayer = new Kinetic.Layer(this);
|
126
|
+
|
127
|
+
this.bufferLayer.getCanvas().style.display = 'none';
|
128
|
+
this.backstageLayer.getCanvas().style.display = 'none';
|
129
|
+
this.listen();
|
130
|
+
|
131
|
+
this.addEventListener("mouseout", function(evt){
|
132
|
+
that.shapeDragging = undefined;
|
133
|
+
}, false);
|
134
|
+
|
135
|
+
// prepare stage for drag and drop
|
136
|
+
var that = this;
|
137
|
+
this.addEventListener("mousemove", function(evt){
|
138
|
+
if (that.shapeDragging) {
|
139
|
+
var mousePos = that.getMousePos();
|
140
|
+
that.shapeDragging.x = mousePos.x - that.shapeDragging.offset.x;
|
141
|
+
that.shapeDragging.y = mousePos.y - that.shapeDragging.offset.y;
|
142
|
+
that.drawActors();
|
143
|
+
}
|
144
|
+
}, false);
|
145
|
+
this.addEventListener("mouseup", function(evt){
|
146
|
+
// execute user defined ondragend if defined
|
147
|
+
|
148
|
+
if (that.shapeDragging) {
|
149
|
+
var dragend = that.shapeDragging.eventListeners.ondragend;
|
150
|
+
if (dragend) {
|
151
|
+
for (var i = 0; i < dragend.length; i++) {
|
152
|
+
dragend[i].func(evt);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}
|
156
|
+
that.shapeDragging = undefined;
|
157
|
+
});
|
158
|
+
};
|
159
|
+
/*
|
160
|
+
* get buffer layer
|
161
|
+
*/
|
162
|
+
Kinetic.Stage.prototype.getBufferLayer = function(){
|
163
|
+
return this.bufferLayer;
|
164
|
+
};
|
165
|
+
/*
|
166
|
+
* get backstage layer
|
167
|
+
*/
|
168
|
+
Kinetic.Stage.prototype.getBackstageLayer = function(){
|
169
|
+
return this.backstageLayer;
|
121
170
|
};
|
122
171
|
/*
|
123
172
|
* get stage layer
|
124
173
|
*/
|
125
|
-
Kinetic.Stage.prototype.getStageLayer = function()
|
126
|
-
|
174
|
+
Kinetic.Stage.prototype.getStageLayer = function(){
|
175
|
+
return this.stageLayer;
|
127
176
|
};
|
128
177
|
/*
|
129
178
|
* get props layer
|
130
179
|
*/
|
131
|
-
Kinetic.Stage.prototype.getPropsLayer = function()
|
132
|
-
|
180
|
+
Kinetic.Stage.prototype.getPropsLayer = function(){
|
181
|
+
return this.propsLayer;
|
133
182
|
};
|
134
183
|
/*
|
135
184
|
* get actors layer
|
136
185
|
*/
|
137
|
-
Kinetic.Stage.prototype.getActorsLayer = function()
|
138
|
-
|
186
|
+
Kinetic.Stage.prototype.getActorsLayer = function(){
|
187
|
+
return this.actorsLayer;
|
139
188
|
};
|
140
189
|
/*
|
141
190
|
* clear stage
|
142
191
|
*/
|
143
|
-
Kinetic.Stage.prototype.clear = function()
|
144
|
-
|
192
|
+
Kinetic.Stage.prototype.clear = function(){
|
193
|
+
this.stageLayer.clear();
|
145
194
|
};
|
146
195
|
/*
|
147
196
|
* clear all canvases
|
148
197
|
*/
|
149
|
-
Kinetic.Stage.prototype.clearAll = function()
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
198
|
+
Kinetic.Stage.prototype.clearAll = function(){
|
199
|
+
this.backstageLayer.clear();
|
200
|
+
this.stageLayer.clear();
|
201
|
+
this.propsLayer.clear();
|
202
|
+
this.actorsLayer.clear();
|
203
|
+
};
|
204
|
+
/*
|
205
|
+
* Composite toDataURL
|
206
|
+
*/
|
207
|
+
Kinetic.Stage.prototype.toDataURL = function(callback){
|
208
|
+
var bufferLayer = this.getBufferLayer();
|
209
|
+
var bufferContext = bufferLayer.getContext();
|
210
|
+
var stageLayer = this.getStageLayer();
|
211
|
+
var propsLayer = this.getPropsLayer();
|
212
|
+
var actorsLayer = this.getActorsLayer();
|
213
|
+
var layers = [stageLayer, propsLayer, actorsLayer];
|
214
|
+
|
215
|
+
function addLayer(n){
|
216
|
+
var dataURL = layers[n].getCanvas().toDataURL();
|
217
|
+
var imageObj = new Image();
|
218
|
+
imageObj.onload = function(){
|
219
|
+
bufferContext.drawImage(this, 0, 0);
|
220
|
+
n++;
|
221
|
+
if (n < layers.length) {
|
222
|
+
addLayer(n);
|
223
|
+
}
|
224
|
+
else {
|
225
|
+
callback(bufferLayer.getCanvas().toDataURL());
|
226
|
+
}
|
227
|
+
};
|
228
|
+
imageObj.src = dataURL;
|
229
|
+
}
|
230
|
+
|
231
|
+
bufferLayer.clear();
|
232
|
+
addLayer(0);
|
154
233
|
};
|
155
234
|
/*
|
156
|
-
*
|
235
|
+
* draw actors layer
|
157
236
|
*/
|
158
|
-
Kinetic.Stage.prototype.
|
159
|
-
|
237
|
+
Kinetic.Stage.prototype.drawActors = function(){
|
238
|
+
this.getActorsLayer().draw();
|
239
|
+
};
|
240
|
+
/*
|
241
|
+
* draw props layer
|
242
|
+
*/
|
243
|
+
Kinetic.Stage.prototype.drawProps = function(){
|
244
|
+
this.getPropsLayer().draw();
|
245
|
+
};
|
246
|
+
/*
|
247
|
+
* draw actors and props layer. This method should be used
|
248
|
+
* in combination with makeActor() or makeProp()
|
249
|
+
*/
|
250
|
+
Kinetic.Stage.prototype.draw = function(){
|
251
|
+
this.drawProps();
|
252
|
+
this.drawActors();
|
160
253
|
};
|
161
254
|
/*
|
162
255
|
* remove a shape from the stage
|
163
256
|
*/
|
164
|
-
Kinetic.Stage.prototype.remove = function(shape)
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
257
|
+
Kinetic.Stage.prototype.remove = function(shape){
|
258
|
+
// remove from shapes array
|
259
|
+
var shapes = this.getShapes();
|
260
|
+
for (var n = 0; n < shapes.length; n++) {
|
261
|
+
var id = shapes[n].id;
|
262
|
+
if (id == shape.id) {
|
263
|
+
shape.getLayer().getShapes().splice(n, 1);
|
264
|
+
}
|
265
|
+
}
|
173
266
|
};
|
174
267
|
/*
|
175
268
|
* remove all shapes from the stage
|
176
269
|
*/
|
177
|
-
Kinetic.Stage.prototype.removeAll = function()
|
178
|
-
|
179
|
-
|
180
|
-
|
270
|
+
Kinetic.Stage.prototype.removeAll = function(){
|
271
|
+
// remove all shapes
|
272
|
+
this.getPropsLayer().shapes = [];
|
273
|
+
this.getActorsLayer().shapes = [];
|
181
274
|
};
|
182
275
|
/*
|
183
276
|
* get stage canvas
|
184
277
|
*/
|
185
|
-
Kinetic.Stage.prototype.getCanvas = function()
|
186
|
-
|
278
|
+
Kinetic.Stage.prototype.getCanvas = function(){
|
279
|
+
return this.stageLayer.getCanvas();
|
187
280
|
};
|
188
281
|
/*
|
189
282
|
* get stage context
|
190
283
|
*/
|
191
|
-
Kinetic.Stage.prototype.getContext = function()
|
192
|
-
|
284
|
+
Kinetic.Stage.prototype.getContext = function(){
|
285
|
+
return this.stageLayer.getContext();
|
193
286
|
};
|
194
287
|
/*
|
195
|
-
* add event listener to stage (which is essentially
|
288
|
+
* short-hand add event listener to stage (which is essentially
|
196
289
|
* the container DOM)
|
197
290
|
*/
|
198
|
-
Kinetic.Stage.prototype.
|
199
|
-
|
291
|
+
Kinetic.Stage.prototype.on = function(type, func){
|
292
|
+
this.container.addEventListener(type, func);
|
293
|
+
};
|
294
|
+
/*
|
295
|
+
* long-hand add event listener to stage (which is essentially
|
296
|
+
* the container DOM)
|
297
|
+
*/
|
298
|
+
Kinetic.Stage.prototype.addEventListener = function(type, func){
|
299
|
+
this.on(type, func);
|
200
300
|
};
|
201
301
|
/*
|
202
302
|
* add shape to stage
|
203
303
|
*/
|
204
|
-
Kinetic.Stage.prototype.add = function(shape)
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
304
|
+
Kinetic.Stage.prototype.add = function(shape){
|
305
|
+
shape.stage = this;
|
306
|
+
if (shape.isProp) {
|
307
|
+
shape.layer = this.propsLayer;
|
308
|
+
}
|
309
|
+
else {
|
310
|
+
shape.layer = this.actorsLayer;
|
311
|
+
}
|
312
|
+
|
313
|
+
shape.getLayer().shapes.push(shape);
|
314
|
+
|
315
|
+
shape.id = this.idCounter++;
|
316
|
+
shape.draw(shape.layer);
|
216
317
|
};
|
217
318
|
/*
|
218
319
|
* handle incoming event
|
219
320
|
*/
|
220
|
-
Kinetic.Stage.prototype.handleEvent = function(evt)
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
321
|
+
Kinetic.Stage.prototype.handleEvent = function(evt){
|
322
|
+
if (!evt) {
|
323
|
+
evt = window.event;
|
324
|
+
}
|
325
|
+
|
326
|
+
this.setMousePosition(evt);
|
327
|
+
this.setTouchPosition(evt);
|
328
|
+
|
329
|
+
var backstageLayer = this.backstageLayer;
|
330
|
+
var backstageLayerContext = backstageLayer.getContext();
|
331
|
+
var that = this;
|
332
|
+
|
333
|
+
backstageLayer.clear();
|
334
|
+
|
335
|
+
for (var n = this.getShapes().length - 1; n >= 0; n--) {
|
336
|
+
var pubShape = this.getShapes()[n];
|
337
|
+
(function(){
|
338
|
+
var shape = pubShape;
|
339
|
+
shape.draw(backstageLayer);
|
340
|
+
var pos = that.touchPos || that.mousePos;
|
341
|
+
var el = shape.eventListeners;
|
342
|
+
|
343
|
+
if (shape.visible && pos !== null && backstageLayerContext.isPointInPath(pos.x, pos.y)) {
|
344
|
+
// handle onmousedown
|
345
|
+
if (that.mouseDown) {
|
346
|
+
that.mouseDown = false;
|
347
|
+
shape.clickStart = true;
|
348
|
+
|
349
|
+
if (el.onmousedown) {
|
350
|
+
for (var i = 0; i < el.onmousedown.length; i++) {
|
351
|
+
el.onmousedown[i].func(evt);
|
352
|
+
}
|
353
|
+
}
|
354
|
+
n = -1;
|
355
|
+
}
|
356
|
+
// handle onmouseup & onclick
|
357
|
+
else if (that.mouseUp) {
|
358
|
+
that.mouseUp = false;
|
359
|
+
if (el.onmouseup) {
|
360
|
+
for (var i = 0; i < el.onmouseup.length; i++) {
|
361
|
+
el.onmouseup[i].func(evt);
|
362
|
+
}
|
363
|
+
}
|
364
|
+
|
365
|
+
// detect if click or double click occurred
|
366
|
+
if (shape.clickStart) {
|
367
|
+
if (el.onclick) {
|
368
|
+
for (var i = 0; i < el.onclick.length; i++) {
|
369
|
+
el.onclick[i].func(evt);
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
if (el.ondblclick && shape.inDoubleClickWindow) {
|
374
|
+
for (var i = 0; i < el.ondblclick.length; i++) {
|
375
|
+
el.ondblclick[i].func(evt);
|
376
|
+
}
|
377
|
+
}
|
378
|
+
|
379
|
+
shape.inDoubleClickWindow = true;
|
380
|
+
|
381
|
+
setTimeout(function(){
|
382
|
+
shape.inDoubleClickWindow = false;
|
383
|
+
}, that.dblClickWindow);
|
384
|
+
}
|
385
|
+
n = -1;
|
386
|
+
}
|
387
|
+
|
388
|
+
// handle touchstart
|
389
|
+
else if (that.touchStart) {
|
390
|
+
that.touchStart = false;
|
391
|
+
if (el.touchstart) {
|
392
|
+
for (var i = 0; i < el.touchstart.length; i++) {
|
393
|
+
el.touchstart[i].func(evt);
|
394
|
+
}
|
395
|
+
}
|
396
|
+
n = -1;
|
397
|
+
}
|
398
|
+
|
399
|
+
// handle touchend
|
400
|
+
else if (that.touchEnd) {
|
401
|
+
that.touchEnd = false;
|
402
|
+
if (el.touchend) {
|
403
|
+
for (var i = 0; i < el.touchend.length; i++) {
|
404
|
+
el.touchend[i].func(evt);
|
405
|
+
}
|
406
|
+
}
|
407
|
+
n = -1;
|
408
|
+
}
|
409
|
+
|
410
|
+
// handle touchmove
|
411
|
+
else if (el.touchmove) {
|
412
|
+
for (var i = 0; i < el.touchmove.length; i++) {
|
413
|
+
el.touchmove[i].func(evt);
|
414
|
+
}
|
415
|
+
n = -1;
|
416
|
+
}
|
417
|
+
|
418
|
+
// handle onmouseover
|
419
|
+
else if (!shape.mouseOver) {
|
420
|
+
shape.mouseOver = true;
|
421
|
+
if (el.onmouseover) {
|
422
|
+
for (var i = 0; i < el.onmouseover.length; i++) {
|
423
|
+
el.onmouseover[i].func(evt);
|
424
|
+
}
|
425
|
+
}
|
426
|
+
n = -1;
|
427
|
+
}
|
428
|
+
|
429
|
+
// handle onmousemove
|
430
|
+
else if (el.onmousemove) {
|
431
|
+
for (var i = 0; i < el.onmousemove.length; i++) {
|
432
|
+
el.onmousemove[i].func(evt);
|
433
|
+
}
|
434
|
+
n = -1;
|
435
|
+
}
|
436
|
+
}
|
437
|
+
// handle mouseout condition
|
438
|
+
else if (shape.mouseOver) {
|
439
|
+
shape.mouseOver = false;
|
440
|
+
if (el.onmouseout) {
|
441
|
+
for (var i = 0; i < el.onmouseout.length; i++) {
|
442
|
+
el.onmouseout[i].func(evt);
|
443
|
+
}
|
444
|
+
}
|
445
|
+
n = -1;
|
446
|
+
}
|
447
|
+
}());
|
448
|
+
}
|
328
449
|
};
|
329
450
|
/*
|
330
451
|
* begin listening for events by adding event handlers
|
331
452
|
* to the container
|
332
453
|
*/
|
333
|
-
Kinetic.Stage.prototype.listen = function()
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
454
|
+
Kinetic.Stage.prototype.listen = function(){
|
455
|
+
var that = this;
|
456
|
+
|
457
|
+
// desktop events
|
458
|
+
this.container.addEventListener("mousedown", function(evt){
|
459
|
+
that.mouseDown = true;
|
460
|
+
that.handleEvent(evt);
|
461
|
+
}, false);
|
462
|
+
|
463
|
+
this.container.addEventListener("mousemove", function(evt){
|
464
|
+
that.mouseUp = false;
|
465
|
+
that.mouseDown = false;
|
466
|
+
that.handleEvent(evt);
|
467
|
+
}, false);
|
468
|
+
|
469
|
+
this.container.addEventListener("mouseup", function(evt){
|
470
|
+
that.mouseUp = true;
|
471
|
+
that.mouseDown = false;
|
472
|
+
that.handleEvent(evt);
|
473
|
+
|
474
|
+
// clear all click starts
|
475
|
+
for (var i = 0; i < that.getShapes().length; i++) {
|
476
|
+
that.getShapes()[i].clickStart = false;
|
477
|
+
}
|
478
|
+
}, false);
|
479
|
+
|
480
|
+
this.container.addEventListener("mouseover", function(evt){
|
481
|
+
that.handleEvent(evt);
|
482
|
+
}, false);
|
483
|
+
|
484
|
+
this.container.addEventListener("mouseout", function(evt){
|
485
|
+
that.mousePos = null;
|
486
|
+
}, false);
|
487
|
+
// mobile events
|
488
|
+
this.container.addEventListener("touchstart", function(evt){
|
489
|
+
evt.preventDefault();
|
490
|
+
that.touchStart = true;
|
491
|
+
that.handleEvent(evt);
|
492
|
+
}, false);
|
493
|
+
|
494
|
+
this.container.addEventListener("touchmove", function(evt){
|
495
|
+
evt.preventDefault();
|
496
|
+
that.handleEvent(evt);
|
497
|
+
}, false);
|
498
|
+
|
499
|
+
this.container.addEventListener("touchend", function(evt){
|
500
|
+
evt.preventDefault();
|
501
|
+
that.touchEnd = true;
|
502
|
+
that.handleEvent(evt);
|
503
|
+
}, false);
|
383
504
|
};
|
384
505
|
/*
|
385
506
|
* get mouse position for desktop apps
|
386
507
|
*/
|
387
|
-
Kinetic.Stage.prototype.getMousePos = function(evt)
|
388
|
-
|
508
|
+
Kinetic.Stage.prototype.getMousePos = function(evt){
|
509
|
+
return this.mousePos;
|
389
510
|
};
|
390
511
|
/*
|
391
512
|
* get touch position for mobile apps
|
392
513
|
*/
|
393
|
-
Kinetic.Stage.prototype.getTouchPos = function(evt)
|
394
|
-
|
514
|
+
Kinetic.Stage.prototype.getTouchPos = function(evt){
|
515
|
+
return this.touchPos;
|
395
516
|
};
|
396
517
|
/*
|
397
518
|
* set mouse positon for desktop apps
|
398
519
|
*/
|
399
|
-
Kinetic.Stage.prototype.setMousePosition = function(evt)
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
520
|
+
Kinetic.Stage.prototype.setMousePosition = function(evt){
|
521
|
+
var mouseX = evt.clientX - this.getContainerPos().left + window.pageXOffset;
|
522
|
+
var mouseY = evt.clientY - this.getContainerPos().top + window.pageYOffset;
|
523
|
+
this.mousePos = {
|
524
|
+
x: mouseX,
|
525
|
+
y: mouseY
|
526
|
+
};
|
406
527
|
};
|
407
528
|
/*
|
408
529
|
* set touch position for mobile apps
|
409
530
|
*/
|
410
|
-
Kinetic.Stage.prototype.setTouchPosition = function(evt)
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
531
|
+
Kinetic.Stage.prototype.setTouchPosition = function(evt){
|
532
|
+
if (evt.touches !== undefined && evt.touches.length == 1) {// Only deal with
|
533
|
+
// one finger
|
534
|
+
var touch = evt.touches[0];
|
535
|
+
// Get the information for finger #1
|
536
|
+
var touchX = touch.clientX - this.getContainerPos().left + window.pageXOffset;
|
537
|
+
var touchY = touch.clientY - this.getContainerPos().top + window.pageYOffset;
|
538
|
+
|
539
|
+
this.touchPos = {
|
540
|
+
x: touchX,
|
541
|
+
y: touchY
|
542
|
+
};
|
543
|
+
}
|
423
544
|
};
|
424
545
|
/*
|
425
546
|
* get container position
|
426
547
|
*/
|
427
|
-
Kinetic.Stage.prototype.getContainerPos = function()
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
548
|
+
Kinetic.Stage.prototype.getContainerPos = function(){
|
549
|
+
var obj = this.container;
|
550
|
+
var top = 0;
|
551
|
+
var left = 0;
|
552
|
+
while (obj.tagName != "BODY") {
|
553
|
+
top += obj.offsetTop;
|
554
|
+
left += obj.offsetLeft;
|
555
|
+
obj = obj.offsetParent;
|
556
|
+
}
|
557
|
+
return {
|
558
|
+
top: top,
|
559
|
+
left: left
|
560
|
+
};
|
440
561
|
};
|
441
562
|
/*
|
442
563
|
* get container DOM element
|
443
564
|
*/
|
444
|
-
Kinetic.Stage.prototype.getContainer = function()
|
445
|
-
|
565
|
+
Kinetic.Stage.prototype.getContainer = function(){
|
566
|
+
return this.container;
|
446
567
|
};
|
447
568
|
/*
|
448
569
|
* get all shapes
|
449
570
|
*/
|
450
|
-
Kinetic.Stage.prototype.getShapes = function()
|
451
|
-
|
571
|
+
Kinetic.Stage.prototype.getShapes = function(){
|
572
|
+
return (this.getPropsLayer().getShapes()).concat(this.getActorsLayer().getShapes());
|
452
573
|
};
|
453
574
|
/****************************************
|
454
575
|
* Shape
|
455
576
|
*/
|
456
|
-
Kinetic.Shape = function(drawFunc, isProp)
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
577
|
+
Kinetic.Shape = function(drawFunc, isProp){
|
578
|
+
this.drawFunc = drawFunc;
|
579
|
+
this.isProp = isProp === undefined ? false : isProp;
|
580
|
+
this.x = 0;
|
581
|
+
this.y = 0;
|
582
|
+
this.scale = {
|
583
|
+
x: 1,
|
584
|
+
y: 1
|
585
|
+
};
|
586
|
+
this.rotation = 0;
|
587
|
+
// radians
|
588
|
+
// store state for next clear
|
589
|
+
this.lastX = 0;
|
590
|
+
this.lastY = 0;
|
591
|
+
this.lastRotation = 0;
|
592
|
+
// radians
|
593
|
+
this.lastScale = {
|
594
|
+
x: 1,
|
595
|
+
y: 1
|
596
|
+
};
|
597
|
+
|
598
|
+
this.eventListeners = {};
|
599
|
+
this.mouseOver = false;
|
600
|
+
this.clickStart = false;
|
601
|
+
this.inDblClickWindow = false;
|
602
|
+
|
603
|
+
this.visible = true;
|
483
604
|
};
|
484
605
|
/*
|
485
606
|
* get stage
|
486
607
|
*/
|
487
|
-
Kinetic.Shape.prototype.getStage = function()
|
488
|
-
|
608
|
+
Kinetic.Shape.prototype.getStage = function(){
|
609
|
+
return this.stage;
|
489
610
|
};
|
490
611
|
/*
|
491
612
|
* draw shape
|
492
613
|
*/
|
493
|
-
Kinetic.Shape.prototype.draw = function(layer)
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
614
|
+
Kinetic.Shape.prototype.draw = function(layer){
|
615
|
+
if (this.visible) {
|
616
|
+
var stage = this.getStage();
|
617
|
+
var context = layer.getContext();
|
618
|
+
|
619
|
+
context.save();
|
620
|
+
|
621
|
+
if (this.x !== 0 || this.y !== 0) {
|
622
|
+
context.translate(this.x, this.y);
|
623
|
+
}
|
624
|
+
if (this.rotation !== 0) {
|
625
|
+
context.rotate(this.rotation);
|
626
|
+
}
|
627
|
+
if (this.scale.x != 1 || this.scale.y != 1) {
|
628
|
+
context.scale(this.scale.x, this.scale.y);
|
629
|
+
}
|
630
|
+
|
631
|
+
this.drawFunc.call(layer);
|
632
|
+
context.restore();
|
633
|
+
}
|
634
|
+
};
|
635
|
+
/*
|
636
|
+
* enable/disable drag and drop
|
637
|
+
*/
|
638
|
+
Kinetic.Shape.prototype.draggable = function(setDraggable){
|
639
|
+
if (setDraggable) {
|
640
|
+
var that = this;
|
641
|
+
this.addEventListener("mousedown.initdrag", function(evt){
|
642
|
+
var stage = that.getStage();
|
643
|
+
stage.shapeDragging = that;
|
644
|
+
var mousePos = stage.getMousePos();
|
645
|
+
|
646
|
+
stage.shapeDragging.offset = {};
|
647
|
+
stage.shapeDragging.offset.x = mousePos.x - that.x;
|
648
|
+
stage.shapeDragging.offset.y = mousePos.y - that.y;
|
649
|
+
|
650
|
+
// execute dragstart events if defined
|
651
|
+
var dragstart = that.eventListeners.ondragstart;
|
652
|
+
if (dragstart) {
|
653
|
+
for (var i = 0; i < dragstart.length; i++) {
|
654
|
+
dragstart[i].func(evt);
|
655
|
+
}
|
656
|
+
}
|
657
|
+
});
|
658
|
+
}
|
659
|
+
else {
|
660
|
+
this.removeEventListener("mousedown.initdrag");
|
661
|
+
}
|
662
|
+
};
|
663
|
+
/*
|
664
|
+
* set shape scale
|
665
|
+
*/
|
666
|
+
Kinetic.Shape.prototype.setScale = function(scale){
|
667
|
+
this.scale.x = scale;
|
668
|
+
this.scale.y = scale;
|
669
|
+
};
|
670
|
+
/*
|
671
|
+
* scale shape
|
672
|
+
*/
|
673
|
+
Kinetic.Shape.prototype.scale = function(scale){
|
674
|
+
this.scale.x *= scale;
|
675
|
+
this.scale.y *= scale;
|
676
|
+
};
|
677
|
+
/*
|
678
|
+
* move shape to position
|
679
|
+
*/
|
680
|
+
Kinetic.Shape.prototype.setPosition = function(x, y){
|
681
|
+
this.x = x;
|
682
|
+
this.y = y;
|
683
|
+
};
|
684
|
+
/*
|
685
|
+
* move shape by amount
|
686
|
+
*/
|
687
|
+
Kinetic.Shape.prototype.move = function(x, y){
|
688
|
+
this.x += x;
|
689
|
+
this.y += y;
|
513
690
|
};
|
514
691
|
/*
|
515
|
-
* set shape
|
692
|
+
* set shape rotation
|
516
693
|
*/
|
517
|
-
Kinetic.Shape.prototype.
|
518
|
-
|
519
|
-
this.scale.y = scale;
|
694
|
+
Kinetic.Shape.prototype.setRotation = function(theta){
|
695
|
+
this.rotation = theta;
|
520
696
|
};
|
521
697
|
/*
|
522
|
-
*
|
698
|
+
* rotate shape by amount
|
523
699
|
*/
|
524
|
-
Kinetic.Shape.prototype.
|
525
|
-
|
526
|
-
|
700
|
+
Kinetic.Shape.prototype.rotate = function(theta){
|
701
|
+
this.rotation += theta;
|
702
|
+
};
|
703
|
+
/*
|
704
|
+
* short-hand add event listener to shape
|
705
|
+
*/
|
706
|
+
Kinetic.Shape.prototype.on = function(type, func){
|
707
|
+
var event = (type.indexOf('touch') == -1) ? 'on' + type : type;
|
708
|
+
var parts = event.split(".");
|
709
|
+
var baseEvent = parts[0];
|
710
|
+
var name = parts.length > 1 ? parts[1] : "";
|
711
|
+
|
712
|
+
if (!this.eventListeners[baseEvent]) {
|
713
|
+
this.eventListeners[baseEvent] = [];
|
714
|
+
}
|
715
|
+
|
716
|
+
this.eventListeners[baseEvent].push({
|
717
|
+
name: name,
|
718
|
+
func: func
|
719
|
+
});
|
720
|
+
};
|
721
|
+
/*
|
722
|
+
* long-hand add event listener to shape
|
723
|
+
*/
|
724
|
+
Kinetic.Shape.prototype.addEventListener = function(type, func){
|
725
|
+
this.on(type, func);
|
726
|
+
};
|
727
|
+
/*
|
728
|
+
* short-hand remove event listener(s)
|
729
|
+
*/
|
730
|
+
Kinetic.Shape.prototype.off = function(type){
|
731
|
+
var event = (type.indexOf('touch') == -1) ? 'on' + type : type;
|
732
|
+
var parts = event.split(".");
|
733
|
+
var baseEvent = parts[0];
|
734
|
+
|
735
|
+
if (parts.length > 1) {
|
736
|
+
var name = parts[1];
|
737
|
+
|
738
|
+
for (var i = 0; i < this.eventListeners[baseEvent].length; i++) {
|
739
|
+
if (this.eventListeners[baseEvent][i].name == name) {
|
740
|
+
this.eventListeners[baseEvent].splice(i, 1);
|
741
|
+
if (this.eventListeners[baseEvent].length == 0) {
|
742
|
+
this.eventListeners[baseEvent] = undefined;
|
743
|
+
}
|
744
|
+
break;
|
745
|
+
}
|
746
|
+
}
|
747
|
+
}
|
748
|
+
else {
|
749
|
+
this.eventListeners[baseEvent] = undefined;
|
750
|
+
}
|
751
|
+
};
|
752
|
+
/*
|
753
|
+
* long-hand remove event listener(s)
|
754
|
+
*/
|
755
|
+
Kinetic.Shape.prototype.removeEventListener = function(type){
|
756
|
+
this.off(type);
|
527
757
|
};
|
528
758
|
/*
|
529
759
|
* show shape
|
530
760
|
*/
|
531
|
-
Kinetic.Shape.prototype.show = function()
|
532
|
-
|
761
|
+
Kinetic.Shape.prototype.show = function(){
|
762
|
+
this.visible = true;
|
533
763
|
};
|
534
764
|
/*
|
535
765
|
* hide shape
|
536
766
|
*/
|
537
|
-
Kinetic.Shape.prototype.hide = function()
|
538
|
-
|
767
|
+
Kinetic.Shape.prototype.hide = function(){
|
768
|
+
this.visible = false;
|
539
769
|
};
|
540
770
|
/*
|
541
771
|
* move shape to top
|
542
772
|
*/
|
543
|
-
Kinetic.Shape.prototype.moveToTop = function()
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
773
|
+
Kinetic.Shape.prototype.moveToTop = function(){
|
774
|
+
var stage = this.stage;
|
775
|
+
var layer = this.getLayer();
|
776
|
+
var shapes = layer.getShapes();
|
777
|
+
|
778
|
+
for (var n = 0; n < shapes.length; n++) {
|
779
|
+
var shape = shapes[n];
|
780
|
+
if (shape.id == this.id) {
|
781
|
+
layer.shapes.splice(n, 1);
|
782
|
+
layer.shapes.push(this);
|
783
|
+
break;
|
784
|
+
}
|
785
|
+
}
|
786
|
+
};
|
787
|
+
/*
|
788
|
+
* get shape layer
|
789
|
+
*/
|
790
|
+
Kinetic.Shape.prototype.getLayer = function(){
|
791
|
+
return this.layer;
|
556
792
|
};
|
557
793
|
/*
|
558
794
|
* get shape layer
|
559
795
|
*/
|
560
|
-
Kinetic.Shape.prototype.
|
561
|
-
|
796
|
+
Kinetic.Shape.prototype.makeActor = function(){
|
797
|
+
var stage = this.stage;
|
798
|
+
var layer = this.getLayer();
|
799
|
+
var propsLayer = stage.getPropsLayer();
|
800
|
+
var actorsLayer = stage.getActorsLayer();
|
801
|
+
var shapes = layer.getShapes();
|
802
|
+
|
803
|
+
for (var n = 0; n < shapes.length; n++) {
|
804
|
+
var shape = shapes[n];
|
805
|
+
if (shape.id == this.id) {
|
806
|
+
layer.shapes.splice(n, 1);
|
807
|
+
actorsLayer.getShapes().push(this);
|
808
|
+
break;
|
809
|
+
}
|
810
|
+
}
|
811
|
+
};
|
812
|
+
/*
|
813
|
+
* make shape into an actor
|
814
|
+
*/
|
815
|
+
Kinetic.Shape.prototype.makeActor = function(){
|
816
|
+
var stage = this.stage;
|
817
|
+
var layer = this.getLayer();
|
818
|
+
var propsLayer = stage.getPropsLayer();
|
819
|
+
var actorsLayer = stage.getActorsLayer();
|
820
|
+
var shapes = layer.getShapes();
|
821
|
+
|
822
|
+
for (var n = 0; n < shapes.length; n++) {
|
823
|
+
var shape = shapes[n];
|
824
|
+
if (shape.id == this.id) {
|
825
|
+
layer.shapes.splice(n, 1);
|
826
|
+
actorsLayer.getShapes().push(this);
|
827
|
+
this.layer = stage.actorsLayer;
|
828
|
+
break;
|
829
|
+
}
|
830
|
+
}
|
831
|
+
};
|
832
|
+
/*
|
833
|
+
* make shape into a prop
|
834
|
+
*/
|
835
|
+
Kinetic.Shape.prototype.makeProp = function(){
|
836
|
+
var stage = this.stage;
|
837
|
+
var layer = this.getLayer();
|
838
|
+
var propsLayer = stage.getPropsLayer();
|
839
|
+
var actorsLayer = stage.getActorsLayer();
|
840
|
+
var shapes = layer.getShapes();
|
841
|
+
for (var n = 0; n < shapes.length; n++) {
|
842
|
+
var shape = shapes[n];
|
843
|
+
if (shape.id == this.id) {
|
844
|
+
layer.shapes.splice(n, 1);
|
845
|
+
propsLayer.getShapes().push(this);
|
846
|
+
this.layer = stage.propsLayer;
|
847
|
+
break;
|
848
|
+
}
|
849
|
+
}
|
562
850
|
};
|
563
851
|
/****************************************
|
564
|
-
*
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
852
|
+
* Image constructor
|
853
|
+
*/
|
854
|
+
Kinetic.Image = function(config){
|
855
|
+
var imageObj = config.imageObj;
|
856
|
+
var x = config.x;
|
857
|
+
var y = config.y;
|
858
|
+
var width = config.width;
|
859
|
+
var height = config.height;
|
860
|
+
var isProp = config.isProp;
|
861
|
+
|
862
|
+
if (!width) {
|
863
|
+
width = imageObj.width;
|
864
|
+
}
|
865
|
+
if (!height) {
|
866
|
+
height = imageObj.height;
|
867
|
+
}
|
868
|
+
var drawImage = function(){
|
869
|
+
var context = this.getContext();
|
870
|
+
context.drawImage(imageObj, x, y, width, height);
|
871
|
+
context.beginPath();
|
872
|
+
context.rect(x, y, width, height);
|
873
|
+
context.closePath();
|
874
|
+
};
|
875
|
+
|
876
|
+
var shape = new Kinetic.Shape(drawImage, isProp);
|
877
|
+
|
878
|
+
/*
|
879
|
+
* copy shape methods and properties to
|
880
|
+
* Image object
|
881
|
+
*/
|
882
|
+
for (var key in shape) {
|
883
|
+
this[key] = shape[key];
|
884
|
+
}
|
582
885
|
};
|