Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys detail of RadialGradient data #121

Open
Wykay-z opened this issue Nov 3, 2021 · 1 comment
Open

Keys detail of RadialGradient data #121

Wykay-z opened this issue Nov 3, 2021 · 1 comment

Comments

@Wykay-z
Copy link

Wykay-z commented Nov 3, 2021

Issue in https://www.adobe.io/xd/uxp/develop/reference/RadialGradient/

Hi there,

I was having a hard time trying to transform the data of RadialGradient to css gradient. There are many keys: startX, startY, startR, endX, endY, endR and gradeintTransfrom with a matrix value. I can get the start point and gradient radius, however there is no information about the transform matrix, so I can't calculate the degree of the gradient.

image

The tutorial only points out what endR means. How about other keys? What are they representing. Please tell us more detail.

Thank you.

Reference: Figma gradient position handler from https://www.figma.com/developers/api
image

@DaveInMatrix
Copy link
Contributor

The rotation of a RadialGradient is not directly exposed via the plugin APIs at this time. However the transformation matrix is provided from toString() on a shape's fill. I answered a similar question here about how to get useful information out of a matrix. This code demonstrates how to get the rotation of a RadialGradient:

function getRotationFromMatrix2D(matrix2D) {
let a = matrix2D.a;
let b = matrix2D.b;
let c = matrix2D.c;
let d = matrix2D.d;
let rotation = 0;
if (a != 0 || b != 0) {
let r = Math.sqrt(a * a + b * b);
rotation = (b > 0 ? Math.acos(a / r) : -Math.acos(a / r)) * (180 / Math.PI);
} else if (c != 0 || d != 0) {
let s = Math.sqrt(c * c + d * d);
rotation = (Math.PI / 2 - (d > 0 ? Math.acos(-c / s) : -Math.acos(c / s))) * (180 / Math.PI);
}
return rotation;
}

const fillValue = selection.items[0].fill;
const transform = fillValue.toString().split("(")[1].split(")")[0].split(",");
const gradientMatrix = new Matrix(transform[0], transform[1], transform[2], transform[3], transform[4], transform[5]);
// Default rotation is 0 degrees, which is to the right
const gradientRotation = getRotationFromMatrix2D(gradientMatrix);
console.log("gradientRotation: " + gradientRotation);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants