@g20/grid
Version:
142 lines (134 loc) • 4.46 kB
JavaScript
/**
* @g20/grid 1.0.0-alpha.46
* (c) David Geo Holmes david.geo.holmes@gmail.com
* Released under the MIT License.
*/
'use strict';
var core = require('@g20/core');
class Axes extends core.Group {
xAxis;
yAxis;
xLabel;
yLabel;
constructor(board, options = {}) {
super(board, [], options);
const bbox = board.getBoundingBox();
const sizeX = Math.abs(bbox.right - bbox.left);
const sizeY = Math.abs(bbox.top - bbox.bottom);
const dx = sizeX * 0.05;
const dy = sizeY * 0.05;
const xHead = [(board.crazy ? bbox.left : bbox.right) - dx, 0];
const xTail = [(board.crazy ? bbox.right : bbox.left) + dx, 0];
const yHead = [0, (board.goofy ? bbox.bottom : bbox.top) - dy];
const yTail = [0, (board.goofy ? bbox.top : bbox.bottom) + dy];
this.xAxis = new core.Arrow(board, core.G20.ex.scale(sizeX - 2 * dx), {
position: xTail,
headLength: 0.025 * sizeX
});
this.add(this.xAxis);
this.yAxis = new core.Arrow(board, core.G20.ey.scale(sizeY - 2 * dy), {
position: yTail,
headLength: 0.025 * sizeY
});
this.add(this.yAxis);
this.xLabel = new core.Text(board, "x", {
position: xHead,
anchor: "start",
baseline: "middle",
dx: 16 * 0.6 // fontSize * ratio of width / height for typical character
});
this.add(this.xLabel);
resize(this.xLabel, board);
this.yLabel = new core.Text(board, "y", {
position: yHead,
anchor: "middle",
baseline: "middle",
dy: 16 // fontSize
});
this.add(this.yLabel);
resize(this.yLabel, board);
}
dispose() {
this.xLabel.dispose();
this.yLabel.dispose();
this.xAxis.dispose();
this.yAxis.dispose();
super.dispose();
}
render(viewDOM, parentElement, svgElement) {
if (this.zzz.viewee) ;
else {
super.render(viewDOM, parentElement, svgElement);
}
}
}
function resize(shape, board) {
shape.sx = 1 / board.sx;
shape.sy = 1 / board.sy;
}
function default_color(attributeValue, defaultValue) {
if (core.is_color(attributeValue)) {
return attributeValue;
}
else {
return defaultValue;
}
}
function default_number(value, defaultValue) {
if (typeof value === "number") {
return value;
}
else {
return defaultValue;
}
}
const STROKE_WIDTH_OPEN_PATH_PIXELS = 3;
function default_open_path_stroke_width(strokeWidth, owner) {
if (typeof strokeWidth === "number") {
return strokeWidth;
}
else {
return STROKE_WIDTH_OPEN_PATH_PIXELS / owner.sx;
}
}
class Grid extends core.Group {
constructor(owner, options = {}) {
super(owner, [], options);
const Nx = 10;
const Ny = 10;
const Lx = Nx - 1;
const Ly = Ny - 1;
const Mx = Nx / 2 - 1;
const My = Ny / 2 - 1;
const bbox = owner.getBoundingBox();
const mx = (bbox.right - bbox.left) / Nx;
const my = (bbox.top - bbox.bottom) / Ny;
const lineOptions = {
fillColor: default_color(options.lineColor, owner.defaults.line.fillColor),
fillOpacity: default_number(options.lineOpacity, owner.defaults.line.fillOpacity),
strokeColor: default_color(options.lineColor, owner.defaults.line.strokeColor),
strokeOpacity: default_number(options.lineOpacity, owner.defaults.line.strokeOpacity),
strokeWidth: default_open_path_stroke_width(default_number(options.lineWidth, owner.defaults.line.strokeWidth), owner)
};
for (let i = 0; i < Ly; i++) {
const line = new core.Line(owner, [bbox.left, my * (i - My)], [bbox.right, my * (i - My)], lineOptions);
this.add(line);
}
for (let i = 0; i < Lx; i++) {
const line = new core.Line(owner, [mx * (i - Mx), bbox.bottom], [mx * (i - Mx), bbox.top], lineOptions);
this.add(line);
}
}
dispose() {
super.dispose();
}
render(viewDOM, parentElement, svgElement) {
if (this.zzz.viewee) ;
else {
super.render(viewDOM, parentElement, svgElement);
}
}
}
exports.Axes = Axes;
exports.Grid = Grid;
//# sourceMappingURL=index.js.map