Skip to content

Commit e498225

Browse files
committed
skip redudant MOVEs when creating SVG
1 parent 6f77158 commit e498225

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎dist/examples/funcarea.js‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ REM funcarea - Functional Area
77
REM 26.11.1989
88
REM Functional Area with Hidden Line Removal
99
REM taken from: https://github.com/benchmarko/CPCBasicApps/blob/master/apps/math/funcarea.js
10-
REM Modifications: remove GOTOs; added animation; skip unnecessary MOVEs
10+
REM Modifications: remove GOTOs; added animation
1111
INK 0,0
1212
MODE 2
1313
DIM uh(639),oh(639)
@@ -32,14 +32,10 @@ RETURN
3232
310 FOR y=yu TO yo STEP dy
3333
x=xu:GOSUB 450:s1=sp:z1=ze
3434
GOSUB 480:f1=f
35-
pdrawn=0
3635
FOR x=xu TO xo STEP dx
3736
GOSUB 450:s2=sp:z2=ze
3837
GOSUB 480
39-
' only MOVE when starting new segment
40-
IF f=0 THEN pdrawn=0
41-
IF f1+f=2 AND pdrawn=0 THEN MOVE s1,399-z1:pdrawn=1
42-
IF f1+f=2 THEN DRAW s2,399-z2
38+
IF f1+f=2 THEN MOVE s1,399-z1:DRAW s2,399-z2
4339
s1=s2:z1=z2:f1=f
4440
NEXT x
4541
NEXT y
@@ -48,14 +44,10 @@ RETURN
4844
380 FOR x=xo TO xu STEP -dx1
4945
y=yu:GOSUB 450:s1=sp:z1=ze
5046
GOSUB 480:f1=f
51-
pdrawn=0
5247
FOR y=yu TO yo STEP dy1
5348
GOSUB 450:s2=sp:z2=ze
5449
GOSUB 480
55-
' only MOVE when starting new segment
56-
IF f=0 THEN pdrawn=0
57-
IF f1+f=2 AND pdrawn=0 THEN MOVE s1,399-z1:pdrawn=1
58-
IF f1+f=2 THEN DRAW s2,399-z2
50+
IF f1+f=2 THEN MOVE s1,399-z1:DRAW s2,399-z2
5951
s1=s2:z1=z2:f1=f
6052
NEXT y
6153
NEXT x

‎dist/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
77
<link rel="stylesheet" href="locobasic.css">
88
<link rel="icon" href="favicon.ico">
9-
<title>LocoBasic v0.5.26</title>
9+
<title>LocoBasic v0.5.27</title>
1010
</head>
1111

1212
<body>

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "locobasic",
3-
"version": "0.5.26",
3+
"version": "0.5.27",
44
"description": "# LocoBasic - Loco BASIC",
55
"type": "commonjs",
66
"scripts": {

‎src/vm/VmWorker.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export const workerFn = (parentPort: NodeWorkerThreadsType["parentPort"] | Brows
113113
_graOutputGraphicsIndex: -1,
114114
_graOriginX: 0,
115115
_graOriginY: 0,
116+
_graLastEmittedX: 0,
117+
_graLastEmittedY: 0,
116118

117119
_isTerminal: false, // output for terminal
118120
_keyBuffer: [] as string[], // buffered pressed keys
@@ -327,6 +329,8 @@ export const workerFn = (parentPort: NodeWorkerThreadsType["parentPort"] | Brows
327329
}
328330
vm._graGraphicsX = 0;
329331
vm._graGraphicsY = 0;
332+
vm._graLastEmittedX = 0;
333+
vm._graLastEmittedY = 0;
330334
vm._graOutputGraphicsIndex = -1;
331335
vm._output = "";
332336
vm._paperSpanPos = -1;
@@ -454,7 +458,9 @@ export const workerFn = (parentPort: NodeWorkerThreadsType["parentPort"] | Brows
454458
y = Math.round(y);
455459

456460
if (!vm._graGraphicsPathBuffer.length && type !== "M" && type !== "P") { // path must start with an absolute move
457-
vm._graGraphicsPathBuffer.push(`M${vm._graGraphicsX + vm._graOriginX} ${399 - vm._graGraphicsY - vm._graOriginY}`);
461+
vm._graLastEmittedX = vm._graGraphicsX + vm._graOriginX;
462+
vm._graLastEmittedY = 399 - vm._graGraphicsY - vm._graOriginY;
463+
vm._graGraphicsPathBuffer.push(`M${vm._graLastEmittedX} ${vm._graLastEmittedY}`);
458464
}
459465

460466
const isAbsolute = type === type.toUpperCase();
@@ -469,11 +475,27 @@ export const workerFn = (parentPort: NodeWorkerThreadsType["parentPort"] | Brows
469475
y = -y;
470476
}
471477

478+
// Skip MOVE if we're already at this position (works regardless of preceding command type)
479+
if (type === "M" || type === "m") {
480+
let compareX = x;
481+
let compareY = y;
482+
if (!isAbsolute) {
483+
// Convert relative to absolute screen coordinates
484+
compareX = vm._graGraphicsX + vm._graOriginX;
485+
compareY = 399 - vm._graGraphicsY - vm._graOriginY;
486+
}
487+
if (vm._graLastEmittedX === compareX && vm._graLastEmittedY === compareY) {
488+
return;
489+
}
490+
}
491+
472492
const svgPathCmd = (type === "P" || type === "p")
473493
? `${isAbsolute ? "M" : "m"}${x} ${y}h1v1h-1v-1`
474494
: `${type}${x} ${y}`;
475495

476496
vm._graGraphicsPathBuffer.push(svgPathCmd);
497+
vm._graLastEmittedX = x;
498+
vm._graLastEmittedY = y;
477499
},
478500

479501
graFlushGraphicsPath: (): void => {
@@ -511,8 +533,10 @@ export const workerFn = (parentPort: NodeWorkerThreadsType["parentPort"] | Brows
511533
text = vm.escapeText(text);
512534
const yOffset = 16;
513535
const colorStyleStr = vm._graCurrGraphicsPen >= 0 ? `; color: ${vm.graGetRgbColorStringForPen(vm._graCurrGraphicsPen)}` : "";
514-
vm.graAddGraphicsElement(`<text x="${vm._graGraphicsX + vm._graOriginX}" y="${399 - vm._graGraphicsY - vm._graOriginY + yOffset}" style="white-space: pre${colorStyleStr}">${text}</text>`);
536+
vm._graLastEmittedY = 399 - vm._graGraphicsY - vm._graOriginY;
537+
vm.graAddGraphicsElement(`<text x="${vm._graGraphicsX + vm._graOriginX}" y="${vm._graLastEmittedY + yOffset}" style="white-space: pre${colorStyleStr}">${text}</text>`);
515538
vm._graGraphicsX += text.length * 8; // assuming 8px width per character
539+
vm._graLastEmittedX = vm._graGraphicsX + vm._graOriginX;
516540
},
517541

518542
graSetOutputGraphicsIndex: (): void => {

0 commit comments

Comments
 (0)