11import type { Size , Vector2 } from '@comfyorg/litegraph'
2- import { CSSProperties , ref } from 'vue'
2+ import { CSSProperties , computed , ref } from 'vue'
33
4- import { app } from '@/scripts/app '
4+ import { useCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion '
55import { useCanvasStore } from '@/stores/graphStore'
66
77export interface PositionConfig {
@@ -13,70 +13,56 @@ export interface PositionConfig {
1313 scale ?: number
1414}
1515
16- export function useAbsolutePosition ( ) {
16+ export function useAbsolutePosition ( options : { useTransform ?: boolean } = { } ) {
17+ const { useTransform = false } = options
18+
1719 const canvasStore = useCanvasStore ( )
18- const style = ref < CSSProperties > ( {
19- position : 'fixed' ,
20- left : '0px' ,
21- top : '0px' ,
22- width : '0px' ,
23- height : '0px'
20+ const lgCanvas = canvasStore . getCanvas ( )
21+ const { canvasPosToClientPos } = useCanvasPositionConversion (
22+ lgCanvas . canvas ,
23+ lgCanvas
24+ )
25+
26+ const position = ref < PositionConfig > ( {
27+ pos : [ 0 , 0 ] ,
28+ size : [ 0 , 0 ]
2429 } )
2530
26- /**
27- * Update the position of the element on the litegraph canvas.
28- *
29- * @param config
30- * @param extraStyle
31- */
32- const updatePosition = (
33- config : PositionConfig ,
34- extraStyle ?: CSSProperties
35- ) => {
36- const { pos, size, scale = canvasStore . canvas ?. ds ?. scale ?? 1 } = config
37- const [ left , top ] = app . canvasPosToClientPos ( pos )
31+ const style = computed < CSSProperties > ( ( ) => {
32+ const { pos, size, scale = lgCanvas . ds . scale } = position . value
33+ const [ left , top ] = canvasPosToClientPos ( pos )
3834 const [ width , height ] = size
3935
40- style . value = {
41- ...style . value ,
42- left : `${ left } px` ,
43- top : `${ top } px` ,
44- width : `${ width * scale } px` ,
45- height : `${ height * scale } px` ,
46- ...extraStyle
47- }
48- }
36+ return useTransform
37+ ? {
38+ position : 'fixed' ,
39+ transformOrigin : '0 0' ,
40+ transform : `scale(${ scale } )` ,
41+ left : `${ left } px` ,
42+ top : `${ top } px` ,
43+ width : `${ width } px` ,
44+ height : `${ height } px`
45+ }
46+ : {
47+ position : 'fixed' ,
48+ left : `${ left } px` ,
49+ top : `${ top } px` ,
50+ width : `${ width * scale } px` ,
51+ height : `${ height * scale } px`
52+ }
53+ } )
4954
5055 /**
51- * Update the position and size of the element on the litegraph canvas,
52- * with CSS transform scaling applied.
56+ * Update the position of the element on the litegraph canvas.
5357 *
5458 * @param config
55- * @param extraStyle
5659 */
57- const updatePositionWithTransform = (
58- config : PositionConfig ,
59- extraStyle ?: CSSProperties
60- ) => {
61- const { pos, size, scale = canvasStore . canvas ?. ds ?. scale ?? 1 } = config
62- const [ left , top ] = app . canvasPosToClientPos ( pos )
63- const [ width , height ] = size
64-
65- style . value = {
66- ...style . value ,
67- transformOrigin : '0 0' ,
68- transform : `scale(${ scale } )` ,
69- left : `${ left } px` ,
70- top : `${ top } px` ,
71- width : `${ width } px` ,
72- height : `${ height } px` ,
73- ...extraStyle
74- }
60+ const updatePosition = ( config : PositionConfig ) => {
61+ position . value = config
7562 }
7663
7764 return {
7865 style,
79- updatePosition,
80- updatePositionWithTransform
66+ updatePosition
8167 }
8268}
0 commit comments