Blame view

mockup/mockup1.html 6.54 KB
3a3241a5e   Quillot Mathias   first file of moc...
1
2
  <html>
      <head>
8a33df386   Quillot Mathias   Charset UTF-8
3
          <meta charset="UTF-8">
9db1a6bb0   Quillot Mathias   First usable mock...
4
          <title>Konva's test</title>
eff4e3732   Quillot Mathias   Add Konva JS to t...
5
          <script src="https://unpkg.com/konva@3.3.2/konva.min.js"></script>   
3a3241a5e   Quillot Mathias   first file of moc...
6
7
      </head>
      <body>
9db1a6bb0   Quillot Mathias   First usable mock...
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
          <h1>Interface Mockup with Konda JS</h1>
          <div id="container"></div>
  
  <script type="text/javascript">
  
  // -- FUNCTIONS -- 
  /**
   * Check if an intersection exists between AB and CD. 
   * Return : 
   *  - false if it has no intersection
   *  - true otherwise 
   */
  function isThereIntersection(A, B, C, D) {
      a = A.x;b = A.y;c = B.x;d = B.y
      p = C.x;q = C.y;r = D.x;s = D.y
      var det, gamma, lambda;
      det = (c - a) * (s - q) - (r - p) * (d - b);
      if (det === 0) {
          return false;
      } else {
          lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
          gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
          return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1)
      }
  };
  
  /**
   * Compute intersection between two lines
   * Please, check if an intersection exists between
   * calling this function. (ref isThereIntersection)
   * 
   * Return:
   * - Intersection point between two lines
   */
  function intersection(A, B, C, D) {
      a = A.x;b = A.y;c = B.x;d = B.y
      p = C.x;q = C.y;r = D.x;s = D.y
  
      l1_a = (B.y - A.y) / (B.x - A.x);
      l1_b = A.y - l1_a * A.x;
      l2_a = (D.y - C.y) / (D.x - C.x);
      l2_b = C.y - l2_a * C.x;
  
      // Compute intersection point
      x = (l2_b - l1_b) / (l1_a - l2_a)        
      y = l1_a * x + l1_b
      return {
          x: x,
          y: y 
      } 
  }
  
  /**
   * Return the index of the closest point
   * 
   */
  function closestPoint(ref, points) {
      var dist = 100000;
      var closest_i = -1;
      for(var i in points) {
          cur = Math.sqrt(Math.pow(ref.x - points[i].x, 2) + Math.pow(ref.y - points[i].y, 2));
          if(cur < dist) {
              dist = cur;
              closest_i = i;
          }
      }
      return closest_i;
  }
  // -- DATA --
  data = {
      triangle: {
          A: {
              x:170,
              y: 80
          },
          B: {
              x: 365,
              y: 125
          },
          C: {
              x: 215, 
              y: 260
          }
      }
  }
  
  // -- Help functions --
  function writeMessage(message) {
      text.text(message);
      layer.draw();
  }
  
  // -- STAGE & LAYER--
  var stage = new Konva.Stage({
    container: 'container',   // id of container <div>
    width: 500,
    height: 500
  });
  
  var layer = new Konva.Layer();
  
  // -- TEXT --
  var text = new Konva.Text({
      x: 10,
      y: 10,
      fontFamily: 'Calibri',
      fontSize: 24,
      text: 'Test',
      fill: 'black'
  });
  
  
  // -- RECTANGLE --
  // TODO: Ajouter un rectangle en fond qui serve à 
  // gérer l'événement de la souris.
  
  var rect = new Konva.Rect({
      x: 0,
      y: 0,
      width: stage.attrs.width,
      height: stage.attrs.height,
      stroke: 'black',
      strokeWidth: 0
  });
  
  
  // -- TRIANGLE --
  var triangle = new Konva.Shape({
      stroke: 'black',
      fill: '#00D2FF',
      strokeWidth: 1,
      sceneFunc: function(context) {
          context.beginPath();
          context.moveTo(data.triangle.A.x, data.triangle.A.y);
          context.lineTo(data.triangle.B.x, data.triangle.B.y);
          context.lineTo(data.triangle.C.x, data.triangle.C.y);
          context.closePath();
          context.fillStrokeShape(this);50
      }
  });
  
  bar = {
      x: (data.triangle.A.x + data.triangle.B.x + data.triangle.C.x) / 3,
      y: (data.triangle.A.y + data.triangle.B.y + data.triangle.C.y) / 3
  }
  
  
  triangle.on("mousemove mousedown", function(event){
      if(event.evt.buttons === 1) {
          var mousePos = stage.getPointerPosition();
          var x = mousePos.x;
          var y = mousePos.y;
          point.position({x: x, y: y});
          layer.draw();
      }
  })
  
  function eventOutTriangle(event) {
      if(event.evt.buttons === 1) {
          var mousePos = stage.getPointerPosition();
          var pointout = {
              x: mousePos.x,
              y: mousePos.y
          }
          var lines_to_test = [
              {
                  A: data.triangle.A, 
                  B: data.triangle.B
              },
              {
                  A: data.triangle.B,
                  B: data.triangle.C
              },
              {
                  A: data.triangle.C,
                  B: data.triangle.A
              }
          ]
  
          var intersections = []
          for(var ind in lines_to_test) {
              line = lines_to_test[ind];
              var inter = isThereIntersection(
                  line.A, line.B,
                  bar, pointout
              )
              if(inter !== false) {
                  intersections.push(
                      intersection(
                          line.A, line.B,
                          bar, pointout
                      )
                  );
              }
          }
          i = closestPoint(pointout, intersections);
          point.position({
              x: intersections[i].x, 
              y: intersections[i].y
          });
          layer.draw();
      }
  }
  
  rect.on("mousemove", function(event) {
      eventOutTriangle(event);
  })
  
  // -- POINT --
  // create our shape
  var point = new Konva.Circle({
    x: bar.x,
    y: bar.y,
    radius: 5,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 1
  });
  
  point.listening(false);
  
  
  // -- IMAGE REFS --
  // 1rst ref
  var ref1Obj = new Image();
  ref1Obj.onload = function() {
      var ref1 = new Konva.Image({
          x: 50,
          y: 50,
          image: ref1Obj,
          width: 100,
          height: 100
      });
  
      ref1.on("mousemove", function(event) {
          eventOutTriangle(event);
      });
  
      layer.add(ref1);
      layer.draw();
  }
  ref1Obj.src="https://pixel.nymag.com/imgs/daily/vulture/2017/11/01/01-thor-ragnarok.w700.h700.jpg";
  
  // 2nd ref
  var ref2Obj = new Image();
  ref2Obj.onload = function() {
      var ref2 = new Konva.Image({
          x: 150,
          y: 300,
          image: ref1Obj,
          width: 100,
          height: 100
      });
  
      ref2.on("mousemove", function(event) {
          eventOutTriangle(event);
      });
  
      layer.add(ref2);
      layer.draw();
  }
  ref2Obj.src="https://pixel.nymag.com/imgs/daily/vulture/2017/11/01/01-thor-ragnarok.w700.h700.jpg";
  
  
  // 3rd ref
  var ref3Obj = new Image();
  ref3Obj.onload = function() {
      var ref3 = new Konva.Image({
          x: 390,
          y: 50,
          image: ref3Obj,
          width: 100,
          height: 100
      });
  
      ref3.on("mousemove", function(event) {
          eventOutTriangle(event);
      });
  
      layer.add(ref3);
      layer.draw();
  }
  ref3Obj.src="https://pixel.nymag.com/imgs/daily/vulture/2017/11/01/01-thor-ragnarok.w700.h700.jpg";
  
  
  // -- ADDING
  // add the shape to the layer
  layer.add(rect);
  layer.add(triangle);
  layer.add(point);
  layer.add(text);
  
  
  // add the layer to the stage
  stage.add(layer);
  
  
  // -- DRAWING
  layer.draw();
  </script>
3a3241a5e   Quillot Mathias   first file of moc...
308
309
      </body>
  </html>