figma

package module
v0.0.0-...-8ac4168 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2018 License: MIT Imports: 8 Imported by: 10

README

figma

A Golang package for interacting with the Figma APIs

godoc license Go Report Card

TODO

  • [GET] Files
  • [GET] Images
  • [POST] Comments
  • [GET] Team projects
  • [GET] Project files
  • Testing
  • CI integration

Getting started

Before using this package, you will need a Figma account and a personal access token.

Installation

> go get -u github.com/torie/figma

Usage

Create a Figma client
c := figma.New("access-token")
Get a Figma document
f, err := c.File("document-key")
Render a node as PNG
imgs, err := c.Images("document-key", 2, figma.ImageFormatPNG, "node-id")
Examples

Examples can be found in the examples folder

Documentation

Overview

Package figma is a Golang package for interacting with the Figma APIs.

Index

Constants

View Source
const (
	// NodeTypeDocument is the root node
	NodeTypeDocument NodeType = "DOCUMENT"

	// NodeTypeCanvas represents a single page
	NodeTypeCanvas = "CANVAS"

	// NodeTypeFrame is A node of fixed size containing other nodes
	NodeTypeFrame = "FRAME"

	// NodeTypeGroup is a logical grouping of nodes
	NodeTypeGroup = "GROUP"

	// NodeTypeVector is a vector network, consisting of vertices and edges
	NodeTypeVector = "VECTOR"

	// NodeTypeBoolean is a group that has a boolean operation applied to it
	NodeTypeBoolean = "BOOLEAN"

	// NodeTypeStar is a regular star shape
	NodeTypeStar = "STAR"

	// NodeTypeLine is a straight line
	NodeTypeLine = "LINE"

	// NodeTypeEllipse is an ellipse
	NodeTypeEllipse = "ELLIPSE"

	// NodeTypeRegularPolygon is a regular n-sided polygon
	NodeTypeRegularPolygon = "REGULAR_POLYGON"

	// NodeTypeRectangle is a rectangle
	NodeTypeRectangle = "RECTANGLE"

	// NodeTypeText is a text box
	NodeTypeText = "TEXT"

	// NodeTypeSlice is a rectangular region of the canvas that can be exported
	NodeTypeSlice = "SLICE"

	// NodeTypeComponent is a node that can have instances created of it that share the same properties
	NodeTypeComponent = "COMPONENT"

	// NodeTypeInstance is an instance of a component, changes to the component result in the same changes applied to the instance
	NodeTypeInstance = "INSTANCE"
)
View Source
const (
	StrokeAlignInside  StrokeAlign = "INSIDE"  // draw stroke inside the shape boundary
	StrokeAlignOutside             = "OUTSIDE" // draw stroke outside the shape boundary
	StrokeAlignCenter              = "CENTER"  // draw stroke centered along the shape boundary
)
View Source
const (
	// HorizontalLayoutConstraintTop specifies a node which is laid out relative
	// to top of the containing frame.
	HorizontalLayoutConstraintTop HorizontalLayoutConstraint = "TOP"

	// HorizontalLayoutConstraintBottom specifies a node which is laid out
	// relative to bottom of the containing frame.
	HorizontalLayoutConstraintBottom = "BOTTOM"

	// HorizontalLayoutConstraintCenter specifies a node which is vertically
	// centered relative to containing frame.
	HorizontalLayoutConstraintCenter = "CENTER"

	// HorizontalLayoutConstraintTopBottom specifies a node where the top and
	// bottom of node are constrained relative to containing frame (node
	// stretches with frame).
	HorizontalLayoutConstraintTopBottom = "TOP_BOTTOM"

	// HorizontalLayoutConstraintScale specifies a node which scales vertically
	// with containing frame.
	HorizontalLayoutConstraintScale = "SCALE"
)
View Source
const (
	// VerticalLayoutConstraintLeft specifies a node which is laid out relative
	// to left of the containing frame.
	VerticalLayoutConstraintLeft VerticalLayoutConstraint = "LEFT"

	// VerticalLayoutConstraintRight specifies a node which is laid out relative
	// to right of the containing frame.
	VerticalLayoutConstraintRight = "RIGHT"

	// VerticalLayoutConstraintCenter specifies a node which is horizontally
	// centered relative to containing frame.
	VerticalLayoutConstraintCenter = "CENTER"

	// VerticalLayoutConstraintLeftRight specifies a node where the left and
	// right side of the node are constrained relative to containing frame (node
	// stretches with frame).
	VerticalLayoutConstraintLeftRight = "LEFT_RIGHT"

	// VerticalLayoutConstraintScale specifies a node which scales horizontally
	// with containing frame.
	VerticalLayoutConstraintScale = "SCALE"
)
View Source
const (
	AlignmentMin    Alignment = "MIN"
	AlignmentMax              = "MAX"
	AlignmentCenter           = "CENTER"
)
View Source
const (
	LayoutGridPatternColumns LayoutGridPattern = "COLUMNS"
	LayoutGridPatternRows                      = "ROWS"
	LayoutGridPatternGrid                      = "GRID"
)
View Source
const (
	EffectTypeInnerShadow    EffectType = "INNER_SHADOW"
	EffectTypeDropShadow                = "DROP_SHADOW"
	EffectTypeLayerBlur                 = "LAYER_BLUR"
	EffectTypeBackgroundBlur            = "BACKGROUND_BLUR"
)
View Source
const (
	// Normal blends
	BlendModePassThrough = "PASS_THROUGH" // Only applicable to objects with children
	BlendModeNormal      = "NORMAL"

	// Darken
	BlendModeDarken     = "DARKEN"
	BlendModeMultiply   = "MULTIPLY"
	BlendModeLinearBurn = "LINEAR_BURN"
	BlendModeColorBurn  = "COLOR_BURN"

	// Lighten
	BlendModeLighten     = "LIGHTEN"
	BlendModeScreen      = "SCREEN"
	BlendModeLinearDodge = "LINEAR_DODGE"
	BlendModeColorDodge  = "COLOR_DODGE"

	// Contrast
	BlendModeOverlay   = "OVERLAY"
	BlendModeSoftLight = "SOFT_LIGHT"
	BlendModeHardLight = "HARD_LIGHT"

	// Inversion
	BlendModeDifference = "DIFFERENCE"
	BlendModeExclusion  = "EXCLUSION"

	// Component
	BlendModeHue        = "HUE"
	BlendModeSaturation = "SATURATION"
	BlendModeColor      = "COLOR"
	BlendModeLuminosity = "LUMINOSITY"
)
View Source
const (
	ConstraintTypeScale  ConstraintType = "SCALE"
	ConstraintTypeWidth                 = "WIDTH"
	ConstraintTypeHeight                = "HEIGTH"
)
View Source
const (
	PaintTypeSolid           PaintType = "SOLID"
	PaintTypeGradientLinear            = "GRADIENT_LINEAR"
	PaintTypeGradientRadial            = "GRADIENT_RADIAL"
	PaintTypeGradientAngular           = "GRADIENT_ANGULAR"
	PaintTypeGradientDiamond           = "GRADIENT_DIAMOND"
	PaintTypeImage                     = "IMAGE"
	PaintTypeEmoji                     = "EMOJI"
)
View Source
const (
	ImageFormatPNG ImageFormat = "png"
	ImageFormatSVG             = "svg"
	ImageFormatJPG             = "jpg"
)
View Source
const (
	ScaleModeFill    ScaleMode = "FILL"
	ScaleModeFit               = "FIT"
	ScaleModeTile              = "TILE"
	ScaleModeStretch           = "STRETCH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Alignment

type Alignment string

Alignment describes positioning of a grid.

type BlendMode

type BlendMode string

BlendMode describes how a layer blends with layers below.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client allows you to interact with the Figma APIs.

func New

func New(token string) *Client

New returns a client initialized with the personal access token provided.

func (*Client) AddComment

func (c *Client) AddComment(key, message string, v Vector) (Comment, error)

AddComment posts a new comment on the file.

 key is the file to add the comment to.
	message is the text contents of the comment to post.
	v is the absolute canvas position of where to place the comment.

func (*Client) Comments

func (c *Client) Comments(key string) (Comments, error)

Comments returns a list of comments made on a file.

key is the file to retrieve comments from.

func (*Client) File

func (c *Client) File(key string) (File, error)

File returns the document referred to by key.

key is the file to export from.

The file key can be parsed from any Figma file url: https://www.figma.com/file/:key/:title.

func (*Client) FileVersions

func (c *Client) FileVersions(key string) ([]Version, error)

FileVersions returns a list of the version history of a file. The version history consists of versions, manually-saved additions to the version history of a file. If the account is not on a paid team, version history is limited to the past 30 days. Note that version history will not include autosaved versions.

key is the file to retrieve versions from.

Note: This endpoint by default will paginate the results, starting with the most recent 30 results.

func (*Client) Images

func (c *Client) Images(key string, scale float64, i ImageFormat, ids ...string) (Images, error)

Images returns a map of URLs for rendered images of the nodes provided.

key is the file to export images from.
format specifies the image output format.
scale is the image scaling factor, it must be between 0.1 and 4.0.
ids is a list of node IDs to render.

Important: the image map may contain values that are null. This indicates that rendering of that specific node has failed. This may be due to the node id not existing, or other reasons such has the node having no renderable components. It is guaranteed that any node that was requested for rendering will be represented in this map whether or not the render succeeded.

func (*Client) TeamProjects

func (c *Client) TeamProjects(teamID string) ([]TeamProject, error)

TeamProjects lists the projects for a specified team. Note that this will only return projects visible to the authenticated user or owner of the developer token.

teamID is the id of the team to list projects from

type Color

type Color struct {
	Alpha float64 `json:"a"`
	Red   float64 `json:"r"`
	Green float64 `json:"g"`
	Blue  float64 `json:"b"`
}

Color is an RGBA color.

type ColorStop

type ColorStop struct {
	// Value between 0 and 1 representing position along gradient axis
	Position float64

	// Color attached to corresponding position
	Color Color
}

ColorStop is a position color pair representing a gradient stop.

type Comment

type Comment struct {
	//	Unique identifier for comment
	ID string

	// The absolute coordinates of where the comment is on the canvas
	ClientMeta Vector `json:"client_meta"`

	// The file in which the comment lives
	FileKey string `json:"file_key"`

	// If present, the id of the comment to which this is the reply
	ParentID string `json:"parent_id"`

	// The user who left the comment
	User User `json:"user"`

	// The time at which the comment was left
	CreatedAt time.Time `json:"created_at"`

	// If set, when the comment was resolved
	ResolvedAt time.Time `json:"resolved_at"`

	// Only set for top level comments. The number displayed with the comment in the UI
	OrderID int `json:"order_id,string"`
}

Comment is a comment or reply left by a user.

type Comments

type Comments []Comment

Comments is a slice of Comment.

type Component

type Component struct {
	// The name of the component
	Name string

	// The description of the component as entered in the editor
	Description string
}

Component is a description of a master component. Helps you identify which component instances are attached to.

type Constraint

type Constraint struct {
	Type  ConstraintType
	Value float64
}

Constraint describes sizing constraint for exports.

type ConstraintType

type ConstraintType string

ConstraintType specifies the type of a constraint.

type Effect

type Effect struct {
	// Type of effect
	Type EffectType

	// Is the effect active?
	Visible bool

	// Radius of the blur effect (applies to shadows as well)
	Radius float64

	// The following properties are for shadows only:
	// The color of the shadow
	Color Color

	// Blend mode of the shadow
	BlendMode BlendMode

	// How far the shadow is projected in the x and y directions
	Offset Vector
}

Effect describes a visual effect such as a shadow or blur.

type EffectType

type EffectType string

EffectType is the type of effect as a string enum.

type ExportSetting

type ExportSetting struct {
	Suffix     string
	Format     ImageFormat
	Constraint string
}

ExportSetting describes the format and size to export an asset at.

type File

type File struct {
	// A mapping from NodeIDs to component metadata This is to help you
	// determine which components each instance comes from. Currently the only
	// piece of metadata available on components is the name of the component,
	// but more properties will be forthcoming.
	Components struct {
	} `json:"components"`

	// A Node of type DOCUMENT.
	Document      Node `json:"document"`
	SchemaVersion int  `json:"schemaVersion"`
}

File contains a Figma file https://www.figma.com/file/:key/:title.

func (File) Nodes

func (f File) Nodes() []Node

Nodes returns a slice containing all subnodes of a Figma file.

type HorizontalLayoutConstraint

type HorizontalLayoutConstraint string

HorizontalLayoutConstraint is the layout constraint type for horizontal layout.

type Image

type Image struct {
	NodeID string
	URL    string
}

Image is a reference to a rendered node from Figma.

type ImageFormat

type ImageFormat string

ImageFormat specifies the file type of an image.

type Images

type Images []Image

Images is a slice of rendered nodes.

func (*Images) UnmarshalJSON

func (i *Images) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the Unmarshaler interface.

type LayoutConstraint

type LayoutConstraint struct {
	Horizontal HorizontalLayoutConstraint
	Vertical   VerticalLayoutConstraint
}

LayoutConstraint specifies the constraint relative to the containing Frame.

type LayoutGrid

type LayoutGrid struct {
	Pattern     LayoutGridPattern
	SectionSize float64
	Visible     bool
	Color       Color
	Alignment   Alignment
	GutterSize  float64
	Offset      float64
	Count       int
}

LayoutGrid contains guides to align and place objects within a frame.

type LayoutGridPattern

type LayoutGridPattern string

LayoutGridPattern describes the orientation of a grid.

type Node

type Node struct {
	// A string uniquely identifying this node within the document.
	ID string `json:"id"`

	// The name given to the node by the user in the tool.
	Name string `json:"name"`

	// The type of the node.
	Type NodeType `json:"type"`

	// Whether or not the node is visible on the canvas. (default: true)
	Visible bool `json:"visible"`

	Children        []Node           `json:"children"`
	BackgroundColor Color            `json:"backgroundColor"`
	ExportSettings  []ExportSetting  `json:"exportSettings"`
	BlendMode       BlendMode        `json:"blendMode"`
	PreserveRatio   bool             `json:"preserveRatio"`
	Constraints     LayoutConstraint `json:"constraints"`
	LayoutGrids     []LayoutGrid     `json:"layoutGrids"`

	TransitionNodeID    string `json:"transitionNodeID"`
	Opacity             float64
	AbsoluteBoundingBox Rectangle `json:"absoluteBoundingBox"`
	ClipsContent        bool      `json:"clipsContent,omitempty"`
	Effects             []Effect  `json:"effects"`
	IsMask              bool
	Fills               []Paint     `json:"fills,omitempty"`
	Strokes             []Paint     `json:"strokes,omitempty"`
	StrokeWeight        int         `json:"strokeWeight,omitempty"`
	StrokeAlign         StrokeAlign `json:"strokeAlign,omitempty"`
	Characters          string      `json:"characters,omitempty"`
	Style               TypeStyle   `json:"style,omitempty"`

	// Array with same number of elements as characeters in text box, each
	// element is a reference to the styleOverrideTable defined below and maps
	// to the corresponding character in the characters field. Elements with
	// value 0 have the default type style.
	CharacterStyleOverrides []int `json:"characterStyleOverrides,omitempty"`

	// Map from ID to TypeStyle for looking up style overrides.
	StyleOverrideTable map[int]TypeStyle `json:"styleOverrideTable,omitempty"`

	// ID of component that this instance came from, refers to components table.
	ComponentID string
}

Node contains a group of properties which specifies a leaf in a Figma File.

Files in Figma consist of a tree of nodes, each with some number of properties. One key property is type, which indicates what kind of node you are dealing with. At the root of every file is a DOCUMENT node, which has some number of CANVAS nodes as its children. Each canvas node represents a Figma Page. This section will explore each node type, what it is, and all the properties it has.

type NodeType

type NodeType string

NodeType specifies the kind of node. Different types of nodes have different properties.

type Paint

type Paint struct {
	// Type of paint as a string enum
	PaintType PaintType `json:"type"`

	// Is the paint enabled? (default: true)
	Visible bool

	// Overall opacity of paint (colors within the paint can also have opacity
	// values which would blend with this) (default: 1).
	Opacity float64

	// For solid paints:
	// Solid color of the paint
	Color Color

	// For gradient paints: This field contains three vectors, each of which are
	// a position in normalized object space (normalized object space is if the
	// top left corner of the bounding box of the object is (0, 0) and the
	// bottom right is (1,1)). The first position corresponds to the start of
	// the gradient (value 0 for the purposes of calculating gradient stops),
	// the second position is the end of the gradient (value 1), and the third
	// handle position determines the width of the gradient (only relevant for
	// non-linear gradients).
	GradientHandlePositions []Vector

	// Positions of key points along the gradient axis with the colors anchored
	// there. Colors along the gradient are interpolated smoothly between
	// neighboring gradient stops.
	GradientStops []ColorStop

	// For image paints:
	// Image scaling mode
	ScaleMode ScaleMode
}

Paint is a solid color, gradient, or image texture that can be applied as fills or strokes.

type PaintType

type PaintType string

PaintType specifies the type of paint applied.

type Rectangle

type Rectangle struct {
	X      float64 `json:"x"`
	Y      float64 `json:"y"`
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

Rectangle expresses a bounding box in absolute coordinates.

type ScaleMode

type ScaleMode string

ScaleMode specifies the scaling mode of an image.

type StrokeAlign

type StrokeAlign string

StrokeAlign specifies where a stroke is drawn relative to the vector outline.

type TeamProject

type TeamProject struct {
	// The ID of the project
	ID string `json:"id"`

	// The Name of the project
	Name string `json:"name"`
}

TeamProject is a project which belongs to a team.

type TypeStyle

type TypeStyle struct {
	// Font family of text (standard name)
	FontFamily string `json:"fontFamily"`

	// PostScript font name
	FontPostScriptName string `json:"fontPostScriptName"`

	// Is text italicized?
	Italic bool

	// Numeric font weight
	FontWeight float64 `json:"fontWeight"`

	// Font size in px
	FontSize float64 `json:"fontSize"`

	// Horizontal text alignment
	TextAlignHorizontal HorizontalLayoutConstraint `json:"textAlignHorizontal"`

	// Vertical text alignment
	TextAlignVertical VerticalLayoutConstraint `json:"textAlignVertical"`

	// Space between characters in px
	LetterSpacing float64 `json:"letterSpacing"`

	// Paints applied to characters
	Fills []Paint

	// Line height in px
	LineHeightPx float64 `json:"lineHeightPx"`

	// Line height as a percentage of normal line height
	LineHeightPercent float64 `json:"lineHeightPercent"`
}

TypeStyle contains metadata for character formatting.

type User

type User struct {
	//	Name of the user
	Handle string `json:"handle"`

	//	URL link to the user's profile image
	ImgURL string `json:"img_url"`
}

User contains a description of a user.

type Vector

type Vector struct {
	X float64 `json:"x"`
	Y float64 `json:"y"`
}

Vector is a 2d vector.

type Version

type Version struct {
	// Unique identifier for version
	ID string `json:"id"`

	// The label given to the version in the editor
	Label string `json:"label"`

	// The description of the version as entered in the editor
	Description string `json:"description"`

	// The user that created the version
	User User `json:"user"`

	// The time at which the version was created
	CreatedAt time.Time `json:"created_at"`
}

Version describes a version of a file.

type VerticalLayoutConstraint

type VerticalLayoutConstraint string

VerticalLayoutConstraint is the layout constraint type for vertical layout.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL