forked from barais/drawwebvallillajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction.js
74 lines (53 loc) · 1.9 KB
/
interaction.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
// La création d'un Dnd requière un canvas et un interacteur.
// L'interacteur viendra dans un second temps donc ne vous en souciez pas au départ.
function DnD(boundingObject, interactor) {
this.initX = 0;
this.initY = 0;
this.finalX = 0;
this.finalY = 0;
this.pression = false;
this.wait = true;
this.boundingObject = boundingObject;
this.interactor = interactor;//.onInteractionStart(this);
this.getInitX = function() {
return this.initX;
}.bind(this) ;
this.getInitY = function() {
return this.initY;
}.bind(this) ;
this.getFinalX = function() {
return this.finalX;
}.bind(this) ;
this.getFinalY = function() {
return this.finalY;
}.bind(this) ;
this.maFctGerantLaPression = function(evt) {
this.pression = true;
this.initX = getMousePosition(this.boundingObject,evt).x;
this.initY = getMousePosition(this.boundingObject,evt).y;
this.interactor.onInteractionStart(this);
}.bind(this) ;
this.maFctGerantLeDeplacement = function(evt) {
if (this.pression){
this.finalX = getMousePosition(this.boundingObject,evt).x;
this.finalY = getMousePosition(this.boundingObject,evt).y;
this.interactor.onInteractionUpdate(this);
}
}.bind(this) ;
this.maFctGerantLeRelachement = function(evt) {
this.pression = false;
this.finalX = getMousePosition(this.boundingObject,evt).x;
this.finalY = getMousePosition(this.boundingObject,evt).y;
this.interactor.onInteractionEnd(this);
}.bind(this) ;
boundingObject.addEventListener('mousedown', this.maFctGerantLaPression, false);
boundingObject.addEventListener('mousemove', this.maFctGerantLeDeplacement, false);
boundingObject.addEventListener('mouseup', this.maFctGerantLeRelachement, false);
};
function getMousePosition(can,evt) {
var rect = can.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
};