diff --git a/examples/test.gs b/examples/test.gs new file mode 100644 index 0000000..7c0d49b --- /dev/null +++ b/examples/test.gs @@ -0,0 +1,3 @@ +point(3 | 4) + +point(6 | 7) \ No newline at end of file diff --git a/package.json b/package.json index 18533db..3c6b35f 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,8 @@ "devDependencies": { "ts-node": "^10.4.0", "typescript": "^4.5.2" + }, + "dependencies": { + "@types/node": "^16.11.10" } } diff --git a/src/doc/syntax.md b/src/doc/syntax.md new file mode 100644 index 0000000..14005e0 --- /dev/null +++ b/src/doc/syntax.md @@ -0,0 +1,16 @@ +# Syntax + +``` +point(3 | 4) -> A +point(6 | 7) -> B + +line(A, B) -> AB +line(0 | 0, 100 | 100) + +circle(A, len(AB)) +``` + +## Primitives +* `Point point(x, y)` is a 2D point. It returns an element of type `Point` +* `Line line(Point from, Point to)` is a straight line. It returns an element of type `Line`. +* `Circle circle(Point center, radius)` draws a circle at `center` and `radius` \ No newline at end of file diff --git a/src/geometry.ts b/src/geometry.ts index 82e2c59..58abec9 100644 --- a/src/geometry.ts +++ b/src/geometry.ts @@ -1,24 +1,70 @@ import { Vector2D } from "./vector.js" import * as shape from "./shapes.js" +function loadScript(filepath: string): string +{ + var result = null; + var xmlhttp = new XMLHttpRequest(); + xmlhttp.open("GET", filepath, false); + xmlhttp.send(); + if (xmlhttp.status==200) { + result = xmlhttp.responseText; + } + + return result; +} + class Geometry extends HTMLElement { private canvas: HTMLCanvasElement; private context: CanvasRenderingContext2D; + private sourceFile: string; constructor() { super(); + if(!this.hasAttribute("src")) + { + return; + } + + let sourceFile = this.getAttribute("src"); + let content = loadScript(sourceFile); + + let lines = content.split("\n"); + for(let line of lines) + { + if(line === "\r") + { + console.log("empty"); + continue; + } + + let instruction = line.split("(")[0]; + + switch(instruction) + { + case instruction: + { + let coords = line.split("(")[1].split("|"); + console.log(coords); + break; + } + + default: + { + console.log("something else"); + break; + } + } + } + this.attachShadow({mode: "open"}); let canvas = document.createElement("canvas"); canvas.width = 500; canvas.height = 500; let context = canvas.getContext("2d"); - context.lineCap = "round"; - context.lineJoin = "round"; - context.strokeStyle = "black"; - context.lineWidth = 1; this.canvas = canvas; this.context = context;