Skip to content

Instantly share code, notes, and snippets.

@vladanyes
Created March 5, 2023 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladanyes/f44926ae82d723d6781a2ea068b75c72 to your computer and use it in GitHub Desktop.
Save vladanyes/f44926ae82d723d6781a2ea068b75c72 to your computer and use it in GitHub Desktop.
react-native-skia/example/src/Examples/Graphs/createGraphPath.ts
import { Skia } from "@shopify/react-native-skia";
export const createGraphPath = (
width: number,
height: number,
steps: number,
round = true
) => {
const retVal = Skia.Path.Make();
let y = height / 2;
retVal.moveTo(0, y);
const prevPt = { x: 0, y };
for (let i = 0; i < width; i += width / steps) {
// increase y by a random amount between -10 and 10
y += Math.random() * 30 - 15;
y = Math.max(height * 0.2, Math.min(y, height * 0.7));
if (round && i > 0) {
const xMid = (prevPt.x + i) / 2;
const yMid = (prevPt!.y + y) / 2;
retVal.quadTo(prevPt.x, prevPt.y, xMid, yMid);
prevPt.x = i;
prevPt.y = y;
} else {
retVal.lineTo(i, y);
}
}
return retVal;
};
export const createZeroPath = (
width: number,
height: number,
steps: number
) => {
const retVal = Skia.Path.Make();
const y = height / 2;
retVal.moveTo(0, y);
for (let i = 0; i < width; i += width / steps) {
retVal.lineTo(i, y);
}
return retVal;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment