-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedObject.js
129 lines (107 loc) · 3.34 KB
/
AnimatedObject.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
/* Animated Object Class */
/* create a new animated object */
function AnimatedObject(name, imageFile) {
this.number = app.animationArea.animatedObjects.length + 1;
this.objectName = name;
// these are hardcoded defaults for now
this.sprites = [];
this.animation = [];
this.objectController = null;
this.imageFile = imageFile;
this.canvasElement = null;
this.addToAnimatedArea();
this.paused = false; // might delete this
}
AnimatedObject.prototype.addToCanvas = function(img) {
debugger;
var kineticImage = new Kinetic.Image({
x: 0,
y: 0,
image: img,
draggable: true,
id: this.objectName
});
// add the layer to the stage
Kinetic.getStage.add(kineticImage);
// save kinetic object to object
this.canvasElement = kineticImage;
this.bindMovementEvents();
}
AnimatedObject.prototype.addToAnimatedArea = function() {
var animatedObject = this;
var img = new Image();
img.src =this.imageFile;
img.onload = this.addToCanvas(img);
this.createController();
}
AnimatedObject.prototype.bindMovementEvents = function() {
var animatedObject = this;
var movementStartTime = 0;
this.canvasElement.on('dragstart', function(event){
movementStartTime = event.timeStamp;
})
this.canvasElement.on('dragmove', function(event){
movement = {
top: event.y,
left: event.x,
deltaTimestamp: event.timeStamp - movementStartTime
}
animatedObject.recordMovement(movement);
})
}
AnimatedObject.prototype.createController = function() {
// create a controller for this animated object
var objectControllerId = this.objectName + 'Controller';
// could not use app var here and just go straight to DOM. tradeoffs?
app.objectsListControl.append('<li id="' + objectControllerId + '">'+ this.number + ") " + this.objectName + '</li>');
this.objectController = $('#'+ objectControllerId);
var animatedObj = this;
// bind a click selection event to the controller
this.objectController.bind('click', function() {
console.log('clicked '+ objectControllerId);
animatedObj.select();
});
}
/* written for DOM, not kinetic.js */
AnimatedObject.prototype.select = function() {
// deselect and disable all draggable objects
$('#objectsList li').removeClass('selected');
$('.ui-draggable').draggable('disable');
// and only select and enable this one
this.objectController.addClass('selected');
this.imageElement.draggable('enable');
}
AnimatedObject.prototype.addSprite = function(params) {
}
AnimatedObject.prototype.playAnimation = function() {
console.log('play');
this.paused = false;
var numMovements = this.animation.length;
// move to original position
this.canvasElement.x = this.animation[0]['x'];
this.canvasElement.y = this.animation[0]['y'];
var i = 1;
while (i < numMovements) {
var movement = this.animation[i];
var lastMovement = this.animation[i-1];
var duration = movement['deltaTimestamp'] - lastMovement['deltaTimestamp'];
movement['duration'] = duration;
if (this.paused === false) {
this.performMovement(movement);
}
i++;
}
}
AnimatedObject.prototype.pauseAnimation = function() {
this.paused = true;
}
AnimatedObject.prototype.performMovement = function(movement) {
this.canvasElement.animate({
left: movement['x'],
top: movement['y'],
duration: movement['duration'],
});
}
AnimatedObject.prototype.recordMovement = function(movement) {
this.animation.push(movement);
}