1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../../../fields/Doc';
import { NumCast } from '../../../../fields/Types';
import './CollectionFreeFormView.scss';
export interface CollectionFreeFormViewBackgroundGridProps {
panX: () => number;
panY: () => number;
PanelWidth: () => number;
PanelHeight: () => number;
color: () => string;
// eslint-disable-next-line react/require-default-props
isAnnotationOverlay?: boolean;
nativeDimScaling: () => number;
zoomScaling: () => number;
layoutDoc: Doc;
centeringShiftX: number;
centeringShiftY: number;
}
@observer
export class CollectionFreeFormBackgroundGrid extends React.Component<CollectionFreeFormViewBackgroundGridProps> {
chooseGridSpace = (gridSpace: number): number => {
if (!this.props.zoomScaling()) return gridSpace;
const divisions = this.props.PanelWidth() / this.props.zoomScaling() / gridSpace;
return divisions < 90 ? gridSpace : this.chooseGridSpace(gridSpace * 2);
};
render() {
const gridSpace = this.chooseGridSpace(NumCast(this.props.layoutDoc['_backgroundGrid-spacing'], 50));
const shiftX = (this.props.isAnnotationOverlay ? 0 : (-this.props.panX() % gridSpace) - gridSpace) * this.props.zoomScaling();
const shiftY = (this.props.isAnnotationOverlay ? 0 : (-this.props.panY() % gridSpace) - gridSpace) * this.props.zoomScaling();
const renderGridSpace = gridSpace * this.props.zoomScaling();
const w = this.props.PanelWidth() / this.props.nativeDimScaling() + 2 * renderGridSpace;
const h = this.props.PanelHeight() / this.props.nativeDimScaling() + 2 * renderGridSpace;
const strokeStyle = this.props.color();
return (
<canvas
className="collectionFreeFormView-grid"
width={w}
height={h}
style={{ transform: `translate(${shiftX}px, ${shiftY}px)` }}
ref={el => {
const ctx = el?.getContext('2d');
if (ctx) {
const Cx = this.props.centeringShiftX % renderGridSpace;
const Cy = this.props.centeringShiftY % renderGridSpace;
ctx.lineWidth = Math.min(1, Math.max(0.5, this.props.zoomScaling()));
ctx.setLineDash(gridSpace > 50 ? [3, 3] : [1, 5]);
ctx.clearRect(0, 0, w, h);
if (ctx) {
ctx.strokeStyle = strokeStyle;
ctx.fillStyle = strokeStyle;
ctx.beginPath();
if (this.props.zoomScaling() > 1) {
for (let x = Cx - renderGridSpace; x <= w - Cx; x += renderGridSpace) {
ctx.moveTo(x, Cy - h);
ctx.lineTo(x, Cy + h);
}
for (let y = Cy - renderGridSpace; y <= h - Cy; y += renderGridSpace) {
ctx.moveTo(Cx - w, y);
ctx.lineTo(Cx + w, y);
}
} else {
for (let x = Cx - renderGridSpace; x <= w - Cx; x += renderGridSpace)
for (let y = Cy - renderGridSpace; y <= h - Cy; y += renderGridSpace) {
ctx.fillRect(Math.round(x), Math.round(y), 1, 1);
}
}
ctx.stroke();
}
}
}}
/>
);
}
}
|