CanvasAnimator.js
1.9 KB
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
/**
* CanvasAnimator
* manages smooth cropping animation
*
* This object is called internally to manage animation.
* An in-memory div is animated and a progress callback
* is used to update the selection coordinates of the
* visible selection in realtime.
*/
var CanvasAnimator = function(stage){
this.stage = stage;
this.core = stage.core;
this.cloneStagePosition();
};
CanvasAnimator.prototype = {
cloneStagePosition: function(){
var s = this.stage;
this.angle = s.angle;
this.scale = s.scale;
this.offset = s.offset;
},
getElement: function(){
var s = this.stage;
return $('<div />')
.css({
position: 'absolute',
top: s.offset[0]+'px',
left: s.offset[1]+'px',
width: s.angle+'px',
height: s.scale+'px'
});
},
animate: function(cb){
var t = this;
this.scale = this.stage.boundScale(this.scale);
t.stage.triggerEvent('croprotstart');
t.getElement().animate({
top: t.offset[0]+'px',
left: t.offset[1]+'px',
width: t.angle+'px',
height: t.scale+'px'
},{
easing: t.core.opt.animEasing,
duration: t.core.opt.animDuration,
complete: function(){
t.stage.triggerEvent('croprotend');
(typeof cb == 'function') && cb.call(this);
},
progress: function(anim){
var props = {}, i, tw = anim.tweens;
for(i=0;i<tw.length;i++){
props[tw[i].prop] = tw[i].now; }
t.stage.setAngle(props.width)
.setScale(props.height)
.setOffset(props.top,props.left)
.redraw();
}
});
}
};
Jcrop.stage.Canvas.prototype.getAnimator = function(){
return new CanvasAnimator(this);
};
Jcrop.registerComponent('CanvasAnimator',CanvasAnimator);