Visualize Your Ideas With Code
refactor: place the center of coordinates in the middle
aarthificial786e51f6c528
diff --git a/src/Project.ts b/src/Project.tsindex 6fe51a89..a8d5d88c 100644--- a/src/Project.ts+++ b/src/Project.ts@@ -8,7 +8,7 @@ import {Konva} from 'konva/lib/Global';import {Thread, ThreadsCallback} from './threading';import {Scene, SceneRunner} from './Scene';import {SimpleEventDispatcher} from 'strongly-typed-events';-import {Origin} from './types';+import {KonvaNode} from './decorators';Konva.autoDrawEnabled = false;@@ -21,6 +21,7 @@ interface ProjectConfig extends Partial<StageConfig> {background: string | false;}+@KonvaNode()export class Project extends Stage {public get ScenesChanged() {return this.scenesChanged.asEvent();@@ -73,6 +74,11 @@ export class Project extends Stage {...rest,});+ this.offset({+ x: this.width() / -2,+ y: this.height() / -2,+ });+this.center = {x: this.width() / 2,y: this.height() / 2,@@ -83,7 +89,6 @@ export class Project extends Stage {y: 0,width: this.width(),height: this.height(),- origin: Origin.TopLeft,fill: config.background || '#ff00ff',visible: config.background !== false,});diff --git a/src/Scene.ts b/src/Scene.tsindex 28a31f76..afa8649b 100644--- a/src/Scene.ts+++ b/src/Scene.ts@@ -71,6 +71,8 @@ export class Scene extends Group {) {super({name: runnerFactory.name,+ width: project.width(),+ height: project.height(),...config,});decorate(runnerFactory, threadable());diff --git a/src/components/Align.tsx b/src/components/Align.tsxindex 596e6f2c..e43c45ac 100644--- a/src/components/Align.tsx+++ b/src/components/Align.tsx@@ -2,13 +2,22 @@ import type {Node} from 'konva/lib/Node';import {getOriginDelta, Origin} from '../types';import {useScene} from '../utils';-export function Align(config: {origin?: Origin; children: Node | Node[]}) {+interface AlignConfig {+ origin?: Origin;+ children: Node | Node[];+}++export function Align(config: AlignConfig) {+ const origin = config.origin ?? Origin.Middle;+ if (origin === Origin.Middle) {+ console.warn(+ '<Align> with origin set to Middle does nothing and can be omitted',+ );+ return <>{config.children}</>;+ }+const scene = useScene();- const position = getOriginDelta(- scene.project.getSize(),- Origin.TopLeft,- config.origin ?? Origin.Middle,- );+ const position = getOriginDelta(scene.getSize(), Origin.Middle, origin);if (Array.isArray(config.children)) {for (const child of config.children) {diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsxindex 16a50f92..f9e08b07 100644--- a/src/components/ColorPicker.tsx+++ b/src/components/ColorPicker.tsx@@ -8,7 +8,7 @@ import {Origin} from '../types';import {Style} from '../styles';import {GetSet} from 'konva/lib/types';import {clampRemap} from '../tweening';-import {useScene} from '../utils';+import {makeRef} from '../utils';export interface ColorPickerConfig extends LinearLayoutConfig {previewColor?: string;@@ -48,22 +48,22 @@ export class ColorPicker extends Surface {this.setChild(<LinearLayout origin={Origin.Top}><Rect- ref={[this, 'preview']}+ ref={makeRef(this, 'preview')}width={360}height={200}fill={'yellow'}cornerRadius={[8, 8, 0, 0]}/><Range- ref={[this, 'r']}+ ref={makeRef(this, 'r')}{...colorRangeConfig}label={'R:'}margin={[40, 40, 10]}/>- <Range ref={[this, 'g']} {...colorRangeConfig} label={'G:'} />- <Range ref={[this, 'b']} {...colorRangeConfig} label={'B:'} />+ <Range ref={makeRef(this, 'g')} {...colorRangeConfig} label={'G:'} />+ <Range ref={makeRef(this, 'b')} {...colorRangeConfig} label={'B:'} /><Range- ref={[this, 'a']}+ ref={makeRef(this, 'a')}{...colorRangeConfig}label={'A:'}margin={[10, 40, 40]}diff --git a/src/jsx-runtime.ts b/src/jsx-runtime.tsindex 92e50da8..71be3a5a 100644--- a/src/jsx-runtime.ts+++ b/src/jsx-runtime.ts@@ -10,8 +10,8 @@ function isConstructor(fn: Function): fn is new (...args: any[]) => any {type ChildrenConfig = {[key in keyof JSX.ElementChildrenAttribute]:- | JSX.ElementClass- | JSX.ElementClass[];+ | JSX.ElementClass+ | JSX.ElementClass[];};type ReferenceConfig = {@@ -47,7 +47,12 @@ export function jsx(}if (ref) {- ref.value = node;+ if (Array.isArray(ref)) {+ console.warn('Reference arrays are deprecated. Use makeRef() instead.');+ ref[0][ref[1]] = node;+ } else {+ ref.value = node;+ }}return node;