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.
@@ -1,9 +1,9 @@
1
1
  /**
2
- * KineticJS JavaScript Library v3.1.0
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 19 2011
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, stripStyling) {
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(stripStyling) {
42
- this.context.stroke = function() {
43
- };
44
- this.context.fill = function() {
45
- };
46
- }
47
-
48
- stage.container.appendChild(this.canvas);
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
- var context = this.getContext();
55
- var canvas = this.getCanvas();
56
- context.clearRect(0, 0, canvas.width, canvas.height);
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
- return this.canvas;
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
- return this.context;
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
- return this.shapes;
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
- this.clear();
81
- var context = this.getContext();
82
-
83
- for(var n = 0; n < this.getShapes().length; n++) {
84
- this.getShapes()[n].draw(this);
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
- this.container = document.getElementById(containerId);
92
- this.width = width;
93
- this.height = height;
94
- this.zIndexCounter = 9999;
95
- this.idCounter = 0;
96
- this.dblClickWindow = 400;
97
- // ms
98
- // desktop flags
99
- this.mousePos = null;
100
- this.mouseDown = false;
101
- this.mouseUp = false;
102
-
103
- // mobile flags
104
- this.touchPos = null;
105
- this.touchStart = false;
106
- this.touchEnd = false;
107
-
108
- // create layers
109
- this.backStageLayer = new Kinetic.Layer(this, true);
110
- this.stageLayer = new Kinetic.Layer(this);
111
- this.propsLayer = new Kinetic.Layer(this);
112
- this.actorsLayer = new Kinetic.Layer(this);
113
- this.backStageLayer.getCanvas().style.display = 'none';
114
- this.listen();
115
- };
116
- /*
117
- * get back stage layer
118
- */
119
- Kinetic.Stage.prototype.getBackStageLayer = function() {
120
- return this.backStageLayer;
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
- return this.stageLayer;
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
- return this.propsLayer;
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
- return this.actorsLayer;
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
- this.stageLayer.clear();
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
- this.backStageLayer.clear();
151
- this.stageLayer.clear();
152
- this.propsLayer.clear();
153
- this.actorsLayer.clear();
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
- * short-hand for draw actors layer
235
+ * draw actors layer
157
236
  */
158
- Kinetic.Stage.prototype.draw = function() {
159
- this.getActorsLayer().draw();
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
- // remove from shapes array
166
- var shapes = this.getShapes();
167
- for(var n = 0; n < shapes.length; n++) {
168
- var id = shapes[n].id;
169
- if(id == shape.id) {
170
- shape.getLayer().getShapes().splice(n, 1);
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
- // remove all shapes
179
- this.getPropsLayer().shapes = [];
180
- this.getActorsLayer().shapes = [];
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
- return this.stageLayer.getCanvas();
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
- return this.stageLayer.getContext();
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.addEventListener = function(type, func) {
199
- this.container.addEventListener(type, func);
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
- shape.stage = this;
206
- if(shape.isProp) {
207
- shape.layer = this.propsLayer;
208
- } else {
209
- shape.layer = this.actorsLayer;
210
- }
211
-
212
- shape.getLayer().shapes.push(shape);
213
-
214
- shape.id = this.idCounter++;
215
- shape.draw(shape.layer);
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
- if(!evt) {
222
- evt = window.event;
223
- }
224
-
225
- this.setMousePosition(evt);
226
- this.setTouchPosition(evt);
227
-
228
- var backStageLayer = this.backStageLayer;
229
- var backStageLayerContext = backStageLayer.getContext();
230
- var that = this;
231
-
232
- backStageLayer.clear();
233
-
234
- for(var n = this.getShapes().length - 1; n >= 0; n--) {
235
- var pubShape = this.getShapes()[n]; ( function() {
236
- var shape = pubShape;
237
- shape.draw(backStageLayer);
238
- var pos = that.touchPos || that.mousePos;
239
- var el = shape.eventListeners;
240
-
241
- if(shape.visible && pos !== null && backStageLayerContext.isPointInPath(pos.x, pos.y)) {
242
- // handle onmousedown
243
- if(that.mouseDown) {
244
- that.mouseDown = false;
245
- shape.clickStart = true;
246
-
247
- if(el.onmousedown !== undefined) {
248
- el.onmousedown(evt);
249
- }
250
- n = -1;
251
- }
252
- // handle onmouseup & onclick
253
- else if(that.mouseUp) {
254
- that.mouseUp = false;
255
- if(el.onmouseup !== undefined) {
256
- el.onmouseup(evt);
257
- }
258
-
259
- // detect if click or double click occurred
260
- if(shape.clickStart) {
261
-
262
- if(el.onclick !== undefined) {
263
- el.onclick(evt);
264
- }
265
-
266
- if(el.ondblclick !== undefined && shape.inDoubleClickWindow) {
267
- el.ondblclick(evt);
268
- }
269
-
270
- shape.inDoubleClickWindow = true;
271
-
272
- setTimeout(function() {
273
- shape.inDoubleClickWindow = false;
274
- }, that.dblClickWindow);
275
- }
276
- n = -1;
277
- }
278
-
279
- // handle touchstart
280
- else if(that.touchStart) {
281
- that.touchStart = false;
282
- if(el.touchstart !== undefined) {
283
- el.touchstart(evt);
284
- }
285
- n = -1;
286
- }
287
-
288
- // handle touchend
289
- else if(that.touchEnd) {
290
- that.touchEnd = false;
291
- if(el.touchend !== undefined) {
292
- el.touchend(evt);
293
- }
294
- n = -1;
295
- }
296
-
297
- // handle touchmove
298
- else if(el.touchmove !== undefined) {
299
- el.touchmove(evt);
300
- n = -1;
301
- }
302
-
303
- // handle onmouseover
304
- else if(!shape.mouseOver) {
305
- shape.mouseOver = true;
306
- if(el.onmouseover !== undefined) {
307
- el.onmouseover(evt);
308
- }
309
- n = -1;
310
- }
311
-
312
- // handle onmousemove
313
- else if(el.onmousemove !== undefined) {
314
- el.onmousemove(evt);
315
- n = -1;
316
- }
317
- }
318
- // handle mouseout condition
319
- else if(shape.mouseOver) {
320
- shape.mouseOver = false;
321
- if(el.onmouseout !== undefined) {
322
- el.onmouseout(evt);
323
- }
324
- n = -1;
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
- var that = this;
335
-
336
- // desktop events
337
- this.container.addEventListener("mousedown", function(evt) {
338
- that.mouseDown = true;
339
- that.handleEvent(evt);
340
- }, false);
341
-
342
- this.container.addEventListener("mousemove", function(evt) {
343
- that.mouseUp = false;
344
- that.mouseDown = false;
345
- that.handleEvent(evt);
346
- }, false);
347
-
348
- this.container.addEventListener("mouseup", function(evt) {
349
- that.mouseUp = true;
350
- that.mouseDown = false;
351
- that.handleEvent(evt);
352
-
353
- // clear all click starts
354
- for(var i = 0; i < that.getShapes().length; i++) {
355
- that.getShapes()[i].clickStart = false;
356
- }
357
- }, false);
358
-
359
- this.container.addEventListener("mouseover", function(evt) {
360
- that.handleEvent(evt);
361
- }, false);
362
-
363
- this.container.addEventListener("mouseout", function(evt) {
364
- that.mousePos = null;
365
- }, false);
366
- // mobile events
367
- this.container.addEventListener("touchstart", function(evt) {
368
- evt.preventDefault();
369
- that.touchStart = true;
370
- that.handleEvent(evt);
371
- }, false);
372
-
373
- this.container.addEventListener("touchmove", function(evt) {
374
- evt.preventDefault();
375
- that.handleEvent(evt);
376
- }, false);
377
-
378
- this.container.addEventListener("touchend", function(evt) {
379
- evt.preventDefault();
380
- that.touchEnd = true;
381
- that.handleEvent(evt);
382
- }, false);
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
- return this.mousePos;
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
- return this.touchPos;
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
- var mouseX = evt.clientX - this.getContainerPos().left + window.pageXOffset;
401
- var mouseY = evt.clientY - this.getContainerPos().top + window.pageYOffset;
402
- this.mousePos = {
403
- x : mouseX,
404
- y : mouseY
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
- if(evt.touches !== undefined && evt.touches.length == 1) {// Only deal with
412
- // one finger
413
- var touch = evt.touches[0];
414
- // Get the information for finger #1
415
- var touchX = touch.clientX - this.getContainerPos().left + window.pageXOffset;
416
- var touchY = touch.clientY - this.getContainerPos().top + window.pageYOffset;
417
-
418
- this.touchPos = {
419
- x : touchX,
420
- y : touchY
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
- var obj = this.container;
429
- var top = 0;
430
- var left = 0;
431
- while(obj.tagName != "BODY") {
432
- top += obj.offsetTop;
433
- left += obj.offsetLeft;
434
- obj = obj.offsetParent;
435
- }
436
- return {
437
- top : top,
438
- left : left
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
- return this.container;
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
- return (this.getPropsLayer().getShapes()).concat(this.getActorsLayer().getShapes());
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
- this.drawFunc = drawFunc;
458
- this.isProp = isProp === undefined ? false : isProp;
459
- this.x = 0;
460
- this.y = 0;
461
- this.scale = {
462
- x : 1,
463
- y : 1
464
- };
465
- this.rotation = 0;
466
- // radians
467
- // store state for next clear
468
- this.lastX = 0;
469
- this.lastY = 0;
470
- this.lastRotation = 0;
471
- // radians
472
- this.lastScale = {
473
- x : 1,
474
- y : 1
475
- };
476
-
477
- this.eventListeners = {};
478
- this.mouseOver = false;
479
- this.clickStart = false;
480
- this.inDblClickWindow = false;
481
-
482
- this.visible = true;
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
- return this.stage;
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
- if(this.visible) {
495
- var stage = this.getStage();
496
- var context = layer.getContext();
497
-
498
- context.save();
499
-
500
- if(this.x !== 0 || this.y !== 0) {
501
- context.translate(this.x, this.y);
502
- }
503
- if(this.rotation !== 0) {
504
- context.rotate(this.rotation);
505
- }
506
- if(this.scale.x != 1 || this.scale.y != 1) {
507
- context.scale(this.scale.x, this.scale.y);
508
- }
509
-
510
- this.drawFunc.call(layer);
511
- context.restore();
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 canvas scale
692
+ * set shape rotation
516
693
  */
517
- Kinetic.Shape.prototype.setScale = function(scale) {
518
- this.scale.x = scale;
519
- this.scale.y = scale;
694
+ Kinetic.Shape.prototype.setRotation = function(theta){
695
+ this.rotation = theta;
520
696
  };
521
697
  /*
522
- * add event listener to shape
698
+ * rotate shape by amount
523
699
  */
524
- Kinetic.Shape.prototype.addEventListener = function(type, func) {
525
- var event = (type.indexOf('touch') == -1) ? 'on' + type : type;
526
- this.eventListeners[event] = func;
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
- this.visible = true;
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
- this.visible = false;
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
- var stage = this.stage;
545
- var layer = this.getLayer();
546
- var shapes = layer.getShapes();
547
-
548
- for(var n = 0; n < shapes.length; n++) {
549
- var shape = shapes[n];
550
- if(shape.id == this.id) {
551
- layer.shapes.splice(n, 1);
552
- layer.shapes.push(this);
553
- break;
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.getLayer = function() {
561
- return this.layer;
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
- * drawImage util
565
- * This util function draws a rectangular shape
566
- * over a canvas image to provide a detectable path
567
- */
568
- Kinetic.drawImage = function(imageObj, x, y, width, height) {
569
- if(!width) {
570
- width = imageObj.width;
571
- }
572
- if(!height) {
573
- height = imageObj.height;
574
- }
575
- return function() {
576
- var context = this.getContext();
577
- context.drawImage(imageObj, x, y, width, height);
578
- context.beginPath();
579
- context.rect(x, y, width, height);
580
- context.closePath();
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
  };