Procházet zdrojové kódy

Do not regenerate shapes when moving

vanilla_orig
Christopher Chedeau před 5 roky
rodič
revize
f8873dd56b
1 změnil soubory, kde provedl 17 přidání a 14 odebrání
  1. 17
    14
      src/index.js

+ 17
- 14
src/index.js Zobrazit soubor

@@ -39,30 +39,29 @@ function generateShape(element) {
39 39
       context.fillStyle = fillStyle;
40 40
     };
41 41
   } else if (element.type === "rectangle") {
42
-    const shape = generator.rectangle(
43
-      element.x,
44
-      element.y,
45
-      element.width,
46
-      element.height
47
-    );
42
+    const shape = generator.rectangle(0, 0, element.width, element.height);
48 43
     element.draw = (rc, context) => {
44
+      context.translate(element.x, element.y);
49 45
       rc.draw(shape);
46
+      context.translate(-element.x, -element.y);
50 47
     };
51 48
   } else if (element.type === "ellipse") {
52 49
     const shape = generator.ellipse(
53
-      element.x + element.width / 2,
54
-      element.y + element.height / 2,
50
+      element.width / 2,
51
+      element.height / 2,
55 52
       element.width,
56 53
       element.height
57 54
     );
58 55
     element.draw = (rc, context) => {
56
+      context.translate(element.x, element.y);
59 57
       rc.draw(shape);
58
+      context.translate(-element.x, -element.y);
60 59
     };
61 60
   } else if (element.type === "arrow") {
62
-    const x1 = element.x;
63
-    const y1 = element.y;
64
-    const x2 = element.x + element.width;
65
-    const y2 = element.y + element.height;
61
+    const x1 = 0;
62
+    const y1 = 0;
63
+    const x2 = element.width;
64
+    const y2 = element.height;
66 65
 
67 66
     const size = 30; // pixels
68 67
     const distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
@@ -76,13 +75,18 @@ function generateShape(element) {
76 75
     const [x4, y4] = rotate(xs, ys, x2, y2, (angle * Math.PI) / 180);
77 76
 
78 77
     const shapes = [
79
-      generator.line(x1, y1, x2, y2),
78
+      //    \
80 79
       generator.line(x3, y3, x2, y2),
80
+      // -----
81
+      generator.line(x1, y1, x2, y2),
82
+      //    /
81 83
       generator.line(x4, y4, x2, y2)
82 84
     ];
83 85
 
84 86
     element.draw = (rc, context) => {
87
+      context.translate(element.x, element.y);
85 88
       shapes.forEach(shape => rc.draw(shape));
89
+      context.translate(-element.x, -element.y);
86 90
     };
87 91
     return;
88 92
   } else if (element.type === "text") {
@@ -165,7 +169,6 @@ function App() {
165 169
           else if (event.key === "ArrowRight") element.x += step;
166 170
           else if (event.key === "ArrowUp") element.y -= step;
167 171
           else if (event.key === "ArrowDown") element.y += step;
168
-          generateShape(element);
169 172
         }
170 173
       });
171 174
       drawScene();

Načítá se…
Zrušit
Uložit