Created
March 5, 2023 10:37
-
-
Save vladanyes/f44926ae82d723d6781a2ea068b75c72 to your computer and use it in GitHub Desktop.
react-native-skia/example/src/Examples/Graphs/createGraphPath.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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