GithubHelp home page GithubHelp logo

cojoclaudiu / css-gradient-angle-to-svg-gradient-coordinates Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 1.0 18 KB

Converts a CSS gradient's angle in degrees to a an SVG gradient's coordinates (x1/y1/x2/y2) to produce a linear gradient of the same angle.

JavaScript 100.00%

css-gradient-angle-to-svg-gradient-coordinates's Introduction

css-gradient-angle-to-svg-gradient-coordinates

Converts a CSS gradient's angle in degrees to an SVG gradient's coordinates (x1/y1/x2/y2) to produce a linear gradient of the same angle.

install

yarn add css-gradient-angle-to-svg-gradient-coordinates

usage

import angleToCoordinates from 'css-gradient-angle-to-svg-gradient-coordinates'

const coordinates = angleToCoordinates(90)

const linearGradient = `
  <linearGradient x1="${coordinates.x1}" y1="${coordinates.y1}" x2="${coordinates.x2}" y2="${coordinates.y2}">
    <stop offset="0%" stop-color="#000000" />
    <stop offset="75%" stop-color="#FFFFFF" />
  </linearGradient
`

API

angleToCoordinates(angleInDegrees, [sizeOfSquare])

Takes an angle in degrees and finds a set of coordinates around the edge of a square which will draw a line dissecting the center of that square. For the purposes of using this to create SVG gradients, the square is 1 long and 1 high, such that the output coordinates are percentages that will understand โ€” but this is also customizable.

  • angleInDegrees - an angle in degrees between -Infinity to Infinity.
  • sizeOfSquare (optional, default = 1) - the size of the square to calculate coordinates on. Usually not necessary to specify unless you've changed gradientUnits or you're trying to do something else with this module.

Returns an object that looks like this: { x1: 0.5, y1: 1, x2: 0.5, y2: 0 }

testing

yarn run test

license

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.