增加网格背景

This commit is contained in:
ZhaoErWei 2024-02-07 11:51:44 +08:00
parent 73aebfa4d3
commit 36f3f0257e
6 changed files with 105 additions and 4 deletions

View File

@ -27,12 +27,19 @@ export interface ICanvasProperties {
height: number; height: number;
backgroundColor: string; backgroundColor: string;
viewportTransform: GraphicTransform; viewportTransform: GraphicTransform;
gridBackground?: IGridBackground;
}
export interface IGridBackground {
hasGrid: boolean;
lineColor: string;
space: number;
} }
export declare class CanvasData implements ICanvasProperties { export declare class CanvasData implements ICanvasProperties {
width: number; width: number;
height: number; height: number;
backgroundColor: string; backgroundColor: string;
viewportTransform: GraphicTransform; viewportTransform: GraphicTransform;
gridBackground: IGridBackground | undefined;
constructor(properties?: ICanvasProperties); constructor(properties?: ICanvasProperties);
copyFrom(properties: ICanvasProperties): boolean; copyFrom(properties: ICanvasProperties): boolean;
clone(): CanvasData; clone(): CanvasData;
@ -72,6 +79,7 @@ export declare class JlCanvas extends Container implements IJlCanvas {
scene: IGraphicScene; scene: IGraphicScene;
_properties: CanvasData; _properties: CanvasData;
bg: Graphics; bg: Graphics;
gridBackgroundLine: Container<DisplayObject>;
nonInteractiveContainer: Container; nonInteractiveContainer: Container;
assistantAppendContainer: Container; assistantAppendContainer: Container;
constructor(scene: IGraphicScene, properties?: CanvasData); constructor(scene: IGraphicScene, properties?: CanvasData);
@ -82,6 +90,7 @@ export declare class JlCanvas extends Container implements IJlCanvas {
get width(): number; get width(): number;
get height(): number; get height(): number;
get backgroundColor(): string; get backgroundColor(): string;
get gridBackground(): IGridBackground | undefined;
doRepaint(): void; doRepaint(): void;
get properties(): CanvasData; get properties(): CanvasData;
saveData(): CanvasData; saveData(): CanvasData;

4
lib/app/index.d.ts vendored
View File

@ -1,5 +1,5 @@
import { DrawAppOptions, DrawAssistant, GraphicDrawAssistant, IDrawApp } from './JlDrawApp'; import { DrawAppOptions, DrawAssistant, GraphicDrawAssistant, IDrawApp } from './JlDrawApp';
import { AppConsts, GraphicAppOptions, ICanvasProperties, IGraphicApp, IGraphicScene, IGraphicStorage, IJlCanvas } from './JlGraphicApp'; import { AppConsts, GraphicAppOptions, ICanvasProperties, IGraphicApp, IGraphicScene, IGraphicStorage, IJlCanvas, IGridBackground } from './JlGraphicApp';
import { GraphicDataUpdateOperation } from './BasicOperation'; import { GraphicDataUpdateOperation } from './BasicOperation';
/** /**
* app * app
@ -14,4 +14,4 @@ export declare function newGraphicApp(options: GraphicAppOptions): IGraphicApp;
*/ */
export declare function newDrawApp(options: DrawAppOptions): IDrawApp; export declare function newDrawApp(options: DrawAppOptions): IDrawApp;
export { AppConsts, GraphicDrawAssistant, GraphicDataUpdateOperation }; export { AppConsts, GraphicDrawAssistant, GraphicDataUpdateOperation };
export type { DrawAssistant, ICanvasProperties, IDrawApp, IGraphicApp, IGraphicScene, IGraphicStorage, IJlCanvas, }; export type { DrawAssistant, ICanvasProperties, IDrawApp, IGraphicApp, IGraphicScene, IGraphicStorage, IJlCanvas, IGridBackground };

View File

@ -2523,7 +2523,7 @@ function debounce(fn, waitMs = 250) {
fn.apply(context, args); fn.apply(context, args);
}; };
if (timeoutId !== undefined) { if (timeoutId !== undefined) {
console.debug('debounce clear timeout', fn); // console.debug('debounce clear timeout', fn);
clearTimeout(timeoutId); clearTimeout(timeoutId);
} }
timeoutId = setTimeout(invokeFunction, waitMs); timeoutId = setTimeout(invokeFunction, waitMs);
@ -6429,16 +6429,19 @@ class CanvasData {
height; height;
backgroundColor; backgroundColor;
viewportTransform; viewportTransform;
gridBackground;
constructor(properties = { constructor(properties = {
width: 1920, width: 1920,
height: 1080, height: 1080,
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
viewportTransform: GraphicTransform.default(), viewportTransform: GraphicTransform.default(),
gridBackground: undefined,
}) { }) {
this.width = properties.width; this.width = properties.width;
this.height = properties.height; this.height = properties.height;
this.backgroundColor = properties.backgroundColor; this.backgroundColor = properties.backgroundColor;
this.viewportTransform = properties.viewportTransform; this.viewportTransform = properties.viewportTransform;
this.gridBackground = properties.gridBackground;
} }
copyFrom(properties) { copyFrom(properties) {
let sizeChange = false; let sizeChange = false;
@ -6459,6 +6462,9 @@ class CanvasData {
} }
this.backgroundColor = properties.backgroundColor; this.backgroundColor = properties.backgroundColor;
this.viewportTransform = properties.viewportTransform; this.viewportTransform = properties.viewportTransform;
if (properties.gridBackground) {
this.gridBackground = properties.gridBackground;
}
return sizeChange; return sizeChange;
} }
clone() { clone() {
@ -6472,6 +6478,7 @@ class JlCanvas extends Container {
scene; scene;
_properties; _properties;
bg = new Graphics(); // 背景 bg = new Graphics(); // 背景
gridBackgroundLine = new Container(); //网格背景
nonInteractiveContainer; // 无交互对象容器 nonInteractiveContainer; // 无交互对象容器
assistantAppendContainer; // 辅助附加容器 assistantAppendContainer; // 辅助附加容器
constructor(scene, properties = new CanvasData()) { constructor(scene, properties = new CanvasData()) {
@ -6483,6 +6490,7 @@ class JlCanvas extends Container {
this.nonInteractiveContainer.name = 'non-interactives'; this.nonInteractiveContainer.name = 'non-interactives';
this.nonInteractiveContainer.eventMode = 'none'; this.nonInteractiveContainer.eventMode = 'none';
this.addChild(this.bg); this.addChild(this.bg);
this.addChild(this.gridBackgroundLine);
this.addChild(this.nonInteractiveContainer); this.addChild(this.nonInteractiveContainer);
this.sortableChildren = true; this.sortableChildren = true;
this.assistantAppendContainer = new Container(); this.assistantAppendContainer = new Container();
@ -6508,12 +6516,44 @@ class JlCanvas extends Container {
get backgroundColor() { get backgroundColor() {
return this._properties.backgroundColor; return this._properties.backgroundColor;
} }
get gridBackground() {
return this._properties.gridBackground;
}
doRepaint() { doRepaint() {
this.bg.clear(); this.bg.clear();
this.bg this.bg
.beginFill(new Color(this.backgroundColor)) .beginFill(new Color(this.backgroundColor))
.drawRect(0, 0, this._properties.width, this._properties.height) .drawRect(0, 0, this._properties.width, this._properties.height)
.endFill(); .endFill();
this.gridBackgroundLine.children.forEach((g) => {
g.clear();
});
if (this.gridBackground &&
this.gridBackground.hasGrid &&
this.gridBackground.space > 0) {
const horizontalAmount = this.height / this.gridBackground.space;
for (let i = 1; i < horizontalAmount; i++) {
const lineGraphic = new Graphics();
const posY = i * this.gridBackground.space;
lineGraphic
.clear()
.lineStyle(1, new Color(this.gridBackground.lineColor))
.moveTo(0, posY)
.lineTo(this.width, posY);
this.gridBackgroundLine.addChild(lineGraphic);
}
const verticalAmount = this.width / this.gridBackground.space;
for (let i = 1; i < verticalAmount; i++) {
const lineGraphic = new Graphics();
const posX = i * this.gridBackground.space;
lineGraphic
.clear()
.lineStyle(1, new Color(this.gridBackground.lineColor))
.moveTo(posX, 0)
.lineTo(posX, this.height);
this.gridBackgroundLine.addChild(lineGraphic);
}
}
} }
get properties() { get properties() {
return this._properties; return this._properties;

View File

@ -1,6 +1,6 @@
{ {
"name": "graphic-pixi", "name": "graphic-pixi",
"version": "0.1.10", "version": "0.1.11",
"description": "基于pixijs的图形应用、绘制应用框架", "description": "基于pixijs的图形应用、绘制应用框架",
"productName": "Graphic-pixi", "productName": "Graphic-pixi",
"author": "walker <shengxuqiang@joylink.club>", "author": "walker <shengxuqiang@joylink.club>",

View File

@ -71,6 +71,13 @@ export interface ICanvasProperties {
height: number; height: number;
backgroundColor: string; backgroundColor: string;
viewportTransform: GraphicTransform; viewportTransform: GraphicTransform;
gridBackground?: IGridBackground; //网格背景
}
export interface IGridBackground {
hasGrid: boolean;
lineColor: string;
space: number;
} }
export class CanvasData implements ICanvasProperties { export class CanvasData implements ICanvasProperties {
@ -78,18 +85,21 @@ export class CanvasData implements ICanvasProperties {
height: number; height: number;
backgroundColor: string; backgroundColor: string;
viewportTransform: GraphicTransform; viewportTransform: GraphicTransform;
gridBackground: IGridBackground | undefined;
constructor( constructor(
properties: ICanvasProperties = { properties: ICanvasProperties = {
width: 1920, width: 1920,
height: 1080, height: 1080,
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
viewportTransform: GraphicTransform.default(), viewportTransform: GraphicTransform.default(),
gridBackground: undefined,
} }
) { ) {
this.width = properties.width; this.width = properties.width;
this.height = properties.height; this.height = properties.height;
this.backgroundColor = properties.backgroundColor; this.backgroundColor = properties.backgroundColor;
this.viewportTransform = properties.viewportTransform; this.viewportTransform = properties.viewportTransform;
this.gridBackground = properties.gridBackground;
} }
copyFrom(properties: ICanvasProperties): boolean { copyFrom(properties: ICanvasProperties): boolean {
@ -110,6 +120,9 @@ export class CanvasData implements ICanvasProperties {
} }
this.backgroundColor = properties.backgroundColor; this.backgroundColor = properties.backgroundColor;
this.viewportTransform = properties.viewportTransform; this.viewportTransform = properties.viewportTransform;
if (properties.gridBackground) {
this.gridBackground = properties.gridBackground;
}
return sizeChange; return sizeChange;
} }
@ -155,6 +168,7 @@ export class JlCanvas extends Container implements IJlCanvas {
scene: IGraphicScene; scene: IGraphicScene;
_properties: CanvasData; _properties: CanvasData;
bg: Graphics = new Graphics(); // 背景 bg: Graphics = new Graphics(); // 背景
gridBackgroundLine = new Container(); //网格背景
nonInteractiveContainer: Container; // 无交互对象容器 nonInteractiveContainer: Container; // 无交互对象容器
assistantAppendContainer: Container; // 辅助附加容器 assistantAppendContainer: Container; // 辅助附加容器
@ -167,6 +181,7 @@ export class JlCanvas extends Container implements IJlCanvas {
this.nonInteractiveContainer.name = 'non-interactives'; this.nonInteractiveContainer.name = 'non-interactives';
this.nonInteractiveContainer.eventMode = 'none'; this.nonInteractiveContainer.eventMode = 'none';
this.addChild(this.bg); this.addChild(this.bg);
this.addChild(this.gridBackgroundLine);
this.addChild(this.nonInteractiveContainer); this.addChild(this.nonInteractiveContainer);
this.sortableChildren = true; this.sortableChildren = true;
@ -199,12 +214,47 @@ export class JlCanvas extends Container implements IJlCanvas {
return this._properties.backgroundColor; return this._properties.backgroundColor;
} }
public get gridBackground(): IGridBackground | undefined {
return this._properties.gridBackground;
}
doRepaint() { doRepaint() {
this.bg.clear(); this.bg.clear();
this.bg this.bg
.beginFill(new Color(this.backgroundColor)) .beginFill(new Color(this.backgroundColor))
.drawRect(0, 0, this._properties.width, this._properties.height) .drawRect(0, 0, this._properties.width, this._properties.height)
.endFill(); .endFill();
this.gridBackgroundLine.children.forEach((g) => {
(g as Graphics).clear();
});
if (
this.gridBackground &&
this.gridBackground.hasGrid &&
this.gridBackground.space > 0
) {
const horizontalAmount = this.height / this.gridBackground.space;
for (let i = 1; i < horizontalAmount; i++) {
const lineGraphic: Graphics = new Graphics();
const posY = i * this.gridBackground.space;
lineGraphic
.clear()
.lineStyle(1, new Color(this.gridBackground.lineColor))
.moveTo(0, posY)
.lineTo(this.width, posY);
this.gridBackgroundLine.addChild(lineGraphic);
}
const verticalAmount = this.width / this.gridBackground.space;
for (let i = 1; i < verticalAmount; i++) {
const lineGraphic: Graphics = new Graphics();
const posX = i * this.gridBackground.space;
lineGraphic
.clear()
.lineStyle(1, new Color(this.gridBackground.lineColor))
.moveTo(posX, 0)
.lineTo(posX, this.height);
this.gridBackgroundLine.addChild(lineGraphic);
}
}
} }
public get properties(): CanvasData { public get properties(): CanvasData {

View File

@ -14,6 +14,7 @@ import {
IGraphicScene, IGraphicScene,
IGraphicStorage, IGraphicStorage,
IJlCanvas, IJlCanvas,
IGridBackground
} from './JlGraphicApp'; } from './JlGraphicApp';
import { GraphicDataUpdateOperation } from './BasicOperation'; import { GraphicDataUpdateOperation } from './BasicOperation';
@ -44,4 +45,5 @@ export type {
IGraphicScene, IGraphicScene,
IGraphicStorage, IGraphicStorage,
IJlCanvas, IJlCanvas,
IGridBackground
}; };