1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-08-26 01:44:03 +02:00

error-only for http-server, better parameter parsing for sketch markup

This commit is contained in:
Pomax
2020-08-27 11:36:17 -07:00
parent 46f94282e1
commit b2314a4f87
47 changed files with 130 additions and 293 deletions

View File

@@ -1,5 +1,3 @@
import { hatch } from "./util/hatchery.js";
/**
* The base API class, responsible for such things as setting up canvas event
* handling, method accumulation, custom element binding, etc. etc.
@@ -65,7 +63,17 @@ class BaseAPI {
this.dataset = {};
}
}
this.HATCHING = hatch(canvasBuildFunction);
this.parameters = Object.fromEntries(
Object.entries(this.dataset)
.map((pair) => {
let v = pair[1];
if (v === `null` || v === `undefined`) return [];
let d = parseFloat(v);
// evaluate "string is number" using == rather than ===
return [pair[0], v == d ? d : v];
})
.filter((v) => v.length)
);
this.addListeners();
this.setSize(width, height);
this.currentPoint = false;

View File

@@ -138,19 +138,6 @@ class GraphicsAPI extends BaseAPI {
points.forEach((p) => this.movable.push(p));
}
/**
* Get a parameter specified via data-attribute
*/
getParameter(name, fallback) {
let val = this.dataset[name];
if (val) {
let asFloat = parseFloat(val);
if (val == asFloat) return asFloat;
return val;
}
return fallback;
}
/**
* Set up a slider to control a named, numerical property in the sketch.
*

View File

@@ -1,111 +0,0 @@
const HATCHING = [];
/**
* Build hatching patterns. These are built fully unrolled,
* mostly because they're small and there's no actual benefit
* to abstracting the drawing for only six patterns.
*/
function hatch(canvasBuildFunction) {
if (HATCHING.length > 0) {
return HATCHING;
}
let cvs,
ctx,
w = 9,
h = 9;
if (canvasBuildFunction) {
let b = canvasBuildFunction(w, h);
cvs = b.canvas;
ctx = b.ctx;
} else {
cvs = document.createElement("canvas");
cvs.width = w;
cvs.height = h;
ctx = cvs.getContext(`2d`);
}
ctx.fillStyle = `#0000FF30`;
ctx.lineWidth = 1;
const paint = (x, y) => ctx.fillRect(x, y, 1, 1);
// pattern: \
ctx.clearRect(0, 0, w, h);
paint(0, 0);
paint(1, 1);
paint(2, 2);
paint(3, 3);
paint(4, 4);
paint(5, 5);
paint(6, 6);
paint(7, 7);
paint(8, 8);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
// pattern: /
ctx.clearRect(0, 0, w, h);
paint(0, 8);
paint(1, 7);
paint(2, 6);
paint(3, 5);
paint(4, 4);
paint(5, 3);
paint(6, 2);
paint(7, 1);
paint(8, 0);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
// pattern: x (without clearing, because we can overlay)
paint(0, 0);
paint(1, 1);
paint(2, 2);
paint(3, 3);
paint(5, 5);
paint(6, 6);
paint(7, 7);
paint(8, 8);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
// pattern: |
ctx.clearRect(0, 0, w, h);
paint(4, 0);
paint(4, 1);
paint(4, 2);
paint(4, 3);
paint(4, 4);
paint(4, 5);
paint(4, 6);
paint(4, 7);
paint(4, 8);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
// pattern: -
ctx.clearRect(0, 0, w, h);
paint(0, 4);
paint(1, 4);
paint(2, 4);
paint(3, 4);
paint(4, 4);
paint(5, 4);
paint(6, 4);
paint(7, 4);
paint(8, 4);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
// pattern: + (without clearing, because we can overlap)
paint(4, 0);
paint(4, 1);
paint(4, 2);
paint(4, 3);
paint(4, 5);
paint(4, 6);
paint(4, 7);
paint(4, 8);
HATCHING.push(ctx.createPattern(cvs, "repeat"));
return HATCHING;
}
export { hatch };