You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is simply to propose the addition of a new action for the community, feel free to close or delete this issue if you think this is not a worthy proposal.
I recently had to work with pointer events and svg elements and faced the annoying case where the auto-scaling resulting from the svg's viewBox meant the pointer coordinates retrieved from native events were misaligned with the svg's coordinates space. This is just a question of reversing the transformation, and luckily could be done rather easily with methods already available. Here's the action I came up with:
constSVG_POINTER_EVENT={Move: 'svg.pointermove',Down: 'svg.pointerdown',Up: 'svg.pointerup',Click: 'svg.click',}asconst;interfaceSvgPointerEventData<EextendsMouseEvent|PointerEvent>{x: number;y: number;originalEvent: E;}declare global {namespacesvelteHTML{interfaceSVGAttributes<T>{'on:svg.pointermove'?: (event: CustomEvent<SvgPointerEventData<PointerEvent>>)=>unknown;'on:svg.pointedown'?: (event: CustomEvent<SvgPointerEventData<PointerEvent>>)=>unknown;'on:svg.pointerup'?: (event: CustomEvent<SvgPointerEventData<PointerEvent>>)=>unknown;'on:svg.click'?: (event: CustomEvent<SvgPointerEventData<MouseEvent>>)=>unknown;}}}/** * Facilitates retrieving the pointer's position as transformed by the host viewboxed svg element's * auto-scaling. * * @see https://stackoverflow.com/questions/10298658/mouse-position-inside-autoscaled-svg */exportdefaultfunctionsvgPointer(svg: SVGSVGElement,{}: {}={}){constpt=svg.createSVGPoint();functiongetSvgPointer<EextendsMouseEvent|PointerEvent>(e: E){pt.x=e.clientX;pt.y=e.clientY;consttransformed=pt.matrixTransform(svg.getScreenCTM()?.inverse());return{x: transformed.x,y: transformed.y,originalEvent: e,}satisfiesSvgPointerEventData<E>;}functionhandlePointerMove(e: PointerEvent){svg.dispatchEvent(newCustomEvent(SVG_POINTER_EVENT.Move,{detail: getSvgPointer(e)}));}functionhandlePointerDown(e: PointerEvent){svg.dispatchEvent(newCustomEvent(SVG_POINTER_EVENT.Down,{detail: getSvgPointer(e)}));}functionhandlePointerUp(e: PointerEvent){svg.dispatchEvent(newCustomEvent(SVG_POINTER_EVENT.Up,{detail: getSvgPointer(e)}));}functionhandleClick(e: MouseEvent){svg.dispatchEvent(newCustomEvent(SVG_POINTER_EVENT.Click,{detail: getSvgPointer(e)}));}svg.addEventListener('pointermove',handlePointerMove);svg.addEventListener('pointerdown',handlePointerDown);svg.addEventListener('pointerup',handlePointerUp);svg.addEventListener('click',handleClick);return{update(args){},destroy(){svg.removeEventListener('pointermove',handlePointerMove);svg.removeEventListener('pointerdown',handlePointerDown);svg.removeEventListener('pointerup',handlePointerUp);svg.removeEventListener('click',handleClick);},}satisfiesSvelteActionReturnType;}
If you think this could be an interesting addition, dont hesitate to adapt it so it better fits svelte-legos's apis.
The text was updated successfully, but these errors were encountered:
This is simply to propose the addition of a new action for the community, feel free to close or delete this issue if you think this is not a worthy proposal.
I recently had to work with pointer events and
svg
elements and faced the annoying case where the auto-scaling resulting from thesvg
'sviewBox
meant the pointer coordinates retrieved from native events were misaligned with thesvg
's coordinates space. This is just a question of reversing the transformation, and luckily could be done rather easily with methods already available. Here's the action I came up with:If you think this could be an interesting addition, dont hesitate to adapt it so it better fits
svelte-legos
's apis.The text was updated successfully, but these errors were encountered: