added file loading
This commit is contained in:
parent
355b840471
commit
48d857f911
3
examples/test.gs
Normal file
3
examples/test.gs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
point(3 | 4)
|
||||||
|
|
||||||
|
point(6 | 7)
|
|
@ -11,5 +11,8 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ts-node": "^10.4.0",
|
"ts-node": "^10.4.0",
|
||||||
"typescript": "^4.5.2"
|
"typescript": "^4.5.2"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "^16.11.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
src/doc/syntax.md
Normal file
16
src/doc/syntax.md
Normal file
|
@ -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`
|
|
@ -1,24 +1,70 @@
|
||||||
import { Vector2D } from "./vector.js"
|
import { Vector2D } from "./vector.js"
|
||||||
import * as shape from "./shapes.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
|
class Geometry extends HTMLElement
|
||||||
{
|
{
|
||||||
private canvas: HTMLCanvasElement;
|
private canvas: HTMLCanvasElement;
|
||||||
private context: CanvasRenderingContext2D;
|
private context: CanvasRenderingContext2D;
|
||||||
|
private sourceFile: string;
|
||||||
|
|
||||||
constructor()
|
constructor()
|
||||||
{
|
{
|
||||||
super();
|
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"});
|
this.attachShadow({mode: "open"});
|
||||||
let canvas = document.createElement("canvas");
|
let canvas = document.createElement("canvas");
|
||||||
canvas.width = 500;
|
canvas.width = 500;
|
||||||
canvas.height = 500;
|
canvas.height = 500;
|
||||||
let context = canvas.getContext("2d");
|
let context = canvas.getContext("2d");
|
||||||
context.lineCap = "round";
|
|
||||||
context.lineJoin = "round";
|
|
||||||
context.strokeStyle = "black";
|
|
||||||
context.lineWidth = 1;
|
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
Loading…
Reference in a new issue