1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-10-02 02:56:41 +02:00

automatic placeholder generation

This commit is contained in:
Pomax
2020-08-05 20:57:43 -07:00
parent 040b545426
commit 6b0d5c1eb2
24 changed files with 274 additions and 233 deletions

View File

@@ -32,10 +32,19 @@ class BaseAPI {
/**
*
*/
constructor(uid, width = 200, height = 200) {
this.element = window[uid];
delete window[uid];
this.canvas = document.createElement(`canvas`);
constructor(uid, width = 200, height = 200, canvasBuildFunction) {
if (uid) {
this.element = window[uid];
delete window[uid];
}
if (canvasBuildFunction) {
const { canvas, ctx } = canvasBuildFunction(width, height);
this.canvas = canvas;
this.ctx = enhanceContext(ctx);
this.preSized = true;
} else {
this.canvas = document.createElement(`canvas`);
}
this.addListeners();
this.setSize(width, height);
this.setup();
@@ -47,8 +56,10 @@ class BaseAPI {
*/
addListeners() {
const canvas = this.canvas;
const element = this.element;
element.setGraphic(this);
if (this.element) {
this.element.setGraphic(this);
}
this.cursor = {};
@@ -185,10 +196,12 @@ class BaseAPI {
setSize(w, h) {
this.width = w || this.width;
this.height = h || this.height;
this.canvas.width = this.width;
this.canvas.style.width = `${this.width}px`;
this.canvas.height = this.height;
this.ctx = enhanceContext(this.canvas.getContext(`2d`));
if (!this.preSized) {
this.canvas.width = this.width;
this.canvas.style.width = `${this.width}px`;
this.canvas.height = this.height;
this.ctx = enhanceContext(this.canvas.getContext(`2d`));
}
}
/**
@@ -223,19 +236,21 @@ class BaseAPI {
function enhanceContext(ctx) {
const styles = [];
ctx.cacheStyle = () => {
let m = ctx.currentTransform || ctx.getTransform();
let e = {
strokeStyle: ctx.strokeStyle,
fillStyle: ctx.fillStyle,
lineWidth: ctx.lineWidth,
transform: ctx.getTransform(),
transform: [m.a, m.b, m.c, m.d, m.e, m.f],
};
styles.push(e);
};
ctx.restoreStyle = () => {
const v = styles.pop();
Object.keys(v).forEach((k) => {
if (k !== `transform`) ctx[k] = v[k];
else ctx.setTransform(v[k]);
let val = v[k];
if (k !== `transform`) ctx[k] = val;
else ctx.setTransform(val[0], val[1], val[2], val[3], val[4], val[5]);
});
};
return ctx;