File size: 4,294 Bytes
c2a5d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class OrientedCursor{
 
    constructor(elementID){
      
      this.elementID = elementID;
      this.tiltX = 0;
      this.tiltY = 0;
      this.pressure = 0;
      this.diameter = 0;
      
      this.targetAngle = 0;
      
      this.isOnCanvas = false;
    }
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    catchCursor(){
      let getCanvas = document.getElementById(this.elementID);
      
      getCanvas.addEventListener("pointermove", (e) => {
        //console.log("pointerMove");
    
        if (this.isOnCanvas) {
          this.tiltX = e.tiltX;
          this.tiltY = e.tiltY;
          this.pressure = e.pressure;
        
          //console.log(inclinationX + ' ' + inclinationY + ' ' + pressure);
        }
      }, false);
  
      getCanvas.addEventListener("pointerdown", (e) => {
        //console.log("pointerDown");
        getCanvas.setPointerCapture(e.pointerId);
        this.isOnCanvas = true;
    
        this.tiltX = e.tiltX;
        this.tiltY = e.tiltY;
        this.pressure = e.pressure;
      
      }, false);
  
      getCanvas.addEventListener("pointerup", (e) => {
        //console.log("pointerUp");
      
        if (this.isOnCanvas) {
          getCanvas.releasePointerCapture(e.pointerId);
          this.isOnCanvas = false;
      
          this.tiltX = e.tiltX;
          this.tiltY = e.tiltY;
          this.pressure = e.pressure;
      
          //console.log(inclinationX + ' ' + inclinationY + ' ' + pressure);
        
        }
      }, false);
    }
    
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    calculateAngle(){
      this.targetAngle = atan2(this.tiltY, this.tiltX);
    }
    
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    showData(){
    // LIVE COORDINATES
    push();
      noFill();
      stroke('#fff')
      text('pressure: ' + this.pressure, 10, 30);
      text('tilt_X: ' + this.tiltX, 10, 50);
      text('tilt_Y: ' + this.tiltY, 10, 70);
      text('angle arctan: ' + this.targetAngle, 10, 90);
    pop();
    }
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    mapPressure(){
      this.diameter = map(this.pressure, 0, 1, 1, 3);
    }
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    process_rotate(){
      translate(mouseX, mouseY); //mouseX & mouseY
      rotate(this.targetAngle);
      translate(-mouseX, -mouseY); // -mouseX & -mouseY
    }
    
    // -----------------------------------------
    // -----------------------------------------
    
    
    showCursor(mouseX, mouseY){
      // POINTER CENTER   
      push();
        noStroke();
        fill(0, 0, 0);
        circle(mouseX, mouseY, 20);
      pop();
      
      // RECTANGLE SHAPE
      push();
        this.process_rotate()
  
        noFill();
        stroke(2)
        rectMode(CENTER)
        rect(mouseX, mouseY, this.diameter, 30); // reacts to pen pressure value
      
        noStroke();
        fill('yellow');
        circle(mouseX, mouseY, 10); // shows the pivot point 
      pop();
     
      // POINTS FROM STYLUS AT GOOD INCLINATION & PRESSURE VALUE
      push();
        this.process_rotate();
        noFill();
        stroke(1);
        ellipseMode(CENTER);
        circle(mouseX, mouseY + this.diameter, 10); // LEFT  || WEST
        circle(mouseX + this.diameter, mouseY, 10);// DOWN  || SOUTH
        circle(mouseX, mouseY - this.diameter, 10); // RIGHT || EAST
        circle(mouseX - this.diameter, mouseY, 10); // UP    || NORTH
      
      
      pop();
    
      circle(mouseX + this.diameter/4 * cos(this.targetAngle), mouseY + this.diameter/4 * sin(this.targetAngle), 1)
      circle(mouseX + this.diameter/4 * cos(this.targetAngle + PI), mouseY + this.diameter/4 * sin(this.targetAngle+ PI), 1)
    
   
    // TILT AXIS & LENGTH
    push();
      fill('red');
      circle(mouseX + this.tiltX, mouseY + this.tiltY, 10);
    
    pop();
    
    push();
      fill('blue'); 
      circle(mouseX - this.tiltX, mouseY - this.tiltY, 10);
    pop();
    }
    
  }