33
loading...
This website collects cookies to deliver better user experience
// You've got a value that bounces back and forth from -multiplier to +multiplier
value = offsetValue + multiplier * Math.cos(angle)
Add the cos and sin components to the x and y coordinates of each menu item based on their element index.
// space the menu items equally around the radial menu
let wholeCircle = 2*Math.PI
let menuRadius = 120;
let angleStep = wholeCircle/numberOfMenuItems
for(let idx in menuItems) {
//set each menuItem's position around a circle
menuItems[idx].xPos = menuCenter.xPos + menuRadius * Math.cos(angleStep*idx)
menuItems[idx].yPos = menuCenter.yPos + menuRadius * Math.sin(angleStep*idx)
}
We are basically done now! Just add a bit of animation to ease in from initial to final positions and voila! you've got a neat looking, highly customisable radial menu in just few lines of code!
gotcha
moments while working on it. If you've got anything interesting made with math/generative art with code. Please share them in the comments! I'd love to see them. 🌠