question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to trasnlate from Dimensions screen width to svg coordinates

See original GitHub issue

I want to do viewBox using the coordinates I set and accordingly determined for the same ratio of x and y to get the coordinates. I can do this easily on Dom. How can I do this in my `react-native 'application using this package.

let point = svg.createSVGPoint();
point.x = e.clientX;
point.y = e.clientY;
let CTM = svg.getScreenCTM().inverse();
point = point.matrixTransform(CTM);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:26

github_iconTop GitHub Comments

2reactions
msandcommented, Oct 7, 2019

This pattern seems to perform quite alright:

import React, {memo, PureComponent} from 'react';
import {View, Dimensions, StyleSheet} from 'react-native';
import Svg, {Circle} from 'react-native-svg';
import Animated from 'react-native-reanimated';
import {PanGestureHandler} from 'react-native-gesture-handler';
const {
  add,
  cos,
  sin,
  sub,
  atan,
  cond,
  Value,
  event,
  divide,
  multiply,
  lessThan,
  View: AnimatedView,
} = Animated;
const {PI} = Math;
const {absoluteFill} = StyleSheet;
const {width, height} = Dimensions.get('window');
const smallestSide = Math.min(width, height);
const r = smallestSide * 0.42;
const cy = height / 2;
const cx = width / 2;

const sliderSize = 40;
const sliderRadius = 20;

const slider = (
  <Svg width={sliderSize} height={sliderSize}>
    <Circle cx={sliderRadius} cy={sliderRadius} r={sliderRadius} />
  </Svg>
);
const Slider = memo(function Slider() {
  return slider;
});

const background = (
  <Svg width={width} height={height} style={absoluteFill}>
    <Circle
      r={r}
      cx={cx}
      cy={cy}
      fill="none"
      stroke="#aaa"
      strokeWidth="7"
      strokeDasharray="1,6"
    />
  </Svg>
);
const Background = memo(function Background() {
  return background;
});

export default class CircularSlider extends PureComponent {
  dragX = new Value(0);
  dragY = new Value(0);
  onGestureEvent = event([
    {
      nativeEvent: {
        absoluteX: this.dragX,
        absoluteY: this.dragY,
      },
    },
  ]);
  rad = add(
    atan(divide(sub(this.dragY, cy), sub(this.dragX, cx))),
    cond(lessThan(this.dragX, cx), PI, 0),
  );
  x = add(cx, multiply(r, cos(this.rad)));
  y = add(cy, multiply(r, sin(this.rad)));
  render() {
    const {onGestureEvent, x, y} = this;
    return (
      <View style={absoluteFill}>
        <Background />
        <View style={sliderStyles}>
          <AnimatedView
            style={{
              transform: [{translateX: x}, {translateY: y}],
            }}>
            <Slider />
          </AnimatedView>
        </View>
        <PanGestureHandler
          onGestureEvent={onGestureEvent}
          onHandlerStateChange={onGestureEvent}>
          <AnimatedView style={absoluteFill} />
        </PanGestureHandler>
      </View>
    );
  }
}

const {sliderOffset} = StyleSheet.create({
  sliderOffset: {
    transform: [{translateX: -sliderRadius}, {translateY: -sliderRadius}],
  },
});
const sliderStyles = StyleSheet.compose(
  absoluteFill,
  sliderOffset,
);

1reaction
msandcommented, Oct 6, 2019

To make it compatible with the web (current version has flickering when the slider circle is under the mouse) you need to change the move handler to use pageX and pageY instead of locationX and locationY:

      onPanResponderMove: ({nativeEvent}) => {
        const {pageX, pageY} = nativeEvent;
        this.setState({
          v: this.cartesianToPolar(pageX, pageY),
        });
      },
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Translate from DOM to SVG Coordinates and Back Again
Mix DOM and vector interactions in SVG, translating from SVG to DOM coordinates and back, and translating to transformed SVG coordinates.
Read more >
How to trasnlate from Dimensions screen width to svg ... - GitHub
I want to do viewBox using the coordinates I set and accordingly determined for the same ratio of x and y to get...
Read more >
How to Translate from DOM to SVG Coordinates and Back ...
The getBoundingClientRect() returns the position as size of the item. With properties- x,y, Width, Height, Left, Right, Top, Bottom.
Read more >
Coordinate Systems, Transformations and Units — SVG 2
This process converts the min-x, min-y, width and height values of a viewBox attribute, the position and size of the element on which...
Read more >
Can not translate screen x,y coordinates to svg coordinates
I have read that point.matrixTransform(svgRoot.getScreenCTM().inverse()); should do the trick and translate screen coordinates to SVG ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found